Last change
on this file since cfca761 was
cfca761,
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 | |
---|
1 | package Facebook::Graph::Response; |
---|
2 | BEGIN { |
---|
3 | $Facebook::Graph::Response::VERSION = '1.0300'; |
---|
4 | } |
---|
5 | |
---|
6 | use Any::Moose; |
---|
7 | use JSON; |
---|
8 | use Ouch; |
---|
9 | |
---|
10 | has response => ( |
---|
11 | is => 'ro', |
---|
12 | required=> 1, |
---|
13 | ); |
---|
14 | |
---|
15 | has as_string => ( |
---|
16 | is => 'ro', |
---|
17 | lazy => 1, |
---|
18 | default => sub { |
---|
19 | my $self = shift; |
---|
20 | return $self->response->content; |
---|
21 | }, |
---|
22 | ); |
---|
23 | |
---|
24 | has 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 | |
---|
44 | has 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 | |
---|
53 | no Any::Moose; |
---|
54 | __PACKAGE__->meta->make_immutable; |
---|
55 | |
---|
56 | =head1 NAME |
---|
57 | |
---|
58 | Facebook::Graph::Response - Handling of a Facebook::Graph response documents. |
---|
59 | |
---|
60 | =head1 VERSION |
---|
61 | |
---|
62 | version 1.0300 |
---|
63 | |
---|
64 | =head1 DESCRIPTION |
---|
65 | |
---|
66 | You'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 | |
---|
71 | Returns the response as a string. Does not throw an exception of any kind. |
---|
72 | |
---|
73 | =head2 as_json () |
---|
74 | |
---|
75 | Returns the response from Facebook as a JSON string. |
---|
76 | |
---|
77 | =head2 as_hashref () |
---|
78 | |
---|
79 | Returns the response from Facebook as a hash reference. |
---|
80 | |
---|
81 | =head2 as_string () |
---|
82 | |
---|
83 | No processing what so ever. Just returns the raw body string that was received from Facebook. |
---|
84 | |
---|
85 | =head2 response () |
---|
86 | |
---|
87 | Direct access to the L<HTTP::Response> object. |
---|
88 | |
---|
89 | =head1 LEGAL |
---|
90 | |
---|
91 | Facebook::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.