File Coverage

blib/lib/Facebook/Graph/Response.pm
Criterion Covered Total %
statement 9 9 100.0
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 12 12 100.0


line stmt bran cond sub pod time code
1             package Facebook::Graph::Response;
2             $Facebook::Graph::Response::VERSION = '1.1203';
3 4     4   23 use Moo;
  4         8  
  4         37  
4 4     4   1411 use JSON;
  4         9  
  4         34  
5 4     4   478 use Ouch;
  4         19  
  4         1328  
6              
7             has response => (
8             is => 'rw',
9             isa => sub {ouch(442,"$_[0] is not an HTTP::Response object") unless ref $_[0] eq 'HTTP::Response'},
10             required=> 1,
11             );
12              
13             has as_string => (
14             is => 'ro',
15             lazy => 1,
16             default => sub {
17             my $self = shift;
18             return $self->response->content;
19             },
20             );
21              
22             has as_json => (
23             is => 'ro',
24             lazy => 1,
25             default => sub {
26             my $self = shift;
27             my $response = $self->response;
28             if ($response->is_success) {
29             return $response->content;
30             }
31             else {
32             my $message = $response->message;
33             my $error = eval { JSON->new->decode($response->content) };
34             unless ($@) {
35             $message = $error->{error}{type} . ' - ' . $error->{error}{message};
36             }
37             ouch $response->code, 'Could not execute request ('.$response->request->uri->as_string.'): '.$message, $response->request->uri->as_string;
38             }
39             },
40             );
41              
42             has as_hashref => (
43             is => 'ro',
44             lazy => 1,
45             default => sub {
46             my $self = shift;
47             return JSON->new->decode($self->as_json);
48             },
49             );
50              
51             1;
52              
53             =head1 NAME
54              
55             Facebook::Graph::Response - Handling of a Facebook::Graph response documents.
56              
57             =head1 VERSION
58              
59             version 1.1203
60              
61             =head1 DESCRIPTION
62              
63             You'll be given one of these as a result of calling the C<request> method on a L<Facebook::Graph::Query> or others, or C<publish> on any of the L<Facebook::Graph::Publish> modules.
64              
65             =head1 METHODS
66              
67             Returns the response as a string. Does not throw an exception of any kind.
68              
69             =head2 as_json ()
70              
71             Returns the response from Facebook as a JSON string.
72              
73             =head2 as_hashref ()
74              
75             Returns the response from Facebook as a hash reference.
76              
77             =head2 as_string ()
78              
79             No processing what so ever. Just returns the raw body string that was received from Facebook.
80              
81             =head2 response ()
82              
83             Direct access to the L<HTTP::Response> object.
84              
85             =head1 LEGAL
86              
87             Facebook::Graph is Copyright 2010 - 2012 Plain Black Corporation (L<http://www.plainblack.com>) and is licensed under the same terms as Perl itself.
88              
89             =cut