Graph/BatchRequests.pm

filter: Statementstmt 33.3% 13 / 39 Branchbran 0.0% 0 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 42.8% 3 / 7 Podpod 100% 2 / 2 totaltotal 31.0% 18 / 58 CC 7 SCAR 26.2
CC
7
Cov
27%
request
30.0
__ANON__
6.0
add_request
6.0
CRAP 13.8
« Graph/Publish/Checkin.pm ↑ Summary Graph.pm »
1 package Facebook::Graph::BatchRequests;
2 4 $Facebook::Graph::BatchRequests::VERSION = '1.1205';
3 4 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use Ouch;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Facebook::Graph::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 has access_token => (
8 is => 'ro',
9 required => 1,
10 );
11  
12 has requests => (
13 is => 'rw',
14 0 ▶ isa => sub { ouch(442,"$_[0] is not an Array Reference") unless ref $_[0] eq 'ARRAY' },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "ARRAY" 0 0 0
15 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
16 4 );
17  
18 0 ▶sub add_request {
Subroutine add_request: not called CC=2 SCAR=17.9
Pod documented
19 0 my ($self, $ele) = @_;
20  
21 0 ▶ unless (ref $ele eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $ele eq "HASH") 0 0 0
22 0 $ele = { method => 'GET', relative_url => $ele };
23 }
24 0 $self->requests( [ @{$self->requests}, $ele ] );
25  
26 0 return $self; # chained
27 }
28  
29 0 ▶sub request {
Subroutine request: not called CC=5 SCAR=34.0
Pod documented
30 0 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 ▶ unless ($resp->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($resp->is_success) 0 0 0
46 0 my $message = $resp->message;
47 0 my $error = eval { $json->decode($resp->content) };
48 0 ▶ unless ($@) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 0 0 0
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;
56 0 ▶ return wantarray ? @$data : $data;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
57 }
58  
59 4 1;
60  
61 =head1 NAME
62  
63 Facebook::Graph::BatchRequests - Batch Requests
64  
65 =head1 VERSION
66  
67 version 1.1205
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 - 2017 Plain Black Corporation (L<http://www.plainblack.com>) and is licensed under the same terms as Perl itself.
123  
124 =cut
« Graph/Publish/Checkin.pm ↑ Summary Graph.pm »