| 1 | package Firebase; | ||
| 2 | $Firebase::VERSION = '0.0400'; | ||
| 3 | use Moo; | ||
| 4 | use Firebase::Auth; | ||
| 5 | use HTTP::Thin; | ||
| 6 | use HTTP::Request::Common qw(DELETE PUT GET POST); | ||
| 7 | use Ouch; | ||
| 8 | use JSON; | ||
| 9 | use URI; | ||
| 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 | my ($self, $path, $params) = @_; | ||
| 44 | my $uri = $self->create_uri($path, $params); | ||
| 45 | return $self->process_request( GET $uri ); | ||
| 46 | } | ||
| 47 | |||
| 48 | sub delete { | ||
| 49 | my ($self, $path) = @_; | ||
| 50 | my $uri = $self->create_uri($path); | ||
| 51 | return $self->process_request( DELETE $uri ); | ||
| 52 | } | ||
| 53 | |||
| 54 | sub put { | ||
| 55 | my ($self, $path, $params) = @_; | ||
| 56 | my $uri = $self->create_uri($path); | ||
| 57 | my $request = POST($uri->as_string, Content_Type => 'form-data', Content => to_json($params)); | ||
| 58 | $request->method('PUT'); # because HTTP::Request::Common treats PUT as GET rather than POST | ||
| 59 | return $self->process_request( $request ); | ||
| 60 | } | ||
| 61 | |||
| 62 | sub patch { | ||
| 63 | my ($self, $path, $params) = @_; | ||
| 64 | my $uri = $self->create_uri($path); | ||
| 65 | my $request = POST($uri->as_string, Content_Type => 'form-data', Content => to_json($params)); | ||
| 66 | $request->method('PATCH'); # because HTTP::Request::Common treats PUT as GET rather than POST | ||
| 67 | return $self->process_request( $request ); | ||
| 68 | } | ||
| 69 | |||
| 70 | sub post { | ||
| 71 | my ($self, $path, $params) = @_; | ||
| 72 | my $uri = $self->create_uri($path); | ||
| 73 | my $request = POST($uri->as_string, Content_Type => 'form-data', Content => to_json($params)); | ||
| 74 | return $self->process_request( $request ); | ||
| 75 | } | ||
| 76 | |||
| 77 | sub create_uri { | ||
| 78 | my ($self, $path) = @_; | ||
| 79 | my $url = 'https://'.$self->firebase.'.firebaseio.com/'.$path.'.json'; | ||
| 80 | $url .= '?auth='.$self->authobj->create_token if $self->has_authobj || $self->has_auth; | ||
| 81 | return URI->new($url); | ||
| 82 | } | ||
| 83 | |||
| 84 | sub process_request { | ||
| 85 | my $self = shift; | ||
| 86 | $self->process_response($self->agent->request( @_ )); | ||
| 87 | } | ||
| 88 | |||
| 89 | sub process_response { | ||
| 90 | my ($self, $response) = @_; | ||
| 91 | $self->debug($response->header('X-Firebase-Auth-Debug')); | ||
| 92 | if ($response->is_success) { | ||
| 93 | if ($response->decoded_content eq 'null') { | ||
| 94 | return undef; | ||
| 95 | } | ||
| 96 | else { | ||
| 97 | my $result = eval { from_json($response->decoded_content) }; | ||
| 98 | if ($@) { | ||
| 99 | warn $response->decoded_content; | ||
| 100 | ouch 500, 'Server returned unparsable content.';#, { error => $@, content => $response->decoded_content }; | ||
| 101 | } | ||
| 102 | return $result; | ||
| 103 | } | ||
| 104 | } | ||
| 105 | else { | ||
| 106 | 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 0.0400 | ||
| 117 | |||
| 118 | =head1 SYNOPSIS | ||
| 119 | |||
| 120 | use Firebase; | ||
| 121 | |||
| 122 | my $fb = Firebase->new(firebase => 'myfirebase', auth => { secret => 'xxxxxxx', data => { id => '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; |