source: perl/modules/Facebook/lib/Facebook/Graph/Response.pm @ 9820d55

Last change on this file since 9820d55 was 9820d55, checked in by Edward Z. Yang <ezyang@mit.edu>, 13 years ago
Convert to async (both Facebook::Graph and us.) Work items: - Documentation is all out of date - Think more carefully about error handling (right now it's delayed into the response object) - Really bad HTTP POST hack in Publish.pm. Signed-off-by: Edward Z. Yang <ezyang@mit.edu>
  • Property mode set to 100644
File size: 2.2 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 headers => (
16    is      => 'ro',
17    required=> 1,
18);
19
20has uri => (
21    is      => 'ro',
22    required=> 1,
23);
24
25has as_string => (
26    is      => 'ro',
27    lazy    => 1,
28    default => sub {
29        my $self = shift;
30        if (!defined $self->response) {
31            ouch $self->headers->{Status}, $self->headers->{Reason}, $self->uri;
32        }
33        if ($self->headers->{Status} < 200 || $self->headers->{Status} >= 300) {
34            my $type = $self->headers->{Status};
35            my $message = $self->response;
36            my $error = eval { JSON->new->decode($self->response) };
37            unless ($@) {
38                $type = $error->{error}{type};
39                $message = $error->{error}{message};
40            }
41            ouch $type, 'Could not execute request ('.$self->uri.'): '.$message, $self->uri;
42        }
43        return $self->response;
44    },
45);
46
47has as_json => (
48    is      => 'ro',
49    lazy    => 1,
50    default => sub {
51        my $self = shift;
52        return $self->as_string;
53    },
54);
55
56has as_hashref => (
57    is      => 'ro',
58    lazy    => 1,
59    default => sub {
60        my $self = shift;
61        return JSON->new->decode($self->as_json);
62    },
63);
64
65no Any::Moose;
66__PACKAGE__->meta->make_immutable;
67
68=head1 NAME
69
70Facebook::Graph::Response - Handling of a Facebook::Graph response documents.
71
72=head1 VERSION
73
74version 1.0300
75
76=head1 DESCRIPTION
77
78You'll be given one of these as a result of calling the C<request> method on a C<Facebook::Graph::Query> or others.
79
80
81=head1 METHODS
82
83Returns the response as a string. Does not throw an exception of any kind.
84
85=head2 as_json ()
86
87Returns the response from Facebook as a JSON string.
88
89=head2 as_hashref ()
90
91Returns the response from Facebook as a hash reference.
92
93=head2 as_string ()
94
95No processing what so ever. Just returns the raw body string that was received from Facebook.
96
97=head2 response ()
98
99Direct access to the L<HTTP::Response> object.
100
101=head1 LEGAL
102
103Facebook::Graph is Copyright 2010 Plain Black Corporation (L<http://www.plainblack.com>) and is licensed under the same terms as Perl itself.
104
105=cut
Note: See TracBrowser for help on using the repository browser.