source: perl/modules/Facebook/lib/Facebook/Graph/Response.pm @ 2a42248

release-1.10release-1.9
Last change on this file since 2a42248 was 2a42248, checked in by Edward Z. Yang <ezyang@mit.edu>, 13 years ago
Add vanilla Facebook::Graph. Signed-off-by: Edward Z. Yang <ezyang@mit.edu>
  • Property mode set to 100644
File size: 2.0 KB
Line 
1package Facebook::Graph::Response;
2BEGIN {
3  $Facebook::Graph::Response::VERSION = '1.0300';
4}
5
6use Any::Moose;
7use JSON;
8use Ouch;
9
10has response => (
11    is      => 'ro',
12    required=> 1,
13);
14
15has as_string => (
16    is      => 'ro',
17    lazy    => 1,
18    default => sub {
19        my $self = shift;
20        return $self->response->content;
21    },
22);
23
24has as_json => (
25    is      => 'ro',
26    lazy    => 1,
27    default => sub {
28        my $self = shift;
29        my $response = $self->response;
30        if ($response->is_success) {
31            return $response->content;
32        }
33        else {
34            my $message = $response->message;
35            my $error = eval { JSON->new->decode($response->content) };
36            unless ($@) {
37                $message = $error->{error}{type} . ' - ' . $error->{error}{message};
38            }
39            ouch $response->code, 'Could not execute request ('.$response->request->uri->as_string.'): '.$message, $response->request->uri->as_string;
40        }
41    },
42);
43
44has as_hashref => (
45    is      => 'ro',
46    lazy    => 1,
47    default => sub {
48        my $self = shift;
49        return JSON->new->decode($self->as_json);
50    },
51);
52
53no Any::Moose;
54__PACKAGE__->meta->make_immutable;
55
56=head1 NAME
57
58Facebook::Graph::Response - Handling of a Facebook::Graph response documents.
59
60=head1 VERSION
61
62version 1.0300
63
64=head1 DESCRIPTION
65
66You'll be given one of these as a result of calling the C<request> method on a C<Facebook::Graph::Query> or others.
67
68
69=head1 METHODS
70
71Returns the response as a string. Does not throw an exception of any kind.
72
73=head2 as_json ()
74
75Returns the response from Facebook as a JSON string.
76
77=head2 as_hashref ()
78
79Returns the response from Facebook as a hash reference.
80
81=head2 as_string ()
82
83No processing what so ever. Just returns the raw body string that was received from Facebook.
84
85=head2 response ()
86
87Direct access to the L<HTTP::Response> object.
88
89=head1 LEGAL
90
91Facebook::Graph is Copyright 2010 Plain Black Corporation (L<http://www.plainblack.com>) and is licensed under the same terms as Perl itself.
92
93=cut
Note: See TracBrowser for help on using the repository browser.