blib/lib/Facebook/OpenGraph.pm

filter: Statementstmt 97.3% 259 / 266 Branchbran 79.3% 92 / 116 Conditioncond 75.4% 43 / 57 MC/DCmcdc 57.5% 19 / 33 Subroutinesub 100% 58 / 58 Podpod 97.6% 42 / 43 totaltotal 89.5% 513 / 573 SCAR 45.4
CC
86
Cov
90%
prep_fields_recursive
19.8
prep_param
15.5
request
15.1
CRAP 94.0
↑ Summary blib/lib/Facebook/OpenGraph/Response.pm »
1 package Facebook::OpenGraph;
2 33 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 33 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 33 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 33 ▶use Facebook::OpenGraph::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 33 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 33 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 33 ▶use Furl::HTTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 33 ▶use Data::Recursive::Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 33 ▶use JSON 2 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 33 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 33 ▶use Digest::SHA qw(hmac_sha256 hmac_sha256_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 33 ▶use MIME::Base64::URLSafe qw(urlsafe_b64decode);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 33 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 33 our $VERSION = '1.30';
18  
19 sub new {
20 66 ▶ my $class = shift;
Subroutine new: called CC=9 SCAR=22.2
Pod documented
21 66 ▶ my $args = shift || +{};
Condition100%2/2
shift() || {}
l!l
4125
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
22  
23 return bless +{
24 app_id => $args->{app_id},
25 secret => $args->{secret},
26 namespace => $args->{namespace},
27 access_token => $args->{access_token},
28 redirect_uri => $args->{redirect_uri},
29 batch_limit => $args->{batch_limit} || 50,
30 is_beta => $args->{is_beta} || 0,
31 json => $args->{json} || JSON->new->utf8,
32 use_appsecret_proof => $args->{use_appsecret_proof} || 0,
33 use_post_method => $args->{use_post_method} || 0,
34 version => $args->{version} || undef,
35 66 ▶ ua => $args->{ua} || Furl::HTTP->new(
Condition81%13/16
$args->{'batch_limit'} || 50
l!l
264
$args->{'is_beta'} || 0
l!l
462
$args->{'json'} || 'JSON'->new->utf8
l!l&&r!l&&!r
0660
$args->{'use_appsecret_proof'} || 0
l!l
363
$args->{'use_post_method'} || 0
l!l
363
$args->{'version'} || undef
l!l
363
$args->{'ua'} || 'Furl::HTTP'->new('capture_request', 1, 'agent', 'Facebook::OpenGraph/' . $Facebook::OpenGraph::VERSION)
l!l&&r!l&&!r
1650
MC/DC100%1/1
$args->{'batch_limit'} || 50
$args->{'batch_limit'}
MC/DC100%1/1
$args->{'is_beta'} || 0
$args->{'is_beta'}
MC/DC0%0/2
$args->{'json'} || 'JSON'->new->utf8
$args->{'json'} 'JSON'->new->utf8
MC/DC100%1/1
$args->{'use_appsecret_proof'} || 0
$args->{'use_appsecret_proof'}
MC/DC100%1/1
$args->{'use_post_method'} || 0
$args->{'use_post_method'}
MC/DC100%1/1
$args->{'version'} || undef
$args->{'version'}
MC/DC0%0/2
$args->{'ua'} || 'Furl::HTTP'->new('capture_request', 1, 'agent', 'Facebook::OpenGraph/' . $Facebook::OpenGraph::VERSION)
$args->{'ua'} 'Furl::HTTP'->new('capture_request', 1, 'agent', 'Facebook::OpenGraph/' . $Facebook::OpenGraph::VERSION)
Truth table100%2/2
A
A= $args->{'batch_limit'}
Aresult
11
00
Truth table100%2/2
A
A= $args->{'is_beta'}
Aresult
11
00
Truth table33%1/3
A || B
A= $args->{'json'}B= 'JSON'->new->utf8
ABresult
1X1
011
000
Truth table100%2/2
A
A= $args->{'use_appsecret_proof'}
Aresult
11
00
Truth table100%2/2
A
A= $args->{'use_post_method'}
Aresult
11
00
Truth table100%2/2
A
A= $args->{'version'}
Aresult
11
00
Truth table66%2/3
A || B
A= $args->{'ua'}B= 'Furl::HTTP'->new('capture_request', 1, 'agent', 'Facebook::OpenGraph/' . $Facebook::OpenGraph::VERSION)
ABresult
1X1
011
000
36 capture_request => 1,
37 agent => __PACKAGE__ . '/' . $VERSION,
38 ),
39 }, $class;
40 }
41  
42 # accessors
43 33 ▶sub app_id { shift->{app_id} }
Subroutine app_id: called CC=1 SCAR=0.0
Pod documented
44 29 ▶sub secret { shift->{secret} }
Subroutine secret: called CC=1 SCAR=0.0
Pod documented
45 42 ▶sub ua { shift->{ua} }
Subroutine ua: called CC=1 SCAR=0.0
Pod documented
46 4 ▶sub namespace { shift->{namespace} }
Subroutine namespace: called CC=1 SCAR=0.0
Pod documented
47 86 ▶sub access_token { shift->{access_token} }
Subroutine access_token: called CC=1 SCAR=0.0
Pod documented
48 16 ▶sub redirect_uri { shift->{redirect_uri} }
Subroutine redirect_uri: called CC=1 SCAR=0.0
Pod documented
49 17 ▶sub batch_limit { shift->{batch_limit} }
Subroutine batch_limit: called CC=1 SCAR=0.0
Pod documented
50 61 ▶sub is_beta { shift->{is_beta} }
Subroutine is_beta: called CC=1 SCAR=0.0
Pod documented
51 68 ▶sub json { shift->{json} }
Subroutine json: called CC=1 SCAR=0.0
Pod documented
52 42 ▶sub use_appsecret_proof { shift->{use_appsecret_proof} }
Subroutine use_appsecret_proof: called CC=1 SCAR=0.0
Pod documented
53 2 ▶sub use_post_method { shift->{use_post_method} }
Subroutine use_post_method: called CC=1 SCAR=0.0
Pod documented
54 63 ▶sub version { shift->{version} }
Subroutine version: called CC=1 SCAR=0.0
Pod documented
55  
56 sub uri {
57 46 ▶ my $self = shift;
Subroutine uri: called CC=2 SCAR=6.9
Pod documented
58  
59 46 ▶ my $base = $self->is_beta ? 'https://graph.beta.facebook.com/'
Branch: 1 branch
BranchTrueFalseTotal
$self->is_beta ? : 3 43 46
60 : 'https://graph.facebook.com/'
61 ;
62  
63 46 return $self->_uri($base, @_);
64 }
65  
66 sub video_uri {
67 9 ▶ my $self = shift;
Subroutine video_uri: called CC=2 SCAR=6.9
Pod documented
68  
69 9 ▶ my $base = $self->is_beta ? 'https://graph-video.beta.facebook.com/'
Branch: 1 branch
BranchTrueFalseTotal
$self->is_beta ? : 3 6 9
70 : 'https://graph-video.facebook.com/'
71 ;
72  
73 9 return $self->_uri($base, @_);
74 }
75  
76 sub site_uri {
77 4 ▶ my $self = shift;
Subroutine site_uri: called CC=2 SCAR=6.9
Pod documented
78  
79 4 ▶ my $base = $self->is_beta ? 'https://www.beta.facebook.com/'
Branch: 1 branch
BranchTrueFalseTotal
$self->is_beta ? : 1 3 4
80 : 'https://www.facebook.com/'
81 ;
82  
83 4 return $self->_uri($base, @_);
84 }
85  
86 sub _uri {
87 59 ▶ my ($self, $base, $path, $param_ref) = @_;
Subroutine _uri: called CC=3 SCAR=11.0
88 59 ▶ my $uri = URI->new_abs($path || '/', $base);
Condition100%2/2
$path || '/'
l!l
4613
MC/DC100%1/1
$path || '/'
$path
Truth table100%2/2
A
A= $path
Aresult
11
00
89 $uri->query_form(+{
90 $uri->query_form, # when given $path is like /foo?bar=bazz
91 59 ▶ %{ $param_ref || +{} }, # additional query parameter
Branch: 1 branch
BranchTrueFalseTotal
$param_ref or {} 51 8 59
92 });
93  
94 59 return $uri;
95 }
96  
97 # Login for Games on Facebook > Checking Login Status > Parsing the Signed Request
98 # https://developers.facebook.com/docs/facebook-login/using-login-with-games
99 sub parse_signed_request {
100 5 ▶ my ($self, $signed_request) = @_;
Subroutine parse_signed_request: called CC=5 SCAR=16.3
Pod documented
101 5 ▶ croak 'signed_request is not given' unless $signed_request;
Branch: 1 branch
BranchTrueFalseTotal
unless $signed_request 0 5 5
102 5 ▶ croak 'secret key must be set' unless $self->secret;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->secret 1 4 5
103  
104 # "1. Split the signed request into two parts delineated by a '.' character
105 # (eg. 238fsdfsd.oijdoifjsidf899)"
106 4 my ($enc_sig, $payload) = split(m{ \. }xms, $signed_request);
107  
108 # "2. Decode the first part - the encoded signature - from base64url"
109 4 my $sig = urlsafe_b64decode($enc_sig);
110  
111 # "3. Decode the second part - the 'payload' - from base64url and then
112 # decode the resultant JSON object"
113 4 my $val = $self->json->decode(urlsafe_b64decode($payload));
114  
115 # "It specifically uses HMAC-SHA256 encoding, which you can again use with
116 # most programming languages."
117 croak 'algorithm must be HMAC-SHA256'
118 4 ▶ unless uc( $val->{algorithm} ) eq 'HMAC-SHA256';
Branch: 1 branch
BranchTrueFalseTotal
unless uc $val->{"algorithm"} eq "HMAC-SHA256" 0 4 4
119  
120 # "You can compare this encoded signature with an expected signature using
121 # the payload you received as well as the app secret which is known only to
122 # your and ensure that they match."
123 4 my $expected_sig = hmac_sha256($payload, $self->secret);
124 4 ▶ croak 'Signature does not match' unless $sig eq $expected_sig;
Branch: 1 branch
BranchTrueFalseTotal
unless $sig eq $expected_sig 0 4 4
125  
126 4 return $val;
127 }
128  
129 # Detailed flow is described here.
130 # Manually Build a Login Flow > Logging people in > Invoking the login dialog
131 # https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow/
132 #
133 # Parameters for login dialog are shown here.
134 # Login Dialog > Parameters
135 # https://developers.facebook.com/docs/reference/dialogs/oauth/
136 sub auth_uri {
137 6 ▶ my ($self, $param_ref) = @_;
Subroutine auth_uri: called CC=8 SCAR=20.9
Pod documented
138 6 ▶ $param_ref ||= +{};
Condition100%2/2
$param_ref ||= {}
l!l
51
MC/DC100%1/1
$param_ref ||= {}
$param_ref
Truth table100%2/2
A
A= $param_ref
Aresult
11
00
139 6 ▶ croak 'redirect_uri and app_id must be set'
Branch: 1 branch
BranchTrueFalseTotal
unless $self->redirect_uri and $self->app_id 1 5 6
Condition66%2/3
$self->redirect_uri and $self->app_id
!ll&&!rl&&r
105
MC/DC50%1/2
$self->redirect_uri and $self->app_id
$self->redirect_uri $self->app_id
Truth table66%2/3
A and B
A= $self->redirect_uriB= $self->app_id
ABresult
0X0
100
111
140 unless $self->redirect_uri && $self->app_id;
141  
142 # "A comma separated list of permission names which you would like people
143 # to grant your app."
144 5 ▶ if (my $scope_ref = ref $param_ref->{scope}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $scope_ref = ref $param_ref->{"scope"}) 4 1 5
145 4 ▶ croak 'scope must be string or array ref' unless $scope_ref eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless $scope_ref eq "ARRAY" 1 3 4
146 3 $param_ref->{scope} = join q{,}, @{ $param_ref->{scope} };
147 }
148  
149 # "The URL to redirect to after a button is clicked or tapped in the
150 # dialog."
151 4 $param_ref->{redirect_uri} = $self->redirect_uri;
152  
153 # "Your App ID. This is called client_id instead of app_id for this
154 # particular method in order to be compliant with the OAuth 2.0
155 # specification."
156 4 $param_ref->{client_id} = $self->app_id;
157  
158 # "If you are using the URL redirect dialog implementation, then this will
159 # be a full page display, shown within Facebook.com. This display type is
160 # called page."
161 4 ▶ $param_ref->{display} ||= 'page';
Condition50%1/2
$param_ref->{"display"} ||= "page"
l!l
04
MC/DC0%0/1
$param_ref->{"display"} ||= "page"
$param_ref->{"display"}
Truth table50%1/2
A
A= $param_ref->{"display"}
Aresult
11
00
162  
163 # "Response data is included as URL parameters and contains code parameter
164 # (an encrypted string unique to each login request). This is the default
165 # behaviour if this parameter is not specified."
166 4 ▶ $param_ref->{response_type} ||= 'code';
Condition50%1/2
$param_ref->{"response_type"} ||= "code"
l!l
04
MC/DC0%0/1
$param_ref->{"response_type"} ||= "code"
$param_ref->{"response_type"}
Truth table50%1/2
A
A= $param_ref->{"response_type"}
Aresult
11
00
167  
168 4 my $uri = $self->site_uri('/dialog/oauth', $param_ref);
169  
170 # Platform Versioning > Making Versioned Requests > Dialogs.
171 # https://developers.facebook.com/docs/apps/versions#dialogs
172 4 $uri->path( $self->gen_versioned_path($uri->path) );
173  
174 4 return $uri->as_string;
175 }
176  
177 sub set_access_token {
178 4 ▶ my ($self, $token) = @_;
Subroutine set_access_token: called CC=1 SCAR=0.0
Pod documented
179 4 $self->{access_token} = $token;
180 }
181  
182 # Access Tokens > App Tokens
183 # https://developers.facebook.com/docs/facebook-login/access-tokens/#apptokens
184 sub get_app_token {
185 3 ▶ my $self = shift;
Subroutine get_app_token: called CC=1 SCAR=0.0
Pod documented
186  
187 # Document does not mention what grant_type is all about or what values can
188 # be set, but RFC 6749 covers the basic idea of grant types and its Section
189 # 4.4 describes Client Credentials Grant.
190 # http://tools.ietf.org/html/rfc6749#section-4.4
191 3 return $self->_get_token(+{grant_type => 'client_credentials'});
192 }
193  
194 # Manually Build a Login Flow > Confirming identity > Exchanging code for an access token
195 # https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow
196 sub get_user_token_by_code {
197 2 ▶ my ($self, $code) = @_;
Subroutine get_user_token_by_code: called CC=3 SCAR=11.3
Pod documented
198  
199 2 ▶ croak 'code is not given' unless $code;
Branch: 1 branch
BranchTrueFalseTotal
unless $code 0 2 2
200 2 ▶ croak 'redirect_uri must be set' unless $self->redirect_uri;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->redirect_uri 0 2 2
201  
202 2 my $query_ref = +{
203 redirect_uri => $self->redirect_uri,
204 code => $code,
205 };
206 2 return $self->_get_token($query_ref);
207 }
208  
209 sub get_user_token_by_cookie {
210 4 ▶ my ($self, $cookie_value) = @_;
Subroutine get_user_token_by_cookie: called CC=3 SCAR=11.0
Pod documented
211  
212 4 ▶ croak 'cookie value is not given' unless $cookie_value;
Branch: 1 branch
BranchTrueFalseTotal
unless $cookie_value 1 3 4
213  
214 3 my $parsed_signed_request = $self->parse_signed_request($cookie_value);
215  
216 # https://github.com/oklahomer/p5-Facebook-OpenGraph/issues/1#issuecomment-41065480
217 # parsed content should be something like below.
218 # {
219 # algorithm => "HMAC-SHA256",
220 # issued_at => 1398180151,
221 # code => "SOME_OPAQUE_STRING",
222 # user_id => 44007581,
223 # };
224 croak q{"code" is not contained in cookie value: } . $cookie_value
225 3 ▶ unless $parsed_signed_request->{code};
Branch: 1 branch
BranchTrueFalseTotal
unless $parsed_signed_request->{"code"} 1 2 3
226  
227 # Redirect_uri MUST be empty string in this case.
228 # That's why I didn't use get_user_token_by_code().
229 my $query_ref = +{
230 code => $parsed_signed_request->{code},
231 2 redirect_uri => '',
232 };
233 2 return $self->_get_token($query_ref);
234 }
235  
236 # Access Tokens > Expiration and Extending Tokens
237 # https://developers.facebook.com/docs/facebook-login/access-tokens/
238 sub exchange_token {
239 3 ▶ my ($self, $short_term_token) = @_;
Subroutine exchange_token: called CC=2 SCAR=7.0
Pod documented
240  
241 3 ▶ croak 'short term token is not given' unless $short_term_token;
Branch: 1 branch
BranchTrueFalseTotal
unless $short_term_token 0 3 3
242  
243 3 my $query_ref = +{
244 grant_type => 'fb_exchange_token',
245 fb_exchange_token => $short_term_token,
246 };
247 3 return $self->_get_token($query_ref);
248 }
249  
250 sub _get_token {
251 10 ▶ my ($self, $param_ref) = @_;
Subroutine _get_token: called CC=5 SCAR=16.1
252  
253 10 ▶ croak 'app_id and secret must be set' unless $self->app_id && $self->secret;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->app_id and $self->secret 4 6 10
Condition100%3/3
$self->app_id and $self->secret
!ll&&!rl&&r
226
MC/DC100%2/2
$self->app_id and $self->secret
$self->app_id $self->secret
Truth table100%3/3
A and B
A= $self->app_idB= $self->secret
ABresult
0X0
100
111
254  
255 6 $param_ref = +{
256 %$param_ref,
257 client_id => $self->app_id,
258 client_secret => $self->secret,
259 };
260  
261 6 my $response = $self->request('GET', '/oauth/access_token', $param_ref);
262 # Document describes as follows:
263 # "The response you will receive from this endpoint, if successful, is
264 # access_token={access-token}&expires={seconds-til-expiration}
265 # If it is not successful, you'll receive an explanatory error message."
266 #
267 # It, however, returnes no "expires" parameter on some edge cases.
268 # e.g. Your app requests manage_pages permission.
269 # https://developers.facebook.com/bugs/597779113651383/
270 6 ▶ if ($response->is_api_version_eq_or_later_than('v2.3')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_api_version_eq_or_later_than("v2.3")) 1 5 6
271 # As of v2.3, to be compliant with RFC 6749, response is JSON formatted
272 # as described below.
273 # {"access_token": <TOKEN>, "token_type":<TYPE>, "expires_in":<TIME>}
274 # https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow/v2.3#confirm
275 1 return $response->as_hashref;
276 }
277  
278 5 my $res_content = $response->content;
279 5 my $token_ref = +{ URI->new("?$res_content")->query_form };
280 croak "can't get access_token properly: $res_content"
281 5 ▶ unless $token_ref->{access_token};
Branch: 1 branch
BranchTrueFalseTotal
unless $token_ref->{"access_token"} 0 5 5
282  
283 5 return $token_ref;
284 }
285  
286 sub get {
287 10 ▶ return shift->request('GET', @_)->as_hashref;
Subroutine get: called CC=1 SCAR=0.0
Pod documented
288 }
289  
290 sub post {
291 21 ▶ return shift->request('POST', @_)->as_hashref;
Subroutine post: called CC=1 SCAR=0.0
Pod documented
292 }
293  
294 # Deleting > Objects
295 # https://developers.facebook.com/docs/reference/api/deleting/
296 sub delete {
297 1 ▶ return shift->request('DELETE', @_)->as_hashref;
Subroutine delete: called CC=1 SCAR=0.0
Pod documented
298 }
299  
300 # For those who got used to Facebook::Graph
301 33 *fetch = \&get;
302 33 *publish = \&post;
303  
304 # Using ETags
305 # https://developers.facebook.com/docs/reference/ads-api/etags-reference/
306 sub fetch_with_etag {
307 2 ▶ my ($self, $uri, $param_ref, $etag) = @_;
Subroutine fetch_with_etag: called CC=2 SCAR=6.9
Pod documented
308  
309 # Attach ETag value to header
310 # Returns status 304 without contnet, or status 200 with modified content
311 2 my $header = ['IF-None-Match' => $etag];
312 2 my $response = $self->request('GET', $uri, $param_ref, $header);
313  
314 2 ▶ return $response->is_modified ? $response->as_hashref : undef;
Branch: 1 branch
BranchTrueFalseTotal
$response->is_modified ? : 1 1 2
315 }
316  
317 sub bulk_fetch {
318 1 ▶ my ($self, $paths_ref) = @_;
Subroutine bulk_fetch: called CC=1 SCAR=0.0
Pod documented
319  
320 my @queries = map {
321 1 +{
322 2 method => 'GET',
323 relative_url => $_,
324 }
325 } @$paths_ref;
326  
327 1 return $self->batch(\@queries);
328 }
329  
330 # Making Multiple API Requests > Making a simple batched request
331 # https://developers.facebook.com/docs/graph-api/making-multiple-requests
332 sub batch {
333 6 ▶ my $self = shift;
Subroutine batch: called CC=4 SCAR=13.9
Pod documented
334  
335 6 my $responses_ref = $self->batch_fast(@_);
336  
337 # Devide response content and create response objects that correspond to
338 # each request
339 5 my @data = ();
340 5 for my $r (@$responses_ref) {
341 7 for my $res_ref (@$r) {
342 my $response = $self->create_response(
343 $res_ref->{code},
344 $res_ref->{message},
345 58 [ map { $_->{name} => $_->{value} } @{ $res_ref->{headers} } ],
346 $res_ref->{body},
347 13 );
348 13 ▶ croak $response->error_string unless $response->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $response->is_success 0 13 13
349  
350 13 push @data, $response->as_hashref;
351 }
352 }
353  
354 5 return \@data;
355 }
356  
357 # doesn't create F::OG::Response object for each response
358 sub batch_fast {
359 6 ▶ my $self = shift;
Subroutine batch_fast: called CC=6 SCAR=17.9
Pod documented
360 6 my $batch = shift;
361  
362 # Other than HTTP header, you need to set access_token as top level
363 # parameter. You can specify individual token for each request so you can
364 # act as several other users and pages.
365 6 ▶ croak 'Top level access_token must be set' unless $self->access_token;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->access_token 1 5 6
366  
367 # "We currently limit the number of requests which can be in a batch to 50"
368 5 my @responses = ();
369 5 ▶ while(my @queries = splice @$batch, 0, $self->batch_limit) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@queries) = splice(@$batch, 0, $self->batch_limit)) 7 5 12
370  
371 7 for my $q (@queries) {
372 13 ▶ if ($q->{method} eq 'POST' && $q->{body}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($q->{"method"} eq "POST" and $q->{"body"}) 3 10 13
Condition66%2/3
$q->{"method"} eq "POST" and $q->{"body"}
!ll&&!rl&&r
1003
MC/DC50%1/2
$q->{"method"} eq "POST" and $q->{"body"}
$q->{"method"} eq "POST" $q->{"body"}
Truth table66%2/3
A and B
A= $q->{"method"} eq "POST"B= $q->{"body"}
ABresult
0X0
100
111
373 3 my $body_ref = $self->prep_param($q->{body});
374 3 my $uri = URI->new;
375 3 $uri->query_form(%$body_ref);
376 3 $q->{body} = $uri->query;
377 }
378 }
379  
380 7 my @req = (
381 '/',
382 +{
383 access_token => $self->access_token,
384 batch => $self->json->encode(\@queries),
385 },
386 @_,
387 );
388  
389 7 push @responses, $self->post(@req);
390 }
391  
392 5 return \@responses;
393 }
394  
395 # Facebook Query Language (FQL) Overview
396 # https://developers.facebook.com/docs/technical-guides/fql/
397 sub fql {
398 2 ▶ my $self = shift;
Subroutine fql: called CC=1 SCAR=0.0
Pod documented
399 2 my $query = shift;
400 2 return $self->get('/fql', +{q => $query}, @_);
401 }
402  
403 # Facebook Query Language (FQL) Overview: Multi-query
404 # https://developers.facebook.com/docs/technical-guides/fql/#multi
405 sub bulk_fql {
406 1 ▶ my $self = shift;
Subroutine bulk_fql: called CC=1 SCAR=0.0
Pod documented
407 1 my $batch = shift;
408 1 return $self->fql($self->json->encode($batch), @_);
409 }
410  
411 sub request {
412 40 ▶ my ($self, $method, $uri, $param_ref, $headers) = @_;
Subroutine request: called CC=15 SCAR=27.1
Pod documented
413  
414 40 $method = uc $method;
415 40 ▶ $uri = $self->uri($uri) unless blessed($uri) && $uri->isa('URI');
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($uri) and $uri->isa("URI") 40 0 40
Condition33%1/3
builtin::blessed($uri) and $uri->isa("URI")
!ll&&!rl&&r
4000
MC/DC0%0/2
builtin::blessed($uri) and $uri->isa("URI")
builtin::blessed($uri) $uri->isa("URI")
Truth table33%1/3
A and B
A= builtin::blessed($uri)B= $uri->isa("URI")
ABresult
0X0
100
111
416 40 $uri->path( $self->gen_versioned_path($uri->path) );
417 $param_ref = $self->prep_param(+{
418 $uri->query_form(+{}),
419 40 ▶ %{$param_ref || +{}},
Branch: 1 branch
BranchTrueFalseTotal
$param_ref or {} 9 31 40
420 });
421  
422 # Securing Graph API Requests > Verifying Graph API Calls with appsecret_proof
423 # https://developers.facebook.com/docs/graph-api/securing-requests/
424 40 ▶ if ($self->use_appsecret_proof) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->use_appsecret_proof) 1 39 40
425 1 $param_ref->{appsecret_proof} = $self->gen_appsecret_proof;
426 }
427  
428 # Use POST as default HTTP method and add method=(POST|GET|DELETE) to query
429 # parameter. Document only says we can send HTTP DELETE method or, instead,
430 # HTTP POST method with ?method=delete to delete object. It does not say
431 # POST with method=(get|post) parameter works, but PHP SDK always sends POST
432 # with method parameter so... I just give you this option.
433 # Check PHP SDK's base_facebook.php for detail.
434 40 ▶ if ($self->{use_post_method}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"use_post_method"}) 2 38 40
435 2 $param_ref->{method} = $method;
436 2 $method = 'POST';
437 }
438  
439 40 ▶ $headers ||= [];
Condition100%2/2
$headers ||= []
l!l
238
MC/DC100%1/1
$headers ||= []
$headers
Truth table100%2/2
A
A= $headers
Aresult
11
00
440  
441 # Document says we can pass access_token as a part of query parameter,
442 # but it actually supports Authorization header to be compliant with the
443 # OAuth 2.0 spec.
444 # http://tools.ietf.org/html/rfc6749#section-7
445 40 ▶ if ($self->access_token) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->access_token) 19 21 40
446 19 push @$headers, (
447 'Authorization',
448 sprintf('OAuth %s', $self->access_token),
449 );
450 }
451  
452 40 my $content = q{};
453 40 ▶ if ($method eq 'POST') {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq 'POST') { } 23 17 40
454 23 ▶ if ($param_ref->{source} || $param_ref->{file} || $param_ref->{upload_phase} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param_ref->{'source'} or $param_ref->{'file'} or $param_ref->{'upload_phase'}) { } 5 18 23
Condition100%6/6
$param_ref->{"source"} or $param_ref->{"file"} or $param_ref->{"upload_phase"}
l!l&&r!l&&!r
3218
$param_ref->{"source"} or $param_ref->{"file"}
l!l&&r!l&&!r
2120
MC/DC100%3/3
$param_ref->{"source"} or $param_ref->{"file"} or $param_ref->{"upload_phase"}
$param_ref->{"source"} $param_ref->{"file"} $param_ref->{"upload_phase"}
Truth table100%4/4
A or B or C
A= $param_ref->{"source"}B= $param_ref->{"file"}C= $param_ref->{"upload_phase"}
ABCresult
1XX1
01X1
0011
0000
455 # post image or video file
456  
457 # https://developers.facebook.com/docs/reference/api/video/
458 # When posting a video, use graph-video.facebook.com .
459 # base_facebook.php has an equivalent part in isVideoPost().
460 # ($method == 'POST' && preg_match("/^(\/)(.+)(\/)(videos)$/", $path))
461 # For other actions, use graph.facebook.com/VIDEO_ID/CONNECTION_TYPE
462 5 ▶ if ($uri->path =~ m{\A /.+/videos \z}xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri->path =~ m[\A /.+/videos \z]msx) 3 2 5
463 3 $uri->host($self->video_uri->host);
464 }
465  
466 # Content-Type should be multipart/form-data
467 # https://developers.facebook.com/docs/reference/api/publishing/
468 5 push @$headers, (Content_Type => 'form-data');
469  
470 # Furl::HTTP document says we can use multipart/form-data with
471 # HTTP::Request::Common.
472 5 my $req = POST $uri, @$headers, Content => [%$param_ref];
473 5 $content = $req->content;
474 5 my $req_header = $req->headers;
475 $headers = +[
476 map {
477 5 my $k = $_;
478 15 map { ( $k => $_ ) } $req_header->header($k);
479 } $req_header->header_field_names
480 ];
481 }
482 else {
483 # Post simple parameters such as message, link, description, etc...
484 # Content-Type: application/x-www-form-urlencoded will be set in
485 # Furl::HTTP, and $content will be treated properly.
486 18 $content = $param_ref;
487 }
488 }
489 else {
490 17 $uri->query_form($param_ref);
491 }
492  
493 40 my ($res_minor_version, @res_elms) = $self->ua->request(
494 method => $method,
495 url => $uri,
496 headers => $headers,
497 content => $content,
498 );
499  
500 40 my $res = $self->create_response(@res_elms);
501  
502 # return F::OG::Response object on success
503 40 ▶ return $res if $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
if $res->is_success 36 4 40
504  
505 # Use later version of Furl::HTTP to utilize req_headers and req_content.
506 # This Should be helpful when debugging.
507 4 my $msg = $res->error_string;
508 4 ▶ if ($res->req_headers) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->req_headers) 0 4 4
509 0 $msg .= "\n" . $res->req_headers . $res->req_content;
510 }
511 4 croak $msg;
512 }
513  
514 # Securing Graph API Requests > Verifying Graph API Calls with appsecret_proof > Generating the proof
515 # https://developers.facebook.com/docs/graph-api/securing-requests/
516 sub gen_appsecret_proof {
517 2 ▶ my $self = shift;
Subroutine gen_appsecret_proof: called CC=3 SCAR=11.4
Pod documented
518 2 ▶ croak 'app secret must be set' unless $self->secret;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->secret 0 2 2
519 2 ▶ croak 'access_token must be set' unless $self->access_token;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->access_token 0 2 2
520 2 return hmac_sha256_hex($self->access_token, $self->secret);
521 }
522  
523 # Platform Versioning > Making Versioned Requests
524 # https://developers.facebook.com/docs/apps/versions
525 sub gen_versioned_path {
526 56 ▶ my ($self, $path) = @_;
Subroutine gen_versioned_path: called CC=4 SCAR=13.9
Pod undocumented
527  
528 56 ▶ $path = '/' unless $path;
Branch: 1 branch
BranchTrueFalseTotal
unless $path 4 52 56
529  
530 56 ▶ if ($self->version && $path !~ m{\A /v(?:\d+)\.(?:\d+)/ }x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->version and not $path =~ m[\A /v(?:\d+)\.(?:\d+)/ ]x) 5 51 56
Condition100%3/3
$self->version and not $path =~ m[\A /v(?:\d+)\.(?:\d+)/ ]x
!ll&&!rl&&r
4925
MC/DC100%2/2
$self->version and not $path =~ m[\A /v(?:\d+)\.(?:\d+)/ ]x
$self->version not $path =~ m[\A /v(?:\d+)\.(?:\d+)/ ]x
Truth table100%3/3
A and not(B)
A= $self->versionB= not $path =~ m[\A /v(?:\d+)\.(?:\d+)/ ]x
ABresult
0X0
100
111
531 # If default platform version is set on initialisation
532 # and given path doesn't contain version,
533 # then prepend the default version.
534 5 $path = sprintf('/%s%s', $self->version, $path);
535 }
536  
537 56 return $path;
538 }
539  
540 sub js_cookie_name {
541 2 ▶ my $self = shift;
Subroutine js_cookie_name: called CC=2 SCAR=6.9
Pod documented
542 2 ▶ croak 'app_id must be set' unless $self->app_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->app_id 1 1 2
543  
544 # Cookie is set by JS SDK with a name of fbsr_{app_id}. Official document
545 # is not provided for more than 3 yaers so I quote from PHP SDK's code.
546 # "Constructs and returns the name of the cookie that potentially houses
547 # the signed request for the app user. The cookie is not set by the
548 # BaseFacebook class, but it may be set by the JavaScript SDK."
549 # The cookie value can be parsed as signed request and it contains 'code'
550 # to exchange for access toekn.
551 1 return sprintf('fbsr_%d', $self->app_id);
552 }
553  
554 sub create_response {
555 53 ▶ my $self = shift;
Subroutine create_response: called CC=1 SCAR=0.0
Pod documented
556 return Facebook::OpenGraph::Response->new(+{
557 json => $self->json,
558 map {
559 53 $_ => shift
560 } qw/code message headers content req_headers req_content/,
561 });
562 }
563  
564 sub prep_param {
565 44 ▶ my ($self, $param_ref) = @_;
Subroutine prep_param: called CC=15 SCAR=27.4
Pod documented
566  
567 44 ▶ $param_ref = Data::Recursive::Encode->encode_utf8($param_ref || +{});
Condition50%1/2
$param_ref || {}
l!l
440
MC/DC0%0/1
$param_ref || {}
$param_ref
Truth table50%1/2
A
A= $param_ref
Aresult
11
00
568  
569 # /?ids=4,http://facebook-docs.oklahome.net
570 44 ▶ if (my $ids = $param_ref->{ids}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ids = $param_ref->{"ids"}) 1 43 44
571 1 ▶ $param_ref->{ids} = ref $ids ? join q{,}, @$ids : $ids;
Branch: 1 branch
BranchTrueFalseTotal
ref $ids ? : 1 0 1
572 }
573  
574 # mostly for /APP_ID/accounts/test-users
575 44 ▶ if (my $perms = $param_ref->{permissions}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $perms = $param_ref->{"permissions"}) 5 39 44
576 5 ▶ $param_ref->{permissions} = ref $perms ? join q{,}, @$perms : $perms;
Branch: 1 branch
BranchTrueFalseTotal
ref $perms ? : 4 1 5
577 }
578  
579 # Source, file and video_file_chunk parameter contains file path.
580 # It must be an array ref to work with HTTP::Request::Common.
581 44 ▶ if (my $path = $param_ref->{source}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $path = $param_ref->{"source"}) 3 41 44
582 3 ▶ $param_ref->{source} = ref $path ? $path : [$path];
Branch: 1 branch
BranchTrueFalseTotal
ref $path ? : 0 3 3
583 }
584 44 ▶ if (my $path = $param_ref->{file}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $path = $param_ref->{"file"}) 1 43 44
585 1 ▶ $param_ref->{file} = ref $path ? $path : [$path];
Branch: 1 branch
BranchTrueFalseTotal
ref $path ? : 0 1 1
586 }
587 44 ▶ if (my $path = $param_ref->{video_file_chunk}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $path = $param_ref->{"video_file_chunk"}) 1 43 44
588 1 ▶ $param_ref->{video_file_chunk} = ref $path ? $path : [$path];
Branch: 1 branch
BranchTrueFalseTotal
ref $path ? : 0 1 1
589 }
590  
591 # use Field Expansion
592 44 ▶ if (my $field_ref = $param_ref->{fields}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $field_ref = $param_ref->{"fields"}) 2 42 44
593 2 $param_ref->{fields} = $self->prep_fields_recursive($field_ref);
594 }
595  
596 # Using Objects: Using the Object API
597 # https://developers.facebook.com/docs/opengraph/using-objects/#objectapi
598 44 my $object = $param_ref->{object};
599 44 ▶ if ($object && ref $object eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($object and ref $object eq "HASH") 1 43 44
Condition66%2/3
$object and ref $object eq "HASH"
!ll&&!rl&&r
4301
MC/DC50%1/2
$object and ref $object eq "HASH"
$object ref $object eq "HASH"
Truth table66%2/3
A and B
A= $objectB= ref $object eq "HASH"
ABresult
0X0
100
111
600 1 $param_ref->{object} = $self->json->encode($object);
601 }
602  
603 44 return $param_ref;
604 }
605  
606 # Using the Graph API: Reading > Choosing Fields > Making Nested Requests
607 # https://developers.facebook.com/docs/graph-api/using-graph-api/
608 sub prep_fields_recursive {
609 4 ▶ my ($self, $val) = @_;
Subroutine prep_fields_recursive: called CC=7 SCAR=29.9
Pod documented
610  
611 4 my $ref = ref $val;
612 4 ▶ if (!$ref) {
Branch: 3 branches
BranchTrueFalseTotal
if (not $ref) { } 3 1 4
elsif ($ref eq 'ARRAY') { } 1 0 1
elsif ($ref eq 'HASH') { } 0 0 0
613 3 return $val;
614 }
615 elsif ($ref eq 'ARRAY') {
616 1 return join q{,}, map { $self->prep_fields_recursive($_) } @$val;
617 }
618 elsif ($ref eq 'HASH') {
619 0 my @strs = ();
620 0 ▶ while (my ($k, $v) = each %$val) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$val) 0 0 0
621 0 my $r = ref $v;
622 0 ▶ my $pattern = $r && $r eq 'HASH' ? '%s.%s' : '%s(%s)';
Branch: 1 branch
BranchTrueFalseTotal
$r && $r eq 'HASH' ? : 0 0 0
Condition0%0/3
$r and $r eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
$r and $r eq "HASH"
$r $r eq "HASH"
Truth table0%0/3
A and B
A= $rB= $r eq "HASH"
ABresult
0X0
100
111
623 0 push @strs, sprintf($pattern, $k, $self->prep_fields_recursive($v));
624 }
625 0 return join q{.}, @strs;
626 }
627 }
628  
629 # Using Actions > Publishing Actions
630 # https://developers.facebook.com/docs/opengraph/using-actions/#publish
631 sub publish_action {
632 1 ▶ my $self = shift;
Subroutine publish_action: called CC=2 SCAR=7.0
Pod documented
633 1 my $action = shift;
634 1 ▶ croak 'namespace is not set' unless $self->namespace;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->namespace 0 1 1
635 1 return $self->post(sprintf('/me/%s:%s', $self->namespace, $action), @_);
636 }
637  
638 # Using Objects > Using the Object API > Images with the Object API
639 # https://developers.facebook.com/docs/opengraph/using-objects/
640 sub publish_staging_resource {
641 1 ▶ my $self = shift;
Subroutine publish_staging_resource: called CC=1 SCAR=0.0
Pod documented
642 1 my $file = shift;
643 1 return $self->post('/me/staging_resources', +{file => $file}, @_);
644 }
645  
646 # Test Users: Creating
647 # https://developers.facebook.com/docs/test_users/
648 sub create_test_users {
649 2 ▶ my $self = shift;
Subroutine create_test_users: called CC=3 SCAR=11.0
Pod documented
650 2 my $settings_ref = shift;
651  
652 2 ▶ if (ref $settings_ref ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $settings_ref ne "ARRAY") 1 1 2
653 1 $settings_ref = [$settings_ref];
654 }
655  
656 2 my @settings = ();
657 2 my $relative_url = sprintf('/%s/accounts/test-users', $self->app_id);
658 2 for my $setting (@$settings_ref) {
659 3 push @settings, +{
660 method => 'POST',
661 relative_url => $relative_url,
662 body => $setting,
663 };
664 }
665  
666 2 return $self->batch(\@settings);
667 }
668  
669 # Using Objects > Using Self-Hosted Objects > Updating Objects
670 # https://developers.facebook.com/docs/opengraph/using-objects/
671 sub check_object {
672 6 ▶ my ($self, $target) = @_;
Subroutine check_object: called CC=1 SCAR=0.0
Pod documented
673 6 my $param_ref = +{
674 id => $target, # $target is object url or open graph object id
675 scrape => 'true',
676 };
677 6 return $self->post(q{}, $param_ref);
678 }
679  
680 33 1;
681 __END__
↑ Summary blib/lib/Facebook/OpenGraph/Response.pm »