Eve/HttpResource/Graph.pm

filter: Statementstmt 13.8% 13 / 94 Branchbran 0.0% 0 / 30 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 17.3% 4 / 23 Podpod 100% 1 / 1 totaltotal 12.1% 18 / 148 CC 16 SCAR 31.8
CC
16
Cov
10%
_process_exceptions
72.0
_build_response
42.0
_post
12.0
CRAP 23.9
« Eve/HttpDispatcher.pm ↑ Summary Eve/HttpResource.pm »
1 package Eve::HttpResource::Graph;
2  
3 1 ▶use parent qw(Eve::HttpResource);
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Eve::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 B<Eve::HttpResource::Graph> - a base class for the Graph API
13 node HTTP resources.
14  
15 =head1 SYNOPSIS
16  
17 package Eve::HttpResource::SomeGraphResource;
18  
19 use parent qw(Eve::HttpResource::Graph);
20  
21 sub _read {
22 # some object's hash is returned here
23 }
24  
25 sub _get_connections {
26 # some connections hash is returned here
27 }
28  
29 sub _get_fields {
30 # some object fields hash is returned here
31 }
32  
33 sub _get_type {
34 # a required type string is returned here
35 }
36  
37 1;
38  
39 =head1 DESCRIPTION
40  
41 B<Eve::HttpResource::Graph> is an HTTP resource based class
42 providing an automation of the Facebook like Graph API node/connection
43 functionality.
44  
45 Methods C<_read()>, C<_publish()>, C<_remove()>,
46 C<_get_connections()>, C<_get_fields()>, C<_get_type()>,
47 C<_get_actions()> and C<_get_id_alias_hash()> could be overriden by
48 the class derivatives. When not overriden these methods except
49 C<_get_connections()> and C<_get_fields> throw the exception
50 C<Eve::Exception::Http::405MethodNotAllowed>. C<_get_type> throws a
51 C<Eve::Error::NotImplemented> exception which means it must be
52 overridden to be used. Inside the described above methods class
53 attributes C<_request>, C<_response>, C<_session>, C<_event_map> and
54 C<_dispatcher> can be found.
55  
56 All methods are passed the list of named matched URI parameters as
57 arguments. The following example illustrates the usage of named
58 arguments in a resource that is bound to the
59 C<http://example.com/:named/:another> pattern URI:
60  
61 my ($self, %arg_hash) = @_;
62 Eve::Support::arguments(\%arg_hash, my ($named, $another));
63  
64 If the C<metadata> query string parameter is specified then the
65 C<metadata> section is added to the result. This section always
66 contains the object's type returned by the C<_get_type> method, and if
67 present, the object's connections returned by the
68 C<_get_connections()> method, the object's fields returned by the
69 C<_get_fields> method and the object's actions returned by the
70 C<_get_actions()> method.
71  
72 If the request method is C<POST> and the query string is supplied with
73 C<method=delete> then it behaves just like the C<DELETE> method is
74 requested. If other value is passed to the C<method> then the
75 exception C<Eve::Exception::Http::400BadRequest> is thrown.
76  
77 If C<Eve::Exception::Privilege> is thrown in the user code the
78 exception C<Eve::Exception::Http::403Forbidden> is rethrown.
79  
80 Note that the resource must be bound with the C<id> placeholder. The
81 C<_id> attribute is representing it. If C<_get_id_alias_hash()> is
82 redefined you can use ID aliases in URI according to the hash keys.
83  
84 Object deletion requests must use the deleted object's node URI. In
85 case a connection with no public ID is deleted, the request must use
86 the respective object connection URI.
87  
88 =head3 Constructor arguments
89  
90 =over 4
91  
92 =item C<request>
93  
94 an HTTP request object
95  
96 =item C<response>
97  
98 an HTTP response object
99  
100 =item C<session_constructor>
101  
102 a reference to a subroutine accepting the session C<id> argument and
103 returning a session object
104  
105 =item C<dispatcher>
106  
107 an HTTP dispatcher object
108  
109 =item C<json>
110  
111 a JSON encoder object.
112  
113 =back
114  
115 =head1 METHODS
116  
117 =head2 B<init()>
118  
119 =cut
120  
121 0 ▶sub init {
Subroutine init: not called CC=1 SCAR=6.9
Pod documented
122 0 my ($self, %arg_hash) = @_;
123 0 my $arg_hash = Eve::Support::arguments(\%arg_hash, my $json);
124  
125 0 $self->{'_json'} = $json;
126  
127 0 $self->{'_id'} = undef;
128  
129 0 $self->SUPER::init(%{$arg_hash});
130  
131 0 return;
132 }
133  
134 =head2 B<_read()>
135  
136 This method is called when the graph node or connection is requested
137 with the GET method.
138  
139 =head3 Returns
140  
141 It is expected to return a data hash reference
142 that will be automatically converted to a required textual
143 representation and returned to the client
144  
145 =head3 Throws
146  
147 =over 4
148  
149 =item C<Eve::Exception::Http::405MethodNotAllowed>
150  
151 When a method is used without being overridden in a descendant class.
152  
153 =back
154  
155 =cut
156  
157 0 ▶sub _read {
Subroutine _read: not called CC=1 SCAR=6.9
158 0 Eve::Exception::Http::405MethodNotAllowed->throw(
159 message => 'Method is not implemented.');
160  
161 0 return;
162 }
163  
164 =head2 B<_publish()>
165  
166 This method is called when the graph node or connection is requested
167 with the POST method.
168  
169 =head3 Returns
170  
171 It is expected to return a data hash reference
172 that will be automatically converted to a required textual
173 representation and returned to the client
174  
175 =head3 Throws
176  
177 =over 4
178  
179 =item C<Eve::Exception::Http::405MethodNotAllowed>
180  
181 When a method is used without being overridden in a descendant class.
182  
183 =back
184  
185 =cut
186  
187 0 ▶sub _publish {
Subroutine _publish: not called CC=1 SCAR=6.9
188 0 Eve::Exception::Http::405MethodNotAllowed->throw(
189 message => 'Method is not implemented.');
190  
191 0 return;
192 }
193  
194 =head2 B<_remove()>
195  
196 This method is called when the graph node or connection is requested
197 with the DELETE method or when the POST method is used in conjunction
198 with a C<method=delete> query string parameter..
199  
200 =head3 Returns
201  
202 It is expected to return a data hash reference
203 that will be automatically converted to a required textual
204 representation and returned to the client
205  
206 =head3 Throws
207  
208 =over 4
209  
210 =item C<Eve::Exception::Http::405MethodNotAllowed>
211  
212 When a method is used without being overridden in a descendant class.
213  
214 =back
215  
216 =cut
217  
218 0 ▶sub _remove {
Subroutine _remove: not called CC=1 SCAR=6.9
219 0 Eve::Exception::Http::405MethodNotAllowed->throw(
220 message => 'Method is not implemented.');
221  
222 0 return;
223 }
224  
225 =head2 B<_get_connections()>
226  
227 This method is called when the C<metadata> parameter is recieved in
228 the request. Overriding this method is optional.
229  
230 =head3 Returns
231  
232 It is expected to return a connection hash reference
233 that will be automatically converted to a required textual
234 representation and returned to the client.
235  
236 =cut
237  
238 0 ▶sub _get_connections {
Subroutine _get_connections: not called CC=1 SCAR=6.9
239 0 return (undef);
240 }
241  
242 =head2 B<_get_fields()>
243  
244 This method is called when the C<metadata> parameter is recieved in
245 the request. Overriding this method is optional.
246  
247 =head3 Returns
248  
249 It is expected to return a fields hash reference that will be
250 automatically converted to a required textual representation and
251 returned to the client.
252  
253 =cut
254  
255 0 ▶sub _get_fields {
Subroutine _get_fields: not called CC=1 SCAR=6.9
256 0 return (undef);
257 }
258  
259 =head2 B<_get_actions()>
260  
261 This method is called when the C<metadata> parameter is recieved in
262 the request. Overriding this method is optional.
263  
264 =head3 Returns
265  
266 It is expected to return an actions hash reference with action keys as
267 keys and action names as values that will be automatically converted
268 to a required textual representation and returned to the client.
269  
270 =cut
271  
272 0 ▶sub _get_actions {
Subroutine _get_actions: not called CC=1 SCAR=6.9
273 0 return (undef);
274 }
275  
276 =head2 B<_get_type()>
277  
278 This method is called when the C<metadata> parameter is recieved in
279 the request.
280  
281 =head3 Returns
282  
283 It is expected to return a type string that will be automatically
284 converted to a required textual representation and returned to the
285 client.
286  
287 =head3 Throws
288  
289 =over 4
290  
291 =item C<Eve::Error::NotImplemented>
292  
293 When a method is used without being overridden in a descendant class.
294  
295 =back
296  
297 =cut
298  
299 0 ▶sub _get_type {
Subroutine _get_type: not called CC=1 SCAR=6.9
300 0 Eve::Error::NotImplemented->throw(
301 message => 'The _get_type method must be implemented.');
302  
303 0 return;
304 }
305  
306 =head2 B<_get_alias_hash()>
307  
308 This method is called when the graph HTTP resource processes the
309 pattern placeholder matches from a request URI. A hash of aliases for
310 an id can be specified in this method. For example, if an id in the
311 URI is specified as an C<alias> keyword, it can be replaced with a
312 real identifier by returning this hash reference:
313  
314 return {'alias' => $some_service->get_parameter(name => 'id')};
315  
316 =head3 Returns
317  
318 It is expected to return a reference to a hash of aliases for an
319 identifier.
320  
321 =head3 Throws
322  
323 =over 4
324  
325 =item C<Eve::Error::NotImplemented>
326  
327 When a method is used without being overridden in a descendant class.
328  
329 =back
330  
331 =cut
332  
333 0 ▶sub _get_id_alias_hash {
Subroutine _get_id_alias_hash: not called CC=1 SCAR=6.9
334 0 return {};
335 }
336  
337 0 ▶sub _get {
Subroutine _get: not called CC=1 SCAR=6.9
338 0 my ($self, %matches_hash) = @_;
339  
340 0 eval {
341 $self->_build_response(
342 0 ▶ result_callback => sub { $self->_read(%matches_hash); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
343 0 matches_hash => \%matches_hash);
344 };
345 0 $self->_process_exceptions();
346  
347 0 return;
348 }
349  
350 0 ▶sub _post {
Subroutine _post: not called CC=3 SCAR=24.8
351 0 my ($self, %matches_hash) = @_;
352  
353 0 my $method = $self->_request->get_uri()->
354 get_query_parameter(name => 'method');
355  
356 0 eval {
357 0 ▶ if (defined $method) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $method) { } 0 0 0
358 0 ▶ if ($method eq 'delete') {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq 'delete') { } 0 0 0
359 0 $self->_delete(%matches_hash);
360 } else {
361 0 Eve::Exception::Http::400BadRequest->throw(
362 message => 'Unsupported pseudo method "'.$method.
363 '" for POST.');
364 }
365 } else {
366 $self->_build_response(
367 0 ▶ result_callback => sub { $self->_publish(%matches_hash); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
368 0 matches_hash => \%matches_hash);
369 }
370 };
371  
372 0 $self->_process_exceptions();
373  
374 0 return;
375 }
376  
377 0 ▶sub _delete {
Subroutine _delete: not called CC=1 SCAR=6.9
378 0 my ($self, %matches_hash) = @_;
379  
380 0 eval {
381 $self->_build_response(
382 0 ▶ result_callback => sub { $self->_remove(%matches_hash); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
383 0 matches_hash => \%matches_hash);
384 };
385  
386 0 $self->_process_exceptions();
387  
388 0 return;
389 }
390  
391 0 ▶sub _get_metadata {
Subroutine _get_metadata: not called CC=2 SCAR=17.9
392 0 my ($self, %matches_hash) = @_;
393  
394 0 my $connections = $self->_get_connections(%matches_hash);
395  
396 0 ▶ my $metadata = {
Branch: 1 branch
BranchTrueFalseTotal
defined $connections ? : 0 0 0
397 defined $connections ? ('connections' => $connections) : ()};
398  
399 0 return $metadata;
400 }
401  
402 0 ▶sub _build_response {
Subroutine _build_response: not called CC=6 SCAR=37.4
403 0 my ($self, %arg_hash) = @_;
404 0 Eve::Support::arguments(
405 \%arg_hash, my ($result_callback, $matches_hash));
406  
407 0 ▶ if (not exists $matches_hash->{'id'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $matches_hash->{'id'}) { } 0 0 0
408 0 Eve::Error::Value->throw(
409 message => 'No identifier has been matched in the URI.');
410 } else {
411 0 ▶ if (exists $self->_get_id_alias_hash()->{$matches_hash->{'id'}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->_get_id_alias_hash->{$matches_hash->{'id'}}) { } 0 0 0
412 0 $self->_id = $self->_get_id_alias_hash()->{$matches_hash->{'id'}};
413 } else {
414 0 $self->_id = $matches_hash->{'id'};
415  
416 0 ▶ if ($self->_id =~ /\D/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_id =~ /\D/) 0 0 0
417 0 Eve::Exception::Http::400BadRequest->throw(
418 message => 'The identifier must be a number or an allowed '.
419 'alias, got "'.$self->_id.'".');
420 }
421 }
422 };
423 0 my $result = $result_callback->();
424  
425 0 my $metadata;
426 0 ▶ if ($self->_request->get_uri()->get_query_parameter(name => 'metadata')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_request->get_uri->get_query_parameter("name", "metadata")) 0 0 0
427 0 $metadata = $self->_get_metadata(%{$matches_hash});
428 }
429  
430 $self->_set_response(
431 code => 200,
432 reference => Eve::Support::indexed_hash(
433 0 ▶ %{$result},
Branch: 1 branch
BranchTrueFalseTotal
defined $metadata ? : 0 0 0
434 defined $metadata ? ('metadata' => $metadata) : ()));
435  
436 0 return;
437 }
438  
439 0 ▶sub _process_exceptions {
Subroutine _process_exceptions: not called CC=8 SCAR=42.8
440 0 my $self = shift;
441  
442 0 my ($e, $code, $type);
443 0 ▶ if ($e = Eve::Exception::Http::400BadRequest->caught()) {
Branch: 6 branches
BranchTrueFalseTotal
if ($e = 'Eve::Exception::Http::400BadRequest'->caught) { } 0 0 0
elsif ($e = 'Eve::Exception::Http::401Unauthorized'->caught) { } 0 0 0
elsif ($e = 'Eve::Exception::Http::405MethodNotAllowed'->caught) { } 0 0 0
elsif ($e = 'Eve::Exception::Data'->caught) { } 0 0 0
elsif ($e = 'Eve::Exception::Privilege'->caught) { } 0 0 0
elsif ($e = 'Exception::Class::Base'->caught) { } 0 0 0
444 0 ($code, $type) = (400, 'Request');
445 } elsif ($e = Eve::Exception::Http::401Unauthorized->caught()) {
446 0 ($code, $type) = (401, 'Authorization');
447 } elsif ($e = Eve::Exception::Http::405MethodNotAllowed->caught()) {
448 0 ($code, $type) = (405, 'Request');
449 } elsif ($e = Eve::Exception::Data->caught()) {
450 0 ($code, $type) = (400, 'Data');
451 } elsif ($e = Eve::Exception::Privilege->caught()) {
452 #print STDERR "HttpResource::Graph::process_exceptions: caught privilege exception!\n";
453 0 ($code, $type) = (403, 'Privilege');
454 } elsif ($e = Exception::Class::Base->caught()) {
455 0 $e->rethrow();
456 }
457  
458 0 ▶ if (defined $e) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $e) 0 0 0
459 0 $self->_set_response(
460 code => $code,
461 reference => {
462 'error' => Eve::Support::indexed_hash(
463 'type' => $type,
464 'message' => $e->message)});
465 }
466  
467 0 return;
468 }
469  
470 0 ▶sub _set_response {
Subroutine _set_response: not called CC=1 SCAR=6.9
471 0 my ($self, %arg_hash) = @_;
472 0 Eve::Support::arguments(\%arg_hash, my ($code, $reference));
473  
474 0 $self->_response->set_status(code => $code);
475 0 $self->_response->set_header(
476 name => 'Content-Type', value => 'text/javascript');
477 0 $self->_response->set_body(
478 text => $self->_json->encode(reference => $reference));
479  
480 0 return;
481 }
482  
483 =head1 SEE ALSO
484  
485 =over 4
486  
487 =item L<Eve::HttpResource>
488  
489 =item L<Eve::Exception>
490  
491 =back
492  
493 =head1 LICENSE AND COPYRIGHT
494  
495 Copyright 2012 Igor Zinovyev.
496  
497 This program is free software; you can redistribute it and/or modify it
498 under the terms of either: the GNU General Public License as published
499 by the Free Software Foundation; or the Artistic License.
500  
501 See http://dev.perl.org/licenses/ for more information.
502  
503  
504 =head1 AUTHOR
505  
506 =over 4
507  
508 =item L<Sergey Konoplev|mailto:gray.ru@gmail.com>
509  
510 =back
511  
512 =cut
513  
514 1 1;
« Eve/HttpDispatcher.pm ↑ Summary Eve/HttpResource.pm »