OpenGraph.pm

filter: Statementstmt 99.6% 262 / 263 Branchbran 84.6% 88 / 104 Conditioncond 82.8% 53 / 64 MC/DCmcdc 66.6% 24 / 36 Subroutinesub 100% 58 / 58 Podpod 97.6% 42 / 43 totaltotal 92.7% 527 / 568 CC 84 SCAR 17.7
CC
84
Cov
94%
request
16.1
prep_param
12.2
new
9.2
CRAP 5.9
↑ Summary 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.31';
18  
19 70 ▶sub new {
Subroutine new: called CC=9 SCAR=22.2
Pod documented
20 70 my $class = shift;
21 70 ▶ my $args = shift || +{};
Condition100%2/2
shift() || {}
l!l
4327
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 70 ▶ ua => $args->{ua} || Furl::HTTP->new(
Condition81%13/16
$args->{'batch_limit'} || 50
l!l
268
$args->{'is_beta'} || 0
l!l
466
$args->{'json'} || 'JSON'->new->utf8
l!l&&r!l&&!r
0700
$args->{'use_appsecret_proof'} || 0
l!l
367
$args->{'use_post_method'} || 0
l!l
367
$args->{'version'} || undef
l!l
367
$args->{'ua'} || 'Furl::HTTP'->new('capture_request', 1, 'agent', 'Facebook::OpenGraph/' . $Facebook::OpenGraph::VERSION)
l!l&&r!l&&!r
1690
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 45 ▶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 91 ▶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 64 ▶sub is_beta { shift->{is_beta} }
Subroutine is_beta: called CC=1 SCAR=0.0
Pod documented
51 71 ▶sub json { shift->{json} }
Subroutine json: called CC=1 SCAR=0.0
Pod documented
52 45 ▶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 66 ▶sub version { shift->{version} }
Subroutine version: called CC=1 SCAR=0.0
Pod documented
55  
56 49 ▶sub uri {
Subroutine uri: called CC=2 SCAR=6.9
Pod documented
57 49 my $self = shift;
58  
59 49 ▶ my $base = $self->is_beta ? 'https://graph.beta.facebook.com/'
Branch: 1 branch
BranchTrueFalseTotal
$self->is_beta ? : 3 46 49
60 : 'https://graph.facebook.com/'
61 ;
62  
63 49 return $self->_uri($base, @_);
64 }
65  
66 9 ▶sub video_uri {
Subroutine video_uri: called CC=2 SCAR=6.9
Pod documented
67 9 my $self = shift;
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 4 ▶sub site_uri {
Subroutine site_uri: called CC=2 SCAR=6.9
Pod documented
77 4 my $self = shift;
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 62 ▶sub _uri {
Subroutine _uri: called CC=3 SCAR=11.0
87 62 my ($self, $base, $path, $param_ref) = @_;
88 62 ▶ my $uri = URI->new_abs($path || '/', $base);
Condition100%2/2
$path || '/'
l!l
4913
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 62 ▶ %{ $param_ref || +{} }, # additional query parameter
Condition100%2/2
$param_ref || {}
l!l
854
MC/DC100%1/1
$param_ref || {}
$param_ref
Truth table100%2/2
A
A= $param_ref
Aresult
11
00
92 });
93  
94 62 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 5 ▶sub parse_signed_request {
Subroutine parse_signed_request: called CC=5 SCAR=16.3
Pod documented
100 5 my ($self, $signed_request) = @_;
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 6 ▶sub auth_uri {
Subroutine auth_uri: called CC=8 SCAR=20.9
Pod documented
137 6 my ($self, $param_ref) = @_;
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 4 ▶sub set_access_token {
Subroutine set_access_token: called CC=1 SCAR=0.0
Pod documented
178 4 my ($self, $token) = @_;
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 3 ▶sub get_app_token {
Subroutine get_app_token: called CC=1 SCAR=0.0
Pod documented
185 3 my $self = shift;
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 2 ▶sub get_user_token_by_code {
Subroutine get_user_token_by_code: called CC=3 SCAR=11.3
Pod documented
197 2 my ($self, $code) = @_;
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 4 ▶sub get_user_token_by_cookie {
Subroutine get_user_token_by_cookie: called CC=3 SCAR=11.0
Pod documented
210 4 my ($self, $cookie_value) = @_;
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 3 ▶sub exchange_token {
Subroutine exchange_token: called CC=2 SCAR=7.0
Pod documented
239 3 my ($self, $short_term_token) = @_;
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 10 ▶sub _get_token {
Subroutine _get_token: called CC=5 SCAR=16.1
251 10 my ($self, $param_ref) = @_;
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 12 ▶sub get {
Subroutine get: called CC=1 SCAR=0.0
Pod documented
287 12 return shift->request('GET', @_)->as_hashref;
288 }
289  
290 22 ▶sub post {
Subroutine post: called CC=1 SCAR=0.0
Pod documented
291 22 return shift->request('POST', @_)->as_hashref;
292 }
293  
294 # Deleting > Objects
295 # https://developers.facebook.com/docs/reference/api/deleting/
296 1 ▶sub delete {
Subroutine delete: called CC=1 SCAR=0.0
Pod documented
297 1 return shift->request('DELETE', @_)->as_hashref;
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 2 ▶sub fetch_with_etag {
Subroutine fetch_with_etag: called CC=2 SCAR=6.9
Pod documented
307 2 my ($self, $uri, $param_ref, $etag) = @_;
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 1 ▶sub bulk_fetch {
Subroutine bulk_fetch: called CC=1 SCAR=0.0
Pod documented
318 1 my ($self, $paths_ref) = @_;
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 6 ▶sub batch {
Subroutine batch: called CC=4 SCAR=13.9
Pod documented
333 6 my $self = shift;
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 6 ▶sub batch_fast {
Subroutine batch_fast: called CC=6 SCAR=17.9
Pod documented
359 6 my $self = shift;
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 2 ▶sub fql {
Subroutine fql: called CC=1 SCAR=0.0
Pod documented
398 2 my $self = shift;
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 1 ▶sub bulk_fql {
Subroutine bulk_fql: called CC=1 SCAR=0.0
Pod documented
406 1 my $self = shift;
407 1 my $batch = shift;
408 1 return $self->fql($self->json->encode($batch), @_);
409 }
410  
411 43 ▶sub request {
Subroutine request: called CC=16 SCAR=27.8
Pod documented
412 43 my ($self, $method, $uri, $param_ref, $headers) = @_;
413  
414 43 $method = uc $method;
415 43 ▶ $uri = $self->uri($uri) unless blessed($uri) && $uri->isa('URI');
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($uri) and $uri->isa("URI") 43 0 43
Condition33%1/3
builtin::blessed($uri) and $uri->isa("URI")
!ll&&!rl&&r
4300
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 43 $uri->path( $self->gen_versioned_path($uri->path) );
417 $param_ref = $self->prep_param(+{
418 $uri->query_form(+{}),
419 43 ▶ %{$param_ref || +{}},
Condition100%2/2
$param_ref || {}
l!l
349
MC/DC100%1/1
$param_ref || {}
$param_ref
Truth table100%2/2
A
A= $param_ref
Aresult
11
00
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 43 ▶ if ($self->use_appsecret_proof) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->use_appsecret_proof) 1 42 43
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 43 ▶ if ($self->{use_post_method}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"use_post_method"}) 2 41 43
435 2 $param_ref->{method} = $method;
436 2 $method = 'POST';
437 }
438  
439 43 ▶ $headers ||= [];
Condition100%2/2
$headers ||= []
l!l
241
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 43 ▶ if ($self->access_token) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->access_token) 21 22 43
446 21 push @$headers, (
447 'Authorization',
448 sprintf('OAuth %s', $self->access_token),
449 );
450 }
451  
452 43 my $content = q{};
453 43 ▶ if ($method eq 'POST') {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq 'POST') { } 24 19 43
454 24 ▶ if ($param_ref->{source}
Branch: 1 branch
BranchTrueFalseTotal
if ($param_ref->{'source'} or $param_ref->{'file'} or $param_ref->{'upload_phase'} or $param_ref->{'captions_file'}) { } 6 18 24
Condition100%9/9
$param_ref->{"source"} or $param_ref->{"file"} or $param_ref->{"upload_phase"} or $param_ref->{"captions_file"}
l!l&&r!l&&!r
5118
$param_ref->{"source"} or $param_ref->{"file"} or $param_ref->{"upload_phase"}
l!l&&r!l&&!r
3219
$param_ref->{"source"} or $param_ref->{"file"}
l!l&&r!l&&!r
2121
MC/DC100%4/4
$param_ref->{"source"} or $param_ref->{"file"} or $param_ref->{"upload_phase"} or $param_ref->{"captions_file"}
$param_ref->{"source"} $param_ref->{"file"} $param_ref->{"upload_phase"} $param_ref->{"captions_file"}
Truth table100%5/5
A or B or C or D
A= $param_ref->{"source"}B= $param_ref->{"file"}C= $param_ref->{"upload_phase"}D= $param_ref->{"captions_file"}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
455 || $param_ref->{file}
456 || $param_ref->{upload_phase}
457 || $param_ref->{captions_file}) {
458 # post image, video or caption file
459  
460 # https://developers.facebook.com/docs/reference/api/video/
461 # When posting a video, use graph-video.facebook.com .
462 # base_facebook.php has an equivalent part in isVideoPost().
463 # ($method == 'POST' && preg_match("/^(\/)(.+)(\/)(videos)$/", $path))
464 # For other actions, use graph.facebook.com/VIDEO_ID/CONNECTION_TYPE
465 6 ▶ if ($uri->path =~ m{\A /.+/videos \z}xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri->path =~ m[\A /.+/videos \z]msx) 3 3 6
466 3 $uri->host($self->video_uri->host);
467 }
468  
469 # Content-Type should be multipart/form-data
470 # https://developers.facebook.com/docs/reference/api/publishing/
471 6 push @$headers, (Content_Type => 'form-data');
472  
473 # Furl::HTTP document says we can use multipart/form-data with
474 # HTTP::Request::Common.
475 6 my $req = POST $uri, @$headers, Content => [%$param_ref];
476 6 $content = $req->content;
477 6 my $req_header = $req->headers;
478 $headers = +[
479 map {
480 6 my $k = $_;
481 18 map { ( $k => $_ ) } $req_header->header($k);
482 } $req_header->header_field_names
483 ];
484 }
485 else {
486 # Post simple parameters such as message, link, description, etc...
487 # Content-Type: application/x-www-form-urlencoded will be set in
488 # Furl::HTTP, and $content will be treated properly.
489 18 $content = $param_ref;
490 }
491 }
492 else {
493 19 $uri->query_form($param_ref);
494 }
495  
496 43 my ($res_minor_version, @res_elms) = $self->ua->request(
497 method => $method,
498 url => $uri,
499 headers => $headers,
500 content => $content,
501 );
502  
503 43 my $res = $self->create_response(@res_elms);
504  
505 # return F::OG::Response object on success
506 43 ▶ return $res if $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
if $res->is_success 39 4 43
507  
508 # Use later version of Furl::HTTP to utilize req_headers and req_content.
509 # This Should be helpful when debugging.
510 4 my $msg = $res->error_string;
511 4 ▶ if ($res->req_headers) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->req_headers) 0 4 4
512 0 $msg .= "\n" . $res->req_headers . $res->req_content;
513 }
514 4 croak $msg;
515 }
516  
517 # Securing Graph API Requests > Verifying Graph API Calls with appsecret_proof > Generating the proof
518 # https://developers.facebook.com/docs/graph-api/securing-requests/
519 2 ▶sub gen_appsecret_proof {
Subroutine gen_appsecret_proof: called CC=3 SCAR=11.4
Pod documented
520 2 my $self = shift;
521 2 ▶ croak 'app secret must be set' unless $self->secret;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->secret 0 2 2
522 2 ▶ croak 'access_token must be set' unless $self->access_token;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->access_token 0 2 2
523 2 return hmac_sha256_hex($self->access_token, $self->secret);
524 }
525  
526 # Platform Versioning > Making Versioned Requests
527 # https://developers.facebook.com/docs/apps/versions
528 59 ▶sub gen_versioned_path {
Subroutine gen_versioned_path: called CC=4 SCAR=13.9
Pod undocumented
529 59 my ($self, $path) = @_;
530  
531 59 ▶ $path = '/' unless $path;
Branch: 1 branch
BranchTrueFalseTotal
unless $path 4 55 59
532  
533 59 ▶ 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 54 59
Condition100%3/3
$self->version and not $path =~ m[\A /v(?:\d+)\.(?:\d+)/ ]x
!ll&&!rl&&r
5225
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
534 # If default platform version is set on initialisation
535 # and given path doesn't contain version,
536 # then prepend the default version.
537 5 $path = sprintf('/%s%s', $self->version, $path);
538 }
539  
540 59 return $path;
541 }
542  
543 2 ▶sub js_cookie_name {
Subroutine js_cookie_name: called CC=2 SCAR=6.9
Pod documented
544 2 my $self = shift;
545 2 ▶ croak 'app_id must be set' unless $self->app_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->app_id 1 1 2
546  
547 # Cookie is set by JS SDK with a name of fbsr_{app_id}. Official document
548 # is not provided for more than 3 yaers so I quote from PHP SDK's code.
549 # "Constructs and returns the name of the cookie that potentially houses
550 # the signed request for the app user. The cookie is not set by the
551 # BaseFacebook class, but it may be set by the JavaScript SDK."
552 # The cookie value can be parsed as signed request and it contains 'code'
553 # to exchange for access toekn.
554 1 return sprintf('fbsr_%d', $self->app_id);
555 }
556  
557 56 ▶sub create_response {
Subroutine create_response: called CC=1 SCAR=0.0
Pod documented
558 56 my $self = shift;
559 return Facebook::OpenGraph::Response->new(+{
560 json => $self->json,
561 map {
562 56 $_ => shift
563 } qw/code message headers content req_headers req_content/,
564 });
565 }
566  
567 47 ▶sub prep_param {
Subroutine prep_param: called CC=12 SCAR=25.0
Pod documented
568 47 my ($self, $param_ref) = @_;
569  
570 47 ▶ $param_ref = Data::Recursive::Encode->encode_utf8($param_ref || +{});
Condition50%1/2
$param_ref || {}
l!l
470
MC/DC0%0/1
$param_ref || {}
$param_ref
Truth table50%1/2
A
A= $param_ref
Aresult
11
00
571  
572 # /?ids=4,http://facebook-docs.oklahome.net
573 47 ▶ if (my $ids = $param_ref->{ids}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ids = $param_ref->{"ids"}) 1 46 47
574 1 ▶ $param_ref->{ids} = ref $ids ? join q{,}, @$ids : $ids;
Branch: 1 branch
BranchTrueFalseTotal
ref $ids ? : 1 0 1
575 }
576  
577 # mostly for /APP_ID/accounts/test-users
578 47 ▶ if (my $perms = $param_ref->{permissions}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $perms = $param_ref->{"permissions"}) 5 42 47
579 5 ▶ $param_ref->{permissions} = ref $perms ? join q{,}, @$perms : $perms;
Branch: 1 branch
BranchTrueFalseTotal
ref $perms ? : 4 1 5
580 }
581  
582 # Source, file, video_file_chunk and captions_file parameter contains file path.
583 # It must be an array ref to work with HTTP::Request::Common.
584 47 for my $file (qw/source file video_file_chunk captions_file/) {
585 188 ▶ next unless my $path = $param_ref->{$file};
Branch: 1 branch
BranchTrueFalseTotal
unless my $path = $param_ref->{$file} 182 6 188
586 6 ▶ $param_ref->{$file} = ref $path ? $path : [$path];
Branch: 1 branch
BranchTrueFalseTotal
ref $path ? : 0 6 6
587 }
588  
589 # use Field Expansion
590 47 ▶ if (my $field_ref = $param_ref->{fields}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $field_ref = $param_ref->{"fields"}) 4 43 47
591 4 $param_ref->{fields} = $self->prep_fields_recursive($field_ref);
592 }
593  
594 # Using Objects: Using the Object API
595 # https://developers.facebook.com/docs/opengraph/using-objects/#objectapi
596 47 my $object = $param_ref->{object};
597 47 ▶ if ($object && ref $object eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($object and ref $object eq "HASH") 1 46 47
Condition66%2/3
$object and ref $object eq "HASH"
!ll&&!rl&&r
4601
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
598 1 $param_ref->{object} = $self->json->encode($object);
599 }
600  
601 47 return $param_ref;
602 }
603  
604 # Using the Graph API: Reading > Choosing Fields > Making Nested Requests
605 # https://developers.facebook.com/docs/graph-api/using-graph-api/
606 60 ▶sub prep_fields_recursive {
Subroutine prep_fields_recursive: called CC=7 SCAR=19.5
Pod documented
607 60 my ($self, $val) = @_;
608  
609 60 my $ref = ref $val;
610 60 ▶ if (!$ref) {
Branch: 3 branches
BranchTrueFalseTotal
if (not $ref) { } 31 29 60
elsif ($ref eq 'ARRAY') { } 11 18 29
elsif ($ref eq 'HASH') { } 18 0 18
611 31 return $val;
612 }
613 elsif ($ref eq 'ARRAY') {
614 11 return join q{,}, map { $self->prep_fields_recursive($_) } @$val;
615 }
616 elsif ($ref eq 'HASH') {
617 18 my @strs = ();
618 18 ▶ while (my ($k, $v) = each %$val) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$val) 24 18 42
619 24 my $r = ref $v;
620 24 ▶ my $pattern = $r && $r eq 'HASH' ? '%s.%s' : '%s(%s)';
Branch: 1 branch
BranchTrueFalseTotal
$r && $r eq 'HASH' ? : 9 15 24
Condition100%3/3
$r and $r eq "HASH"
!ll&&!rl&&r
969
MC/DC100%2/2
$r and $r eq "HASH"
$r $r eq "HASH"
Truth table100%3/3
A and B
A= $rB= $r eq "HASH"
ABresult
0X0
100
111
621 24 push @strs, sprintf($pattern, $k, $self->prep_fields_recursive($v));
622 }
623 18 return join q{.}, @strs;
624 }
625 }
626  
627 # Using Actions > Publishing Actions
628 # https://developers.facebook.com/docs/opengraph/using-actions/#publish
629 1 ▶sub publish_action {
Subroutine publish_action: called CC=2 SCAR=7.0
Pod documented
630 1 my $self = shift;
631 1 my $action = shift;
632 1 ▶ croak 'namespace is not set' unless $self->namespace;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->namespace 0 1 1
633 1 return $self->post(sprintf('/me/%s:%s', $self->namespace, $action), @_);
634 }
635  
636 # Using Objects > Using the Object API > Images with the Object API
637 # https://developers.facebook.com/docs/opengraph/using-objects/
638 1 ▶sub publish_staging_resource {
Subroutine publish_staging_resource: called CC=1 SCAR=0.0
Pod documented
639 1 my $self = shift;
640 1 my $file = shift;
641 1 return $self->post('/me/staging_resources', +{file => $file}, @_);
642 }
643  
644 # Test Users: Creating
645 # https://developers.facebook.com/docs/test_users/
646 2 ▶sub create_test_users {
Subroutine create_test_users: called CC=3 SCAR=11.0
Pod documented
647 2 my $self = shift;
648 2 my $settings_ref = shift;
649  
650 2 ▶ if (ref $settings_ref ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $settings_ref ne "ARRAY") 1 1 2
651 1 $settings_ref = [$settings_ref];
652 }
653  
654 2 my @settings = ();
655 2 my $relative_url = sprintf('/%s/accounts/test-users', $self->app_id);
656 2 for my $setting (@$settings_ref) {
657 3 push @settings, +{
658 method => 'POST',
659 relative_url => $relative_url,
660 body => $setting,
661 };
662 }
663  
664 2 return $self->batch(\@settings);
665 }
666  
667 # Using Objects > Using Self-Hosted Objects > Updating Objects
668 # https://developers.facebook.com/docs/opengraph/using-objects/
669 6 ▶sub check_object {
Subroutine check_object: called CC=1 SCAR=0.0
Pod documented
670 6 my ($self, $target) = @_;
671 6 my $param_ref = +{
672 id => $target, # $target is object url or open graph object id
673 scrape => 'true',
674 };
675 6 return $self->post(q{}, $param_ref);
676 }
677  
678 33 1;
679 __END__
↑ Summary OpenGraph/Response.pm »