File Coverage

blib/lib/Facebook/Graph/BatchRequests.pm
Criterion Covered Total %
statement 9 33 27.2
branch 0 8 0.0
condition n/a
subroutine 3 5 60.0
pod 2 2 100.0
total 14 48 29.1


line stmt bran cond sub pod time code
1             package Facebook::Graph::BatchRequests;
2             $Facebook::Graph::BatchRequests::VERSION = '1.1204';
3 4     4   24 use Moo;
  4         9  
  4         21  
4 4     4   1129 use Ouch;
  4         8  
  4         250  
5 4     4   23 use Facebook::Graph::Request;
  4         12  
  4         1351  
6              
7             has access_token => (
8             is => 'ro',
9             required => 1,
10             );
11              
12             has requests => (
13             is => 'rw',
14             isa => sub { ouch(442,"$_[0] is not an Array Reference") unless ref $_[0] eq 'ARRAY' },
15             default => sub { [] },
16             );
17              
18             sub add_request {
19 0     0 1   my ($self, $ele) = @_;
20              
21 0 0         unless (ref $ele eq 'HASH') {
22 0           $ele = { method => 'GET', relative_url => $ele };
23             }
24 0           $self->requests( [ @{$self->requests}, $ele ] );
  0            
25              
26 0           return $self; # chained
27             }
28              
29             sub request {
30 0     0 1   my ($self, @reqs) = @_;
31              
32 0           $self->add_request($_) foreach @reqs;
33              
34 0           my $json = JSON->new;
35 0           my $post = {
36             access_token => $self->access_token, # required
37             batch => $json->encode($self->requests),
38             };
39              
40 0           $self->requests([]); # reset
41              
42 0           my $uri = "https://graph.facebook.com";
43 0           my $resp = Facebook::Graph::Request->new->post($uri, $post)->response;
44              
45 0 0         unless ($resp->is_success) {
46 0           my $message = $resp->message;
47 0           my $error = eval { $json->decode($resp->content) };
  0            
48 0 0         unless ($@) {
49 0           $message = $error->{error}{type} . ' - ' . $error->{error}{message};
50             }
51 0           ouch $resp->code, "Could not execute batch requests: $message";
52             }
53              
54 0           my $data = $json->decode($resp->decoded_content);
55 0           map { $_->{data} = $json->decode($_->{body}) } @$data;
  0            
56 0 0         return wantarray ? @$data : $data;
57             }
58              
59             1;
60              
61             =head1 NAME
62              
63             Facebook::Graph::BatchRequests - Batch Requests
64              
65             =head1 VERSION
66              
67             version 1.1204
68              
69             =head1 SYNOPSIS
70              
71             # set access_token, required
72             my $fb = Facebook::Graph->new(access_token => $access_token);
73             my @batches = $fb->batch_requests
74             ->add_request('sarahbownds')
75             ->add_request({"method" => "POST", "relative_url" => 'me/feed', body => "message=Test update"})
76             ->request;
77              
78             foreach my $batch (@batches) {
79             print $batch->{code} . $batch->{body} . Dumper(\$batch->{data}, \$batch->{headers}) . "\n";
80             }
81              
82             =head1 DESCRIPTION
83              
84             send batch requests to save time: L<http://developers.facebook.com/docs/reference/api/batch/>
85              
86             =head1 METHODS
87              
88             =head2 new ( [ params ] )
89              
90             =over
91              
92             =item params
93              
94             A hash or hashref of parameters to pass to the constructor.
95              
96             =over
97              
98             =item access_token
99              
100             An access token string used to make Facebook requests as a privileged user.
101              
102             =back
103              
104             =back
105              
106             =head2 add_request
107              
108             add request, if not HASHREF, will default method as GET and arg as relative_url
109              
110             $batch_requests->add_request('sarahbownds'); # as { method => 'GET', relative_url => 'sarahbownds' }
111             $batch_requests->add_request({"method" => "POST", "relative_url" => 'me/feed', body => "message=Test update"})
112              
113             =head2 request
114              
115             $batch_requests->request;
116             $batch_requests->request(@requests);
117              
118             Fire the request and return decoded @batches data
119              
120             =head1 LEGAL
121              
122             Facebook::Graph is Copyright 2010 - 2012 Plain Black Corporation (L<http://www.plainblack.com>) and is licensed under the same terms as Perl itself.
123              
124             =cut