Firebase.pm

filter: Statementstmt 97.1% 68 / 70 Branchbran 87.5% 7 / 8 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 18 / 18 Podpod 100% 8 / 8 totaltotal 97.2% 106 / 109 CC 6 SCAR 5.9
CC
6
Cov
96%
process_response
4.1
create_uri
3.0
BEGIN
1.0
CRAP 1.8
« Firebase/Auth.pm ↑ Summary
1 package Firebase;
2 1 $Firebase::VERSION = '1.0002';
3 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Firebase::Auth;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use HTTP::Thin;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use HTTP::Request::Common qw(DELETE PUT GET POST);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Ouch;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 has firebase => (
12 is => 'ro',
13 required => 1,
14 );
15  
16 1 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 2 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
26 2 Firebase::Auth->new(%{$_[0]->auth});
27 },
28 1 );
29  
30 has debug => (
31 is => 'rw',
32 3 ▶ default => sub { '' },
Subroutine __ANON__: called CC=1 SCAR=0.0
33 1 );
34  
35 has agent => (
36 is => 'ro',
37 required => 0,
38 lazy => 1,
39 3 ▶ default => sub { HTTP::Thin->new() },
Subroutine __ANON__: called CC=1 SCAR=0.0
40 1 );
41  
42 3 ▶sub get {
Subroutine get: called CC=1 SCAR=0.0
Pod documented
43 3 my ($self, $path, $params) = @_;
44 3 my $uri = $self->create_uri($path, $params);
45 3 return $self->process_request( GET $uri );
46 }
47  
48 3 ▶sub delete {
Subroutine delete: called CC=1 SCAR=0.0
Pod documented
49 3 my ($self, $path) = @_;
50 3 my $uri = $self->create_uri($path);
51 3 return $self->process_request( DELETE $uri );
52 }
53  
54 4 ▶sub put {
Subroutine put: called CC=1 SCAR=0.0
Pod documented
55 4 my ($self, $path, $params) = @_;
56 4 my $uri = $self->create_uri($path);
57 4 my $request = POST($uri->as_string, Content_Type => 'form-data', Content => to_json($params));
58 4 $request->method('PUT'); # because HTTP::Request::Common treats PUT as GET rather than POST
59 4 return $self->process_request( $request );
60 }
61  
62 1 ▶sub patch {
Subroutine patch: called CC=1 SCAR=0.0
Pod documented
63 1 my ($self, $path, $params) = @_;
64 1 my $uri = $self->create_uri($path);
65 1 my $request = POST($uri->as_string, Content_Type => 'form-data', Content => to_json($params));
66 1 $request->method('PATCH'); # because HTTP::Request::Common treats PUT as GET rather than POST
67 1 return $self->process_request( $request );
68 }
69  
70 1 ▶sub post {
Subroutine post: called CC=1 SCAR=0.0
Pod documented
71 1 my ($self, $path, $params) = @_;
72 1 my $uri = $self->create_uri($path);
73 1 my $request = POST($uri->as_string, Content_Type => 'form-data', Content => to_json($params));
74 1 return $self->process_request( $request );
75 }
76  
77 12 ▶sub create_uri {
Subroutine create_uri: called CC=3 SCAR=11.0
Pod documented
78 12 my ($self, $path) = @_;
79 12 my $url = 'https://'.$self->firebase.'.firebaseio.com/'.$path.'.json';
80 12 ▶ $url .= '?auth='.$self->authobj->create_token if $self->has_authobj || $self->has_auth;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_authobj or $self->has_auth 11 1 12
Condition100%3/3
$self->has_authobj or $self->has_auth
l!l&&r!l&&!r
1011
MC/DC100%2/2
$self->has_authobj or $self->has_auth
$self->has_authobj $self->has_auth
Truth table100%3/3
A or B
A= $self->has_authobjB= $self->has_auth
ABresult
1X1
011
000
81 12 return URI->new($url);
82 }
83  
84 12 ▶sub process_request {
Subroutine process_request: called CC=1 SCAR=0.0
Pod documented
85 12 my $self = shift;
86 12 $self->process_response($self->agent->request( @_ ));
87 }
88  
89 12 ▶sub process_response {
Subroutine process_response: called CC=4 SCAR=14.0
Pod documented
90 12 my ($self, $response) = @_;
91 12 $self->debug($response->header('X-Firebase-Auth-Debug'));
92 12 ▶ if ($response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) { } 11 1 12
93 11 ▶ if ($response->decoded_content eq 'null') {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->decoded_content eq 'null') { } 4 7 11
94 4 return undef;
95 }
96 else {
97 7 my $result = eval { from_json($response->decoded_content) };
98 7 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 7 7
99 0 warn $response->decoded_content;
100 0 ouch 500, 'Server returned unparsable content.';#, { error => $@, content => $response->decoded_content };
101 }
102 7 return $result;
103 }
104 }
105 else {
106 1 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<https://www.firebase.com/docs/rest-api-quickstart.html>.
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<Firebase::Auth> object. This is a shortcut for constructing the object yourself and passing it into C<authobj>.
150  
151 =item authobj
152  
153 A L<Firebase::Auth> object. Will be generated for you automatically if you don't supply one, but do supply C<auth>.
154  
155 =item agent
156  
157 A user agent. An L<HTTP::Thin> 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<Warning:> 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<Warning:> 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<debug> has been set to a true value in C<Firebase::Auth>, 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<get>, C<put> or C<delete> 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<process_response>. You almost certainly want to use C<get>, C<put> or C<delete> instead.
270  
271 =over
272  
273 =item request
274  
275 An L<HTTP::Request> 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<get>, C<put> or C<delete> instead.
282  
283 =over
284  
285 =item response
286  
287 An L<HTTP::Response> object.
288  
289 =back
290  
291  
292 =head1 AUTHOR
293  
294 =over
295  
296 =item *
297  
298 Kiran Kumar, C<< <kiran at brainturk.com> >>
299  
300 =item *
301  
302 JT Smith, C<< <jt at plainblack.com> >>
303  
304 =back
305  
306  
307  
308 =head1 SUPPORT
309  
310 =over
311  
312 =item Source Code Repository
313  
314 L<https://github.com/rizen/Firebase>
315  
316 =item Issue Tracker
317  
318 L<https://github.com/rizen/Firebase/issues>
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<http://www.perlfoundation.org/artistic_license_2_0>
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 1;
« Firebase/Auth.pm ↑ Summary