| 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() || {}
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||
| 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
$args->{'req_headers'} || ''
$args->{'req_content'} || ''
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
Truth table100%2/2
A
A= $args->{'req_headers'}
Truth table50%1/2
A
A= $args->{'req_content'}
| |||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||
| 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
$comp_major == $response_major and $comp_minor <= $response_minor
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
| |||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||
| 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
$response_major == $comp_major and $response_minor <= $comp_minor
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
| |||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||
| 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
}
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
}
| |||||||||||||||||||||||||||||||||||||||||
| 71 | 9 | my $ref = +{}; | |||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||
| 73 | 9 | ▶ | while (my ($k, $v) = splice @{ $self->headers }, 0, 2) { | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 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
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
| |||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
$@ or not $error
MC/DC0%0/2
$@ or not $error
$@
not $error
Truth table33%1/3
A or not(B)
A= $@B= not $error
| |||||||||||||||||||||||||||||||||||||||||
| 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'} || '-'
$error->{'error_user_title'} || '-'
$error->{'error_user_msg'} || '-'
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'}
Truth table100%2/2
A
A= $error->{'error_user_title'}
Truth table100%2/2
A
A= $error->{'error_user_msg'}
| |||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||
| 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'
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'
| |||||||||||||||||||||||||||||||||||||||||
| 161 | 2 | return !$not_modified; | |||||||||||||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||
| 164 | 34 | 1; | |||||||||||||||||||||||||||||||||||||||
| 165 | __END__ | ||||||||||||||||||||||||||||||||||||||||