File Coverage

blib/lib/Firebase.pm
Criterion Covered Total %
statement 57 59 96.6
branch 7 8 87.5
condition 3 3 100.0
subroutine 15 15 100.0
pod 8 8 100.0
total 90 93 96.7


line stmt bran cond sub pod time code
1             package Firebase;
2             $Firebase::VERSION = '1.0002';
3 1     1   4005 use Moo;
  1         23728  
  1         7  
4 1     1   2534 use Firebase::Auth;
  1         6  
  1         50  
5 1     1   1433 use HTTP::Thin;
  1         483085  
  1         29  
6 1     1   787 use HTTP::Request::Common qw(DELETE PUT GET POST);
  1         3343  
  1         84  
7 1     1   7 use Ouch;
  1         2  
  1         153  
8 1     1   951 use JSON;
  1         5257  
  1         7  
9 1     1   135 use URI;
  1         3  
  1         655  
10              
11             has firebase => (
12             is => 'ro',
13             required => 1,
14             );
15              
16             has auth => (
17             is => 'ro',
18             predicate => 'has_auth',
19             );
20              
21             has authobj => (
22             is => 'rw',
23             lazy => 1,
24             predicate => 'has_authobj',
25             default => sub {
26             Firebase::Auth->new(%{$_[0]->auth});
27             },
28             );
29              
30             has debug => (
31             is => 'rw',
32             default => sub { '' },
33             );
34              
35             has agent => (
36             is => 'ro',
37             required => 0,
38             lazy => 1,
39             default => sub { HTTP::Thin->new() },
40             );
41              
42             sub get {
43 3     3 1 7886 my ($self, $path, $params) = @_;
44 3         19 my $uri = $self->create_uri($path, $params);
45 3         316 return $self->process_request( GET $uri );
46             }
47              
48             sub delete {
49 3     3 1 10152 my ($self, $path) = @_;
50 3         17 my $uri = $self->create_uri($path);
51 3         388 return $self->process_request( DELETE $uri );
52             }
53              
54             sub put {
55 4     4 1 2103 my ($self, $path, $params) = @_;
56 4         19 my $uri = $self->create_uri($path);
57 4         10378 my $request = POST($uri->as_string, Content_Type => 'form-data', Content => to_json($params));
58 4         1847 $request->method('PUT'); # because HTTP::Request::Common treats PUT as GET rather than POST
59 4         49 return $self->process_request( $request );
60             }
61              
62             sub patch {
63 1     1 1 845 my ($self, $path, $params) = @_;
64 1         7 my $uri = $self->create_uri($path);
65 1         114 my $request = POST($uri->as_string, Content_Type => 'form-data', Content => to_json($params));
66 1         365 $request->method('PATCH'); # because HTTP::Request::Common treats PUT as GET rather than POST
67 1         12 return $self->process_request( $request );
68             }
69              
70             sub post {
71 1     1 1 804 my ($self, $path, $params) = @_;
72 1         7 my $uri = $self->create_uri($path);
73 1         109 my $request = POST($uri->as_string, Content_Type => 'form-data', Content => to_json($params));
74 1         393 return $self->process_request( $request );
75             }
76              
77             sub create_uri {
78 12     12 1 36 my ($self, $path) = @_;
79 12         93 my $url = 'https://'.$self->firebase.'.firebaseio.com/'.$path.'.json';
80 12 100 100     524 $url .= '?auth='.$self->authobj->create_token if $self->has_authobj || $self->has_auth;
81 12         152 return URI->new($url);
82             }
83              
84             sub process_request {
85 12     12 1 615 my $self = shift;
86 12         345 $self->process_response($self->agent->request( @_ ));
87             }
88              
89             sub process_response {
90 12     12 1 8012558 my ($self, $response) = @_;
91 12         103 $self->debug($response->header('X-Firebase-Auth-Debug'));
92 12 100       1252 if ($response->is_success) {
93 11 100       168 if ($response->decoded_content eq 'null') {
94 4         621 return undef;
95             }
96             else {
97 7         1046 my $result = eval { from_json($response->decoded_content) };
  7         23  
98 7 50       1018 if ($@) {
99 0         0 warn $response->decoded_content;
100 0         0 ouch 500, 'Server returned unparsable content.';#, { error => $@, content => $response->decoded_content };
101             }
102 7         109 return $result;
103             }
104             }
105             else {
106 1         16 ouch 500, $response->status_line, $response->decoded_content;
107             }
108             }
109              
110             =head1 NAME
111              
112             Firebase - An interface to firebase.com.
113              
114             =head1 VERSION
115              
116             version 1.0002
117              
118             =head1 SYNOPSIS
119              
120             use Firebase;
121            
122             my $fb = Firebase->new(firebase => 'myfirebase', auth => { secret => 'xxxxxxx', data => { uid => 'xxx', username => 'fred' }, admin => \1 } );
123            
124             my $result = $fb->put('foo', { this => 'that' });
125             my $result = $fb->get('foo'); # or $fb->get('foo/this');
126             my $result = $fb->delete('foo');
127            
128             =head1 DESCRIPTION
129              
130             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.
131              
132             More info at L.
133              
134             =head1 METHODS
135              
136              
137             =head2 new
138              
139             Constructor
140              
141             =over
142              
143             =item firebase
144              
145             Required. The name of your firebase.
146              
147             =item auth
148              
149             The parameters you'd pass to create a C object. This is a shortcut for constructing the object yourself and passing it into C.
150              
151             =item authobj
152              
153             A L object. Will be generated for you automatically if you don't supply one, but do supply C.
154              
155             =item agent
156              
157             A user agent. An L object will be generated for you automatically if you don't supply one.
158              
159             =back
160              
161              
162             =head2 get
163              
164             Fetch some data from firebase.
165              
166             =over
167              
168             =item path
169              
170             The path to the info you want to fetch.
171              
172             =back
173              
174              
175             =head2 put
176              
177             Put some data into a firebase.
178              
179             =over
180              
181             =item path
182              
183             The path where the info should be stored.
184              
185             =item params
186              
187             A hash reference of parameters to be stored at this location.
188              
189             B Firebase doesn't work with arrays, so you can nest scalars and hashes here, but not arrays.
190              
191             =back
192              
193             =head2 patch
194              
195             Partial update of data in a location
196              
197             =over
198              
199             =item path
200              
201             The path where the info should be stored.
202              
203             =item params
204              
205             A hash reference of parameters to be updated at this location.
206              
207             =back
208              
209              
210             =head2 post
211              
212             Adds data to an existing location, creating a hash of objects below the path.
213              
214             =over
215              
216             =item path
217              
218             The path where the info should be stored.
219              
220             =item params
221              
222             A hash reference of parameters to be stored at this location.
223              
224             B Firebase doesn't work with arrays, so you can nest scalars and hashes here, but not arrays.
225              
226             =back
227              
228              
229             =head2 delete
230              
231             Delete some data from a firebase.
232              
233             =over
234              
235             =item path
236              
237             The path where the info is that you want deleted.
238              
239             =back
240              
241              
242              
243             =head2 debug
244              
245             If C has been set to a true value in C, this will return the debug message returned with the previous response.
246              
247              
248              
249              
250             =head2 create_uri
251              
252             Creates a URI to a firebase data segment. You almost certainly want to use C, C or C instead.
253              
254             =over
255              
256             =item path
257              
258             The path to the data.
259              
260             =item params
261              
262             Any parameters you need to pass for any reason.
263              
264             =back
265              
266              
267             =head2 process_request
268              
269             Requests data and runs it through C. You almost certainly want to use C, C or C instead.
270              
271             =over
272              
273             =item request
274              
275             An L object.
276              
277             =back
278              
279             =head2 process_response
280              
281             Checks for errors, decodes json, and returns a result. You almost certainly want to use C, C or C instead.
282              
283             =over
284              
285             =item response
286              
287             An L object.
288              
289             =back
290              
291              
292             =head1 AUTHOR
293              
294             =over
295              
296             =item *
297              
298             Kiran Kumar, C<< >>
299              
300             =item *
301              
302             JT Smith, C<< >>
303              
304             =back
305              
306              
307              
308             =head1 SUPPORT
309              
310             =over
311              
312             =item Source Code Repository
313              
314             L
315              
316             =item Issue Tracker
317              
318             L
319              
320             =back
321              
322              
323              
324             =head1 LICENSE AND COPYRIGHT
325              
326             Copyright 2013 Plain Black Corporation
327              
328             This program is free software; you can redistribute it and/or modify it
329             under the terms of the the Artistic License (2.0). You may obtain a
330             copy of the full license at:
331              
332             L
333              
334             Any use, modification, and distribution of the Standard or Modified
335             Versions is governed by this Artistic License. By using, modifying or
336             distributing the Package, you accept this license. Do not use, modify,
337             or distribute the Package, if you do not accept this license.
338              
339             If your Modified Version has been derived from a Modified Version made
340             by someone other than you, you are nevertheless required to ensure that
341             your Modified Version complies with the requirements of this license.
342              
343             This license does not grant you the right to use any trademark, service
344             mark, tradename, or logo of the Copyright Holder.
345              
346             This license includes the non-exclusive, worldwide, free-of-charge
347             patent license to make, have made, use, offer to sell, sell, import and
348             otherwise transfer the Package with respect to any patent claims
349             licensable by the Copyright Holder that are necessarily infringed by the
350             Package. If you institute patent litigation (including a cross-claim or
351             counterclaim) against any party alleging that the Package constitutes
352             direct or contributory patent infringement, then this Artistic License
353             to you shall terminate on the date that such litigation is filed.
354              
355             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
356             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
357             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
358             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
359             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
360             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
361             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
362             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
363            
364             =cut
365              
366             1;