OpenGraph/Response.pm

filter: Statementstmt 98.5% 69 / 70 Branchbran 64.2% 9 / 14 Conditioncond 82.0% 32 / 39 MC/DCmcdc 59.0% 13 / 22 Subroutinesub 100% 23 / 23 Podpod 100% 18 / 18 totaltotal 88.1% 164 / 186 CC 23 SCAR 11.5
CC
23
Cov
89%
error_string
6.3
new
5.1
is_api_version_eq_or_older_than
4.1
CRAP 3.2
« OpenGraph.pm ↑ Summary
1 package Facebook::OpenGraph::Response;
2 34 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 34 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 34 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 34 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 34 ▶use JSON 2 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 61 ▶sub new {
Subroutine new: called CC=5 SCAR=16.3
Pod documented
10 61 my $class = shift;
11 61 ▶ my $args = shift || +{};
Condition100%2/2
shift() || {}
l!l
601
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
12  
13 return bless +{
14 json => $args->{json} || JSON->new->utf8,
15 headers => $args->{headers},
16 code => $args->{code},
17 message => $args->{message},
18 content => $args->{content},
19 req_headers => $args->{req_headers} || q{},
20 61 ▶ req_content => $args->{req_content} || q{},
Condition71%5/7
$args->{'json'} || 'JSON'->new->utf8
l!l&&r!l&&!r
5650
$args->{'req_headers'} || ''
l!l
457
$args->{'req_content'} || ''
l!l
061
MC/DC0%0/2
$args->{'json'} || 'JSON'->new->utf8
$args->{'json'} 'JSON'->new->utf8
MC/DC100%1/1
$args->{'req_headers'} || ''
$args->{'req_headers'}
MC/DC0%0/1
$args->{'req_content'} || ''
$args->{'req_content'}
Truth table66%2/3
A || B
A= $args->{'json'}B= 'JSON'->new->utf8
ABresult
1X1
011
000
Truth table100%2/2
A
A= $args->{'req_headers'}
Aresult
11
00
Truth table50%1/2
A
A= $args->{'req_content'}
Aresult
11
00
21 }, $class;
22 }
23  
24 # accessors
25 64 ▶sub code { shift->{code} }
Subroutine code: called CC=1 SCAR=0.0
Pod documented
26 36 ▶sub headers { shift->{headers} }
Subroutine headers: called CC=1 SCAR=0.0
Pod documented
27 2 ▶sub message { shift->{message} }
Subroutine message: called CC=1 SCAR=0.0
Pod documented
28 57 ▶sub content { shift->{content} }
Subroutine content: called CC=1 SCAR=0.0
Pod documented
29 5 ▶sub req_headers { shift->{req_headers} }
Subroutine req_headers: called CC=1 SCAR=0.0
Pod documented
30 1 ▶sub req_content { shift->{req_content} }
Subroutine req_content: called CC=1 SCAR=0.0
Pod documented
31 53 ▶sub json { shift->{json} }
Subroutine json: called CC=1 SCAR=0.0
Pod documented
32 1 ▶sub etag { shift->header('etag') }
Subroutine etag: called CC=1 SCAR=0.0
Pod documented
33  
34 23 ▶sub api_version {
Subroutine api_version: called CC=1 SCAR=0.0
Pod documented
35 23 my $self = shift;
36 23 return $self->header('facebook-api-version');
37 }
38  
39 14 ▶sub is_api_version_eq_or_later_than {
Subroutine is_api_version_eq_or_later_than: called CC=4 SCAR=13.9
Pod documented
40 14 my ($self, $comparing_version) = @_;
41 14 ▶ croak 'comparing version is not given.' unless $comparing_version;
Branch: 1 branch
BranchTrueFalseTotal
unless $comparing_version 0 14 14
42  
43 14 (my $comp_major, my $comp_minor)
44 = $comparing_version =~ m/ (\d+) \. (\d+ )/x;
45  
46 14 (my $response_major, my $response_minor)
47 = $self->api_version =~ m/ (\d+) \. (\d+ )/x;
48  
49 14 ▶ return $comp_major < $response_major || ($comp_major == $response_major && $comp_minor <= $response_minor);
Condition100%6/6
$comp_major < $response_major || $comp_major == $response_major && $comp_minor <= $response_minor
l!l&&r!l&&!r
338
$comp_major == $response_major and $comp_minor <= $response_minor
!ll&&!rl&&r
263
MC/DC100%3/3
$comp_major < $response_major || $comp_major == $response_major && $comp_minor <= $response_minor
$comp_major < $response_major $comp_major == $response_major $comp_minor <= $response_minor
Truth table100%4/4
A || B and C
A= $comp_major < $response_majorB= $comp_major == $response_majorC= $comp_minor <= $response_minor
ABCresult
1XX1
0111
00X0
0100
50 }
51  
52 8 ▶sub is_api_version_eq_or_older_than {
Subroutine is_api_version_eq_or_older_than: called CC=4 SCAR=14.0
Pod documented
53 8 my ($self, $comparing_version) = @_;
54 8 ▶ croak 'comparing version is not given.' unless $comparing_version;
Branch: 1 branch
BranchTrueFalseTotal
unless $comparing_version 0 8 8
55  
56 8 (my $comp_major, my $comp_minor)
57 = $comparing_version =~ m/ (\d+) \. (\d+ )/x;
58  
59 8 (my $response_major, my $response_minor)
60 = $self->api_version =~ m/ (\d+) \. (\d+ )/x;
61  
62 8 ▶ return $response_major < $comp_major || ($response_major == $comp_major && $response_minor <= $comp_minor);
Condition83%5/6
$response_major < $comp_major || $response_major == $comp_major && $response_minor <= $comp_minor
l!l&&r!l&&!r
332
$response_major == $comp_major and $response_minor <= $comp_minor
!ll&&!rl&&r
203
MC/DC66%2/3
$response_major < $comp_major || $response_major == $comp_major && $response_minor <= $comp_minor
$response_major < $comp_major $response_major == $comp_major $response_minor <= $comp_minor
Truth table75%3/4
A || B and C
A= $response_major < $comp_majorB= $response_major == $comp_majorC= $response_minor <= $comp_minor
ABCresult
1XX1
0111
00X0
0100
63 }
64  
65 24 ▶sub header {
Subroutine header: called CC=4 SCAR=13.9
Pod documented
66 24 my ($self, $key) = @_;
67  
68 24 ▶ croak 'header field name is not given' unless $key;
Branch: 1 branch
BranchTrueFalseTotal
unless $key 0 24 24
69  
70 24 ▶ $self->{header} ||= do {
Condition66%2/3
$self->{"header"} ||= do { my $ref = {}; while (() = splice(@{$self->headers;}, 0, 2)) { do { $ref->{$k} = $v }; } ; $ref }
l!l&&r!l&&!r
1590
MC/DC0%0/2
$self->{"header"} ||= do { my $ref = {}; while (() = splice(@{$self->headers;}, 0, 2)) { do { $ref->{$k} = $v }; } ; $ref }
$self->{"header"} do { my $ref = {}; while (() = splice(@{$self->headers;}, 0, 2)) { do { $ref->{$k} = $v }; } ; $ref }
Truth table66%2/3
A ||= B
A= $self->{"header"}B= do { my $ref = {}; while (() = splice(@{$self->headers;}, 0, 2)) { do { $ref->{$k} = $v }; } ; $ref }
ABresult
1X1
011
000
71 9 my $ref = +{};
72  
73 9 ▶ while (my ($k, $v) = splice @{ $self->headers }, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@{$self->headers;}, 0, 2)) 26 9 35
74 26 $ref->{$k} = $v;
75 }
76  
77 9 $ref;
78 };
79  
80 24 return $self->{header}->{$key};
81 }
82  
83 56 ▶sub is_success {
Subroutine is_success: called CC=2 SCAR=6.9
Pod documented
84 56 my $self = shift;
85 # code 2XX or 304
86 # 304 is returned when you use ETag and the data is not changed
87 56 ▶ return substr($self->code, 0, 1) == 2 || $self->code == 304;
Condition100%3/3
substr($self->code, 0, 1) == 2 || $self->code == 304
l!l&&r!l&&!r
5114
MC/DC100%2/2
substr($self->code, 0, 1) == 2 || $self->code == 304
substr($self->code, 0, 1) == 2 $self->code == 304
Truth table100%3/3
A || B
A= substr($self->code, 0, 1) == 2B= $self->code == 304
ABresult
1X1
011
000
88 }
89  
90 # Using the Graph API > Handling Errors
91 # https://developers.facebook.com/docs/graph-api/using-graph-api/
92 5 ▶sub error_string {
Subroutine error_string: called CC=6 SCAR=18.5
Pod documented
93 5 my $self = shift;
94  
95 # When an error occurs, the response should be given in a form below:
96 #{
97 # "error": {
98 # "message": "Message describing the error",
99 # "type": "OAuthException",
100 # "code": 190,
101 # "error_subcode": 460,
102 # "error_user_title": "A title",
103 # "error_user_msg": "A message",
104 # "fbtrace_id": "EJplcsCHuLu"
105 # }
106 #}
107 5 my $error = eval { $self->as_hashref->{error}; };
108  
109 5 my $err_str = q{};
110 5 ▶ if ($@ || !$error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or not $error) { } 0 5 5
Condition33%1/3
$@ or not $error
l!l&&r!l&&!r
005
MC/DC0%0/2
$@ or not $error
$@ not $error
Truth table33%1/3
A or not(B)
A= $@B= not $error
ABresult
1X1
011
000
111 0 $err_str = $self->message;
112 }
113 else {
114 # sometimes error_subcode is not given
115 $err_str = sprintf(
116 qq{%s:%s\t%s:%s\t%s\t%s:%s},
117 $error->{code},
118 $error->{error_subcode} || '-',
119 $error->{type},
120 $error->{message},
121 $error->{fbtrace_id},
122 $error->{error_user_title} || '-',
123 5 ▶ $error->{error_user_msg} || '-',
Condition100%6/6
$error->{'error_subcode'} || '-'
l!l
14
$error->{'error_user_title'} || '-'
l!l
14
$error->{'error_user_msg'} || '-'
l!l
14
MC/DC100%1/1
$error->{'error_subcode'} || '-'
$error->{'error_subcode'}
MC/DC100%1/1
$error->{'error_user_title'} || '-'
$error->{'error_user_title'}
MC/DC100%1/1
$error->{'error_user_msg'} || '-'
$error->{'error_user_msg'}
Truth table100%2/2
A
A= $error->{'error_subcode'}
Aresult
11
00
Truth table100%2/2
A
A= $error->{'error_user_title'}
Aresult
11
00
Truth table100%2/2
A
A= $error->{'error_user_msg'}
Aresult
11
00
124 );
125 }
126  
127 5 return $err_str;
128 }
129  
130 51 ▶sub as_json {
Subroutine as_json: called CC=2 SCAR=6.9
Pod documented
131 51 my $self = shift;
132  
133 51 my $content = $self->content;
134 51 ▶ if ($content =~ m{\A (true|false) \z}xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /\A (true|false) \z/msx) 1 50 51
135 # On v2.0 and older version, some endpoints return plain text saying
136 # 'true' or 'false' to indicate result, so make it JSON formatted for
137 # our convinience. The key is named "success" so its format matches with
138 # other endpoints that return {"success": "(true|false)"}.
139 # From v2.1 they always return in form of {"success": "(true|false)"}.
140 # See https://developers.facebook.com/docs/apps/changelog#v2_1_changes
141 1 $content = sprintf('{"success" : "%s"}', $1);
142 };
143  
144 51 return $content; # content is JSON formatted
145 }
146  
147 51 ▶sub as_hashref {
Subroutine as_hashref: called CC=2 SCAR=7.0
Pod documented
148 51 my $self = shift;
149 # just in case content is not properly formatted
150 51 my $hash_ref = eval { $self->json->decode( $self->as_json ); };
151 51 ▶ croak $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 51 51
152 51 return $hash_ref;
153 }
154  
155 # Indicates whether the data is modified.
156 # It should be used when you request with ETag.
157 # https://developers.facebook.com/docs/reference/ads-api/etags-reference/
158 2 ▶sub is_modified {
Subroutine is_modified: called CC=2 SCAR=7.0
Pod documented
159 2 my $self = shift;
160 2 ▶ my $not_modified = $self->code == 304 && $self->message eq 'Not Modified';
Condition66%2/3
$self->code == 304 && $self->message eq 'Not Modified'
!ll&&!rl&&r
101
MC/DC50%1/2
$self->code == 304 && $self->message eq 'Not Modified'
$self->code == 304 $self->message eq 'Not Modified'
Truth table66%2/3
A && B
A= $self->code == 304B= $self->message eq 'Not Modified'
ABresult
0X0
100
111
161 2 return !$not_modified;
162 }
163  
164 34 1;
165 __END__
« OpenGraph.pm ↑ Summary