Consumer/HandlerInvocation.pm

filter: Statementstmt 92.0% 58 / 63 Branchbran 100% 8 / 8 Conditioncond 83.3% 5 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 79.1% 19 / 24 Podpod 0.0% 0 / 12 totaltotal 78.6% 92 / 117 CC 7 SCAR 6.6
CC
7
Cov
92%
attempt_content_deserialization
5.0
new
2.0
fail
2.0
CRAP 1.9
« Consumer/RequestException.pm ↑ Summary Consumer/Exception.pm »
1 package REST::Consumer::HandlerInvocation;
2  
3 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 9 ▶use REST::Consumer::ResponseException;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # This is an object which is passed to a coderef in the handlers => {}
9 # hash. It represents an invocation of a particular response-code handler.
10 #
11 # Your code should never need to instantiate this class itself.
12 53 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod undocumented
13 53 my ($class, %args) = @_;
14 53 my $self = (bless {%args} => $class);
15 53 ▶ $self->{debugger} ||= sub {};
Subroutine __ANON__: not called CC=1 SCAR=0.2
Condition66%2/3
$self->{"debugger"} ||= sub { }
l!l&&r!l&&!r
5210
MC/DC0%0/2
$self->{"debugger"} ||= sub { }
$self->{"debugger"} sub { }
Truth table66%2/3
A ||= B
A= $self->{"debugger"}B= sub { }
ABresult
1X1
011
000
16 53 return $self;
17 }
18  
19 # Here are elments of the public API:
20 # Magic values your code can return to change the execution flow:
21 1 ▶sub default {
Subroutine default: called CC=1 SCAR=0.0
Pod undocumented
22 1 my ($self) = @_;
23 1 return REST::Consumer::HandlerFlow::Default->new;
24 }
25 39 ▶sub retry {
Subroutine retry: called CC=1 SCAR=0.0
Pod undocumented
26 39 my ($self) = @_;
27 39 return REST::Consumer::HandlerFlow::Retry->new(
28 request => $self->request,
29 response => $self->response,
30 attempt => $self->attempt,
31 );
32 }
33 0 ▶sub fail {
Subroutine fail: not called CC=1 SCAR=6.9
Pod undocumented
34 0 my ($self) = @_;
35 0 return REST::Consumer::HandlerFlow::Fail->new(
36 request => $self->request,
37 response => $self->response,
38 attempt => $self->attempt,
39 );
40 }
41  
42  
43 # Accessors that can provide information to your handler:
44 39 ▶sub request { shift->{request} }
Subroutine request: called CC=1 SCAR=0.0
Pod undocumented
45 223 ▶sub response { shift->{response} }
Subroutine response: called CC=1 SCAR=0.0
Pod undocumented
46 39 ▶sub attempt { shift->{attempt} } # e.g. attempt #2
Subroutine attempt: called CC=1 SCAR=0.0
Pod undocumented
47  
48 # ->parsed_response throws an exception if the response is not parseable.
49 # You might want to ask whether ->response_parseable.
50 5 ▶sub parsed_response {
Subroutine parsed_response: called CC=2 SCAR=6.9
Pod undocumented
51 5 my ($self) = @_;
52 5 $self->attempt_content_deserialization;
53 5 ▶ unless ($self->{response_parseable}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"response_parseable"}) 1 4 5
54 REST::Consumer::ResponseException->throw(
55 request => $self->{request},
56 response => $self->{response},
57 attempts => $self->{attempt},
58 1 );
59 }
60 4 return $self->{parsed_response};
61 }
62  
63 # True iff the response was parseable.
64 8 ▶sub response_parseable {
Subroutine response_parseable: called CC=1 SCAR=0.0
Pod undocumented
65 8 my ($self) = @_;
66 8 $self->attempt_content_deserialization;
67 8 return $self->{response_parseable};
68 }
69  
70 # This is never parsed, but it is decoded.
71 7 ▶sub response_body {
Subroutine response_body: called CC=1 SCAR=0.0
Pod undocumented
72 7 my ($self) = @_;
73 7 $self->attempt_content_deserialization;
74 7 return $self->{response_body};
75 }
76  
77  
78 # This is a private API. Do not invoke.
79 20 ▶sub attempt_content_deserialization {
Subroutine attempt_content_deserialization: called CC=5 SCAR=16.1
Pod undocumented
80 20 my ($self) = @_;
81 20 ▶ return if exists $self->{response_body};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"response_body"} 8 12 20
82 12 $self->{response_body} = $self->response->decoded_content();
83  
84 # parse response content, if present
85 12 my $response_content;
86 12 my $content_type = $self->response->header('Content-Type');
87 12 ▶ if ($content_type && $content_type =~ m|.+/json|) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content_type and $content_type =~ m[.+/json]) 10 2 12
Condition100%3/3
$content_type and $content_type =~ m[.+/json]
!ll&&!rl&&r
1110
MC/DC100%2/2
$content_type and $content_type =~ m[.+/json]
$content_type $content_type =~ m[.+/json]
Truth table100%3/3
A and B
A= $content_typeB= $content_type =~ m[.+/json]
ABresult
0X0
100
111
88 eval {
89 10 $self->{parsed_response} = JSON::XS::decode_json($self->response->decoded_content() );
90 4 $self->{response_parseable} = 1;
91 4 1;
92 10 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->{"parsed_response"} = &JSON::XS::decode_json(scalar $self->response->decoded_content); $self->{"response_parseable"} = 1; 1 } } or do { $self->debug(sprintf("failed to parse json response: %s\n%s\n", $self->{"response_body"}, $@)); $self->{"response_parseable"} = 0 } 4 6 10
93 # might or might not be an error. e.g. if content is empty or is just a string
94 $self->debug(sprintf("failed to parse json response: %s\n%s\n",
95 $self->{response_body},
96 6 $@,
97 ));
98 6 $self->{response_parseable} = 0;
99 };
100 };
101 }
102  
103 6 ▶sub debug {
Subroutine debug: called CC=1 SCAR=0.0
Pod undocumented
104 6 shift->{debugger}->(@_);
105 }
106  
107 # These are part of a private API. You should not instantiate them.
108 # Instead access them in the scope of a handler like so:
109 # 4xx => sub {
110 # my ($h) = @_;
111 # return $h->default; # or
112 # return $h->retry; # or
113 # return $h->fail;
114 # }
115 package REST::Consumer::HandlerFlow::Base;
116 40 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
117 40 my ($self, %args) = @_;
118 40 return bless {%args} => $self;
119 }
120  
121 11 ▶sub throw {
Subroutine throw: called CC=1 SCAR=0.0
122 11 my ($self) = @_;
123 REST::Consumer::RequestException->throw(
124 request => $self->{request},
125 response => $self->{response},
126 attempts => $self->{attempt},
127 11 );
128 }
129  
130  
131 package REST::Consumer::HandlerFlow::Default;
132 9 ▶use base qw(REST::Consumer::HandlerFlow::Base);
Subroutine BEGIN: called CC=1 SCAR=0.0
133 0 ▶sub rest_consumer_should_default { 1 }
Subroutine rest_consumer_should_default: not called CC=1 SCAR=6.9
134  
135 package REST::Consumer::HandlerFlow::Retry;
136 9 ▶use base qw(REST::Consumer::HandlerFlow::Base);
Subroutine BEGIN: called CC=1 SCAR=0.0
137 0 ▶sub rest_consumer_should_retry { 1 }
Subroutine rest_consumer_should_retry: not called CC=1 SCAR=6.9
138  
139 package REST::Consumer::HandlerFlow::Fail;
140 9 ▶use base qw(REST::Consumer::HandlerFlow::Base);
Subroutine BEGIN: called CC=1 SCAR=0.0
141 0 ▶sub rest_consumer_should_fail { 1 }
Subroutine rest_consumer_should_fail: not called CC=1 SCAR=6.9
142  
143 9 1;
« Consumer/RequestException.pm ↑ Summary Consumer/Exception.pm »