Firebase
view release on metacpan or search on metacpan
lib/Firebase.pm view on Meta::CPAN
);
has agent => (
is => 'ro',
required => 0,
lazy => 1,
default => sub { HTTP::Thin->new() },
);
sub get {
my ($self, $path, $params) = @_;
my $uri = $self->create_uri($path, $params);
return $self->process_request( GET $uri );
}
sub delete {
my ($self, $path) = @_;
my $uri = $self->create_uri($path);
return $self->process_request( DELETE $uri );
}
sub put {
my ($self, $path, $params) = @_;
my $uri = $self->create_uri($path);
my $request = POST($uri->as_string, Content_Type => 'form-data', Content => to_json($params));
$request->method('PUT'); # because HTTP::Request::Common treats PUT as GET rather than POST
return $self->process_request( $request );
}
sub patch {
my ($self, $path, $params) = @_;
my $uri = $self->create_uri($path);
my $request = POST($uri->as_string, Content_Type => 'form-data', Content => to_json($params));
$request->method('PATCH'); # because HTTP::Request::Common treats PUT as GET rather than POST
return $self->process_request( $request );
}
sub post {
my ($self, $path, $params) = @_;
my $uri = $self->create_uri($path);
my $request = POST($uri->as_string, Content_Type => 'form-data', Content => to_json($params));
return $self->process_request( $request );
}
sub create_uri {
my ($self, $path) = @_;
my $url = 'https://'.$self->firebase.'.firebaseio.com/'.$path.'.json';
$url .= '?auth='.$self->authobj->create_token if $self->has_authobj || $self->has_auth;
return URI->new($url);
}
sub process_request {
my $self = shift;
$self->process_response($self->agent->request( @_ ));
}
sub process_response {
my ($self, $response) = @_;
$self->debug($response->header('X-Firebase-Auth-Debug'));
if ($response->is_success) {
if ($response->decoded_content eq 'null') {
return undef;
}
else {
my $result = eval { from_json($response->decoded_content) };
if ($@) {
warn $response->decoded_content;
ouch 500, 'Server returned unparsable content.';#, { error => $@, content => $response->decoded_content };
}
return $result;
}
}
else {
ouch 500, $response->status_line, $response->decoded_content;
}
}
=head1 NAME
Firebase - An interface to firebase.com.
=head1 VERSION
version 1.0002
=head1 SYNOPSIS
use Firebase;
my $fb = Firebase->new(firebase => 'myfirebase', auth => { secret => 'xxxxxxx', data => { uid => 'xxx', username => 'fred' }, admin => \1 } );
my $result = $fb->put('foo', { this => 'that' });
my $result = $fb->get('foo'); # or $fb->get('foo/this');
my $result = $fb->delete('foo');
=head1 DESCRIPTION
This is a light-weight wrapper around the Firebase REST API. Firebase is a real-time web service that acts as both a queue and a datastore. It's used for building real-time web apps and web services.
More info at L<https://www.firebase.com/docs/rest-api-quickstart.html>.
=head1 METHODS
=head2 new
Constructor
=over
=item firebase
Required. The name of your firebase.
=item auth
The parameters you'd pass to create a C<Firebase::Auth> object. This is a shortcut for constructing the object yourself and passing it into C<authobj>.
=item authobj
A L<Firebase::Auth> object. Will be generated for you automatically if you don't supply one, but do supply C<auth>.
=item agent
A user agent. An L<HTTP::Thin> object will be generated for you automatically if you don't supply one.
=back
=head2 get
Fetch some data from firebase.
=over
( run in 0.773 second using v1.01-cache-2.11-cpan-e1769b4cff6 )