File Coverage

blib/lib/Facebook/Graph/AccessToken/Response.pm
Criterion Covered Total %
statement 15 21 71.4
branch 0 2 0.0
condition n/a
subroutine 5 6 83.3
pod n/a
total 20 29 68.9


line stmt bran cond sub pod time code
1             package Facebook::Graph::AccessToken::Response;
2             $Facebook::Graph::AccessToken::Response::VERSION = '1.1203';
3 4     4   24 use Moo;
  4         8  
  4         19  
4 4     4   2219 use URI;
  4         10992  
  4         103  
5 4     4   1655 use URI::QueryParam;
  4         2339  
  4         106  
6 4     4   1409 use Ouch;
  4         6256  
  4         231  
7 4     4   30 use JSON;
  4         10  
  4         27  
8              
9             has response => (
10             is => 'ro',
11             required=> 1,
12             );
13              
14             has token => (
15             is => 'ro',
16             lazy => 1,
17             default => sub {
18             my $self = shift;
19             my $response = $self->response;
20             if ($response->is_success) {
21             if ($response->content =~ m/\A { .+ } \z/xm) {
22             # From v2.3 they return JSON object.
23             return JSON->new->decode($response->content)->{access_token};
24             }
25             else {
26             return URI->new('?'.$response->content)->query_param('access_token');
27             }
28             }
29             else {
30             ouch $response->code, 'Could not fetch access token: '._retrieve_error_message($response), $response->request->uri->as_string;
31             }
32             }
33             );
34              
35             has expires => (
36             is => 'ro',
37             lazy => 1,
38             default => sub {
39             my $self = shift;
40             my $response = $self->response;
41             if ($response->is_success) {
42             if ($response->content =~ m/\A { .+ } \z/xm) {
43             # From v2.3 they return JSON object.
44             return JSON->new->decode($response->content)->{expires_in};
45             }
46             else {
47             return URI->new('?'.$response->content)->query_param('expires');
48             }
49             }
50             else {
51             ouch $response->code, 'Could not fetch access token: '._retrieve_error_message($response), $response->request->uri->as_string;
52             }
53             }
54             );
55              
56             sub _retrieve_error_message {
57 0     0     my $response = shift;
58 0           my $content = eval { from_json($response->decoded_content) };
  0            
59 0 0         if ($@) {
60 0           return $response->message;
61             }
62             else {
63 0           return $content->{error}{message};
64             }
65             }
66              
67             1;
68              
69             =head1 NAME
70              
71             Facebook::Graph::AccessToken::Response - The Facebook access token request response.
72              
73             =head1 VERSION
74              
75             version 1.1203
76              
77             =head1 Description
78              
79             You'll be given one of these as a result of calling the C<request> method from a L<Facebook::Graph::AccessToken> object.
80              
81             =head1 METHODS
82              
83             =head2 token ()
84              
85             Returns the token string.
86              
87             =head2 expires ()
88              
89             Returns the time allotted to this token. If undefined then the token is forever.
90              
91             =head2 response ()
92              
93             Direct access to the L<HTTP::Response> object.
94              
95             =head1 LEGAL
96              
97             Facebook::Graph is Copyright 2010 - 2012 Plain Black Corporation (L<http://www.plainblack.com>) and is licensed under the same terms as Perl itself.
98              
99             =cut