Tumblr/ResponseError.pm

filter: Statementstmt 25.0% 11 / 44 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 28 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 50.0% 3 / 6 Podpod 66.6% 2 / 3 totaltotal 13.3% 16 / 120 CC 24 SCAR 50.2
CC
24
Cov
11%
reasons
210.0
is_rate_limited
132.0
code
2.0
CRAP 151.6
↑ Summary Tumblr/Authentication/OAuth.pm »
1 package WWW::Tumblr::ResponseError;
2  
3 16 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 16 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 16 ▶use JSON 'decode_json';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 16 has 'response', is => 'rw', isa => 'HTTP::Response';
8  
9 0 ▶sub code { $_[0]->response->code }
Subroutine code: not called CC=1 SCAR=6.9
Pod documented
10  
11 0 ▶sub is_rate_limited {
Subroutine is_rate_limited: not called CC=11 SCAR=48.8
Pod undocumented
12 0 my $self = shift;
13 # Check for HTTP 429 (Too Many Requests) or 400 with rate limit error code
14 0 ▶ return 1 if $self->code == 429;
Branch: 1 branch
BranchTrueFalseTotal
if $self->code == 429 0 0 0
15
16 0 ▶ if ($self->code == 400) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->code == 400) 0 0 0
17 0 my $content = $self->response->decoded_content;
18 0 my $j;
19 0 eval { $j = JSON::decode_json($content); };
20 0 ▶ return 0 if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
21
22 # Check for Tumblr's rate limit error code 8004
23 0 ▶ if (ref $j eq 'HASH' && ref $j->{response} eq 'HASH' &&
Branch: 1 branch
BranchTrueFalseTotal
if (ref $j eq "HASH" and ref $j->{"response"} eq "HASH" and ref $j->{"response"}{"errors"} eq "ARRAY") 0 0 0
Condition0%0/6
ref $j eq "HASH" and ref $j->{"response"} eq "HASH" and ref $j->{"response"}{"errors"} eq "ARRAY"
!ll&&!rl&&r
000
ref $j eq "HASH" and ref $j->{"response"} eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/3
ref $j eq "HASH" and ref $j->{"response"} eq "HASH" and ref $j->{"response"}{"errors"} eq "ARRAY"
ref $j eq "HASH" ref $j->{"response"} eq "HASH" ref $j->{"response"}{"errors"} eq "ARRAY"
Truth table0%0/4
A and B and C
A= ref $j eq "HASH"B= ref $j->{"response"} eq "HASH"C= ref $j->{"response"}{"errors"} eq "ARRAY"
ABCresult
0XX0
10X0
1100
1111
24 ref $j->{response}{errors} eq 'ARRAY') {
25 0 for my $err (@{$j->{response}{errors}}) {
26 0 ▶ return 1 if ref $err eq 'HASH' && ($err->{code} || 0) == 8004;
Branch: 1 branch
BranchTrueFalseTotal
if ref $err eq "HASH" and ($err->{"code"} || 0) == 8004 0 0 0
Condition0%0/5
ref $err eq "HASH" and ($err->{"code"} || 0) == 8004
!ll&&!rl&&r
000
$err->{'code'} || 0
l!l
00
MC/DC0%0/2
ref $err eq "HASH" and ($err->{"code"} || 0) == 8004
ref $err eq "HASH" $err->{'code'}
Truth table0%0/3
A and B
A= ref $err eq "HASH"B= $err->{'code'}
ABresult
0X0
100
111
27 }
28 }
29 }
30 0 return 0;
31 }
32 0 ▶sub reasons {
Subroutine reasons: not called CC=14 SCAR=53.5
Pod documented
33 0 my $self = $_[0];
34 0 my $content = $_[0]->response->decoded_content;
35 0 my $j;
36 0 eval { $j = decode_json($content); };
37 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
38 # Response is not valid JSON, return HTTP message
39 0 ▶ return [ $self->response->message || 'Unknown error' ];
Condition0%0/2
$self->response->message || 'Unknown error'
l!l
00
MC/DC0%0/1
$self->response->message || 'Unknown error'
$self->response->message
Truth table0%0/2
A
A= $self->response->message
Aresult
11
00
40 }
41 0 ▶ if ( ref $j && ref $j eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $j and ref $j eq 'HASH') { } 0 0 0
Condition0%0/3
ref $j and ref $j eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $j and ref $j eq "HASH"
ref $j ref $j eq "HASH"
Truth table0%0/3
A and B
A= ref $jB= ref $j eq "HASH"
ABresult
0X0
100
111
42 0 ▶ if ( ref $j->{response} && ref $j->{response} eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $j->{'response'} and ref $j->{'response'} eq 'ARRAY') { } 0 0 0
elsif (ref $j->{'response'} and ref $j->{'response'} eq 'HASH' and defined $j->{'response'}{'errors'}) { } 0 0 0
Condition0%0/3
ref $j->{"response"} and ref $j->{"response"} eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $j->{"response"} and ref $j->{"response"} eq "ARRAY"
ref $j->{"response"} ref $j->{"response"} eq "ARRAY"
Truth table0%0/3
A and B
A= ref $j->{"response"}B= ref $j->{"response"} eq "ARRAY"
ABresult
0X0
100
111
43 0 ▶ unless ( scalar @{ $j->{response} }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @{$j->{"response"};}) 0 0 0
44 0 return [ $self->response->message ]
45 }
46 0 return $j->{response};
47 ▶ } elsif ( ref $j->{response} && ref $j->{response} eq 'HASH' &&
Condition0%0/6
ref $j->{"response"} and ref $j->{"response"} eq "HASH" and defined $j->{"response"}{"errors"}
!ll&&!rl&&r
000
ref $j->{"response"} and ref $j->{"response"} eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/3
ref $j->{"response"} and ref $j->{"response"} eq "HASH" and defined $j->{"response"}{"errors"}
ref $j->{"response"} ref $j->{"response"} eq "HASH" defined $j->{"response"}{"errors"}
Truth table0%0/4
A and B and C
A= ref $j->{"response"}B= ref $j->{"response"} eq "HASH"C= defined $j->{"response"}{"errors"}
ABCresult
0XX0
10X0
1100
1111
48 defined $j->{response}->{errors}
49 ) {
50 0 ▶ if ( ref $j->{response}->{errors} eq 'HASH' &&
Branch: 2 branches
BranchTrueFalseTotal
if (ref $j->{'response'}{'errors'} eq 'HASH' and defined $j->{'response'}{'errors'}{'state'}) { } 0 0 0
elsif (ref $j->{'response'}{'errors'} eq 'ARRAY') { } 0 0 0
Condition0%0/3
ref $j->{"response"}{"errors"} eq "HASH" and defined $j->{"response"}{"errors"}{"state"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $j->{"response"}{"errors"} eq "HASH" and defined $j->{"response"}{"errors"}{"state"}
ref $j->{"response"}{"errors"} eq "HASH" defined $j->{"response"}{"errors"}{"state"}
Truth table0%0/3
A and B
A= ref $j->{"response"}{"errors"} eq "HASH"B= defined $j->{"response"}{"errors"}{"state"}
ABresult
0X0
100
111
51 defined $j->{response}->{errors}->{state} ) {
52 return [
53 $j->{response}->{errors}->{0},
54 $j->{response}->{errors}->{state}
55 0 ];
56 } elsif ( ref $j->{response}->{errors} eq 'ARRAY' ) {
57 0 return $j->{response}->{errors};
58 } else {
59 0 Carp::croak "Unimplemented";
60 }
61 } else {
62 0 Carp::croak "Unimplemented";
63 }
64 } else {
65 0 Carp::croak "Unimplemented";
66 }
67 }
68  
69 16 1;
70  
71 =pod
72  
73 =head1 NAME
74  
75 WWW::Tumblr::ResponseError
76  
77 =head1 SYNOPSIS
78  
79 my $posts = $tumblr->blog('stupidshit.tumblr.com')->posts;
80  
81 die "Couldn't get posts! " . Dumper( $tumblr->error->reasons ) unless $posts;
82  
83 =head1 DESCRIPTION
84  
85 This a class representing L<WWW::Tumblr>'s C<error> method. It contains the
86 response from upstream Tumblr API.
87  
88 =head1 METHODS
89  
90 =head2 error
91  
92 Callable from a model context, usually L<WWW::Tumblr>.
93  
94 print Dumper $tumblr->error unless $post;
95  
96 =head2 code
97  
98 HTTP response code for the error:
99  
100 my $info = $blog->info;
101 print $blog->error->code . ' nono :(' unless $info;
102  
103 =head2 reasons
104  
105 Commodity method to display reasons why the error ocurred. It returns an array
106 reference:
107  
108 unless ( $some_tumblr_action ) {
109 print "Errors! \n";
110 print $_, "\n" for @{ $tumblr->error->reasons || [] };
111 }
112  
113 =head1 BUGS
114  
115 Please refer to L<WWW::Tumblr>.
116  
117 =head1 AUTHOR(S)
118  
119 The same folks as L<WWW::Tumblr>.
120  
121 =head1 SEE ALSO
122  
123 L<WWW::Tumblr>, L<WWW::Tumblr::ResponseError>.
124  
125 =head1 COPYRIGHT and LICENSE
126  
127 Same as L<WWW::Tumblr>.
128  
129 =cut
130  
↑ Summary Tumblr/Authentication/OAuth.pm »