blib/lib/Stash/REST.pm

filter: Statementstmt 95.6% 220 / 230 Branchbran 67.8% 95 / 140 Conditioncond 62.2% 56 / 90 MC/DCmcdc 33.9% 19 / 56 Subroutinesub 100% 28 / 28 Podpod 100% 8 / 8 totaltotal 77.1% 426 / 552 CC 104 SCAR 36.8
CC
104
Cov
81%
_rest_request
82.4
_capture_args
28.4
rest_reload_list
12.8
CRAP 39.8
↑ Summary
1 package Stash::REST;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use 5.008_005;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 our $VERSION = '0.11';
5  
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use URI::QueryParam;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use HTTP::Request::Common qw(GET POST DELETE HEAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Carp qw/confess cluck/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 ▶use Class::Trigger;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 has 'do_request' => (
19 is => 'rw',
20 1 ▶ isa => sub { die "$_[0] is not a CodeRef" unless ref $_[0] eq 'CODE' },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "CODE" 0 1 1
21 1 required => 1
22 );
23  
24 has 'decode_response' => (
25 is => 'rw',
26 1 ▶ isa => sub { die "$_[0] is not a CodeRef" unless ref $_[0] eq 'CODE' },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "CODE" 0 1 1
27 1 required => 1
28 );
29  
30 has 'stash' => (
31 is => 'rw',
32 1 ▶ isa => sub { die "$_[0] is not a HashRef" unless ref $_[0] eq 'HASH' },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "HASH" 0 1 1
33 1 ▶ default => sub { {} }
Subroutine __ANON__: called CC=1 SCAR=0.0
34 1 );
35  
36 has 'fixed_headers' => (
37 is => 'rw',
38 2 ▶ isa => sub { die "$_[0] is not a ArrayRef" unless ref $_[0] eq 'ARRAY' },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "ARRAY" 0 2 2
39 1 ▶ default => sub { [] }
Subroutine __ANON__: called CC=1 SCAR=0.0
40 1 );
41  
42 89 ▶around 'stash' => sub {
Subroutine __ANON__: called CC=7 SCAR=19.7
43 89 my $orig = shift;
44 89 my $c = shift;
45 89 my $stash = $orig->($c);
46  
47 89 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 23 66 89
48 23 ▶ return $stash->{ $_[0] } if ( @_ == 1 && ref $_[0] eq '' );
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 and ref $_[0] eq "" 5 18 23
Condition66%2/3
@_ == 1 and ref $_[0] eq ""
!ll&&!rl&&r
1805
MC/DC50%1/2
@_ == 1 and ref $_[0] eq ""
@_ == 1 ref $_[0] eq ""
Truth table66%2/3
A and B
A= @_ == 1B= ref $_[0] eq ""
ABresult
0X0
100
111
49  
50 18 ▶ my $new_stash = @_ > 1 ? {@_} : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 18 0 18
51 18 ▶ die('stash takes a hash or hashref') unless ref $new_stash;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $new_stash 0 18 18
52 18 foreach my $key ( keys %$new_stash ) {
53 19 $stash->{$key} = $new_stash->{$key};
54 }
55 }
56  
57 84 return $stash;
58 1 };
59  
60 20 ▶sub _capture_args {
Subroutine _capture_args: called CC=22 SCAR=33.5
61 20 my ( $method, $self, @params ) = @_;
62 20 my ( $uri, $data, %conf );
63  
64 20 ▶ confess 'invalid number of params' if @params < 1;
Branch: 1 branch
BranchTrueFalseTotal
if @params < 1 1 19 20
65  
66 19 $uri = shift @params;
67 19 ▶ confess 'invalid uri param' if ref $uri ne '' && ref $uri ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $uri ne "" and ref $uri ne "ARRAY" 1 18 19
Condition100%3/3
ref $uri ne "" and ref $uri ne "ARRAY"
!ll&&!rl&&r
1711
MC/DC100%2/2
ref $uri ne "" and ref $uri ne "ARRAY"
ref $uri ne "" ref $uri ne "ARRAY"
Truth table100%3/3
A and B
A= ref $uri ne ""B= ref $uri ne "ARRAY"
ABresult
0X0
100
111
68  
69 18 ▶ $uri = join '/', @$uri if ref $uri eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $uri eq "ARRAY" 1 17 18
70  
71 # if number of params is odd, then, the last item is defined as $data
72 18 ▶ if ( scalar @params % 2 == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @params % 2 == 0) { } 12 6 18
73 12 %conf = @params;
74 12 ▶ $data = exists $conf{data} ? $conf{data} : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $conf{'data'} ? : 6 6 12
75 }
76 else {
77 6 $data = pop @params;
78 6 %conf = @params;
79 }
80  
81 0 ▶ $conf{headers} = [ %{ $conf{headers} } ] if exists $conf{headers} && ref $conf{headers} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if exists $conf{"headers"} and ref $conf{"headers"} eq "HASH" 0 18 18
Condition66%2/3
exists $conf{"headers"} and ref $conf{"headers"} eq "HASH"
!ll&&!rl&&r
1620
MC/DC0%0/2
exists $conf{"headers"} and ref $conf{"headers"} eq "HASH"
exists $conf{"headers"} ref $conf{"headers"} eq "HASH"
Truth table66%2/3
A and B
A= exists $conf{"headers"}B= ref $conf{"headers"} eq "HASH"
ABresult
0X0
100
111
82  
83 confess "Can't use ->{files} helper with custom Content-Type."
84 0 ▶ if exists $conf{files} && ( exists $conf{headers} && grep { /Content-Type/i } @{ $conf{headers} } );
Branch: 1 branch
BranchTrueFalseTotal
if exists $conf{"files"} and (exists $conf{"headers"} and grep {/Content-Type/i;} @{$conf{"headers"};}) 0 18 18
Condition50%3/6
exists $conf{"files"} and (exists $conf{"headers"} and grep {/Content-Type/i;} @{$conf{"headers"};})
!ll&&!rl&&r
1710
exists $conf{"headers"} and grep {/Content-Type/i;} @{$conf{"headers"};}
!ll&&!rl&&r
100
MC/DC0%0/3
exists $conf{"files"} and (exists $conf{"headers"} and grep {/Content-Type/i;} @{$conf{"headers"};})
exists $conf{"files"} exists $conf{"headers"} grep {/Content-Type/i;} @{$conf{"headers"};}
Truth table50%2/4
A and B and C
A= exists $conf{"files"}B= exists $conf{"headers"}C= grep {/Content-Type/i;} @{$conf{"headers"};}
ABCresult
0XX0
10X0
1100
1111
85  
86 18 my $can_have_body = $method =~ /POST|PUT|DELETE/;
87  
88 18 ▶ if ( !$can_have_body && $data && exists $conf{params} ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not $can_have_body and $data and exists $conf{'params'}) { } 1 17 18
elsif (not $can_have_body and $data and not exists $conf{'params'}) { } 3 14 17
elsif (not $can_have_body and $data) { } 0 14 14
Condition100%6/6
not $can_have_body and $data and exists $conf{"params"}
!ll&&!rl&&r
1431
not $can_have_body and $data
!ll&&!rl&&r
954
MC/DC100%3/3
not $can_have_body and $data and exists $conf{"params"}
not $can_have_body $data exists $conf{"params"}
Truth table100%4/4
not(A) and B and C
A= not $can_have_bodyB= $dataC= exists $conf{"params"}
ABCresult
0XX0
10X0
1100
1111
89 confess "You are using {data} and {params} in $method. Please, use only {params} instead"
90 1 ▶ unless $conf{allow_get_with_body};
Branch: 1 branch
BranchTrueFalseTotal
unless $conf{"allow_get_with_body"} 1 0 1
91 0 $conf{data} = $data;
92 }
93 ▶ elsif ( !$can_have_body && $data && !exists $conf{params} ) {
Condition83%5/6
not $can_have_body and $data and not exists $conf{"params"}
!ll&&!rl&&r
1403
not $can_have_body and $data
!ll&&!rl&&r
953
MC/DC66%2/3
not $can_have_body and $data and not exists $conf{"params"}
not $can_have_body $data not exists $conf{"params"}
Truth table75%3/4
not(A) and B and not(C)
A= not $can_have_bodyB= $dataC= not exists $conf{"params"}
ABCresult
0XX0
10X0
1100
1111
94 3 $conf{params} = $data;
95 }
96 ▶ elsif ( !$can_have_body && $data ) {
Condition66%2/3
not $can_have_body and $data
!ll&&!rl&&r
950
MC/DC0%0/2
not $can_have_body and $data
not $can_have_body $data
Truth table66%2/3
not(A) and B
A= not $can_have_bodyB= $data
ABresult
0X0
100
111
97 confess "$method does not allow body. You may have problems with proxy. Consider removing it"
98 0 ▶ unless $conf{allow_get_with_body};
Branch: 1 branch
BranchTrueFalseTotal
unless $conf{"allow_get_with_body"} 0 0 0
99 0 $conf{data} = $data;
100 }
101 else {
102 14 $conf{data} = $data;
103 }
104  
105 17 return ( $self, $uri, %conf );
106 }
107  
108 1 ▶sub rest_put {
Subroutine rest_put: called CC=2 SCAR=7.1
Pod documented
109 1 my ( $self, $url, %conf ) = &_capture_args( 'PUT', @_ );
110  
111 1 $self->call_trigger( 'before_rest_put', { url => $url, conf => \%conf } );
112 $self->_rest_request(
113 $url,
114 1 ▶ code => ( exists $conf{is_fail} ? 400 : 202 ),
Branch: 1 branch
BranchTrueFalseTotal
exists $conf{'is_fail'} ? : 0 1 1
115 %conf,
116 method => 'PUT'
117 );
118 }
119  
120 1 ▶sub rest_head {
Subroutine rest_head: called CC=1 SCAR=0.0
Pod documented
121 1 my ( $self, $url, %conf ) = &_capture_args( 'HEAD', @_ );
122  
123 1 $self->call_trigger( 'before_rest_head', { url => $url, conf => \%conf } );
124 1 $self->_rest_request(
125 $url,
126 code => 200,
127 %conf,
128 method => 'HEAD'
129 );
130 }
131  
132 1 ▶sub rest_delete {
Subroutine rest_delete: called CC=1 SCAR=0.0
Pod documented
133 1 my ( $self, $url, %conf ) = &_capture_args( 'DELETE', @_ );
134  
135 1 $self->call_trigger( 'before_rest_delete', { url => $url, conf => \%conf } );
136 1 $self->_rest_request(
137 $url,
138 code => 204,
139 %conf,
140 method => 'DELETE'
141 );
142 }
143  
144 10 ▶sub rest_get {
Subroutine rest_get: called CC=1 SCAR=0.0
Pod documented
145 10 my ( $self, $url, %conf ) = &_capture_args( 'GET', @_ );
146  
147 7 $self->call_trigger( 'before_rest_get', { url => $url, conf => \%conf } );
148 7 $self->_rest_request(
149 $url,
150 code => 200,
151 %conf,
152 method => 'GET'
153 );
154 }
155  
156 7 ▶sub rest_post {
Subroutine rest_post: called CC=1 SCAR=0.0
Pod documented
157 7 my ( $self, $url, %conf ) = &_capture_args( 'POST', @_ );
158 7 $self->call_trigger( 'before_rest_post', { url => $url, conf => \%conf } );
159  
160 7 $self->_rest_request( $url, %conf, method => 'POST' );
161 }
162  
163 17 ▶sub _rest_request {
Subroutine _rest_request: called CC=51 SCAR=44.1
164 17 my ( $self, $url, %conf ) = @_;
165  
166 17 ▶ my $data = exists $conf{data} ? $conf{data} : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $conf{'data'} ? : 16 1 17
167  
168 17 ▶ $conf{automatic_load_item} = 1 unless exists $conf{automatic_load_item};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $conf{"automatic_load_item"} 17 0 17
169  
170 17 ▶ my $is_fail = exists $conf{is_fail} && $conf{is_fail};
Condition100%3/3
exists $conf{'is_fail'} && $conf{'is_fail'}
!ll&&!rl&&r
1412
MC/DC100%2/2
exists $conf{'is_fail'} && $conf{'is_fail'}
exists $conf{'is_fail'} $conf{'is_fail'}
Truth table100%3/3
A && B
A= exists $conf{'is_fail'}B= $conf{'is_fail'}
ABresult
0X0
100
111
171  
172 17 my $code = $conf{code};
173 17 ▶ $code ||= $is_fail ? 400 : 201;
Branch: 1 branch
BranchTrueFalseTotal
$is_fail ? : 0 4 4
Condition66%2/3
$code ||= $is_fail ? 400 : 201
l!l&&r!l&&!r
1340
MC/DC0%0/2
$code ||= $is_fail ? 400 : 201
$code $is_fail ? 400 : 201
Truth table66%2/3
A ||= B
A= $codeB= $is_fail ? 400 : 201
ABresult
1X1
011
000
174 17 $conf{code} = $code;
175  
176 17 my $uri = URI->new($url);
177 17 ▶ if ( $conf{params} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conf{"params"}) 6 11 17
178 0 ▶ my @old = ref $conf{params} eq 'ARRAY' ? @{ $conf{params} } : %{ $conf{params} };
Branch: 1 branch
BranchTrueFalseTotal
ref $conf{'params'} eq 'ARRAY' ? : 6 0 6
179 6 ▶ while ( my ( $k, $v ) = splice( @old, 0, 2 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@old, 0, 2)) 8 6 14
180 8 $uri->query_param_append( $k, $v );
181 }
182 }
183 17 $url = $uri->as_string;
184  
185 17 ▶ my $stashkey = exists $conf{stash} ? $conf{stash} : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $conf{'stash'} ? : 14 3 17
186  
187 17 ▶ my @headers = ( @{ $self->fixed_headers() }, @{ $conf{headers} || [] } );
Condition100%2/2
$conf{'headers'} || []
l!l
215
MC/DC100%1/1
$conf{'headers'} || []
$conf{'headers'}
Truth table100%2/2
A
A= $conf{'headers'}
Aresult
11
00
188  
189 17 my $req;
190  
191 17 ▶ if ( !exists $conf{files} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $conf{'files'}) { } 16 1 17
192 16 ▶ if ( defined $data ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data) { } 9 7 16
193 9 $req = POST $url, @headers, Content => $data;
194 }
195 else {
196 7 $req = GET $url, @headers;
197 }
198 }
199 else {
200 1 $conf{files}{$_} = [ $conf{files}{$_} ] for keys %{ $conf{files} };
201  
202 $req = POST $url, @headers,
203 'Content-Type' => 'form-data',
204 1 ▶ Content => [ ( $data && ref $data eq 'ARRAY' ? @$data : () ), %{ $conf{files} } ];
Branch: 1 branch
BranchTrueFalseTotal
$data && ref $data eq 'ARRAY' ? : 1 0 1
Condition33%1/3
$data and ref $data eq "ARRAY"
!ll&&!rl&&r
001
MC/DC0%0/2
$data and ref $data eq "ARRAY"
$data ref $data eq "ARRAY"
Truth table33%1/3
A and B
A= $dataB= ref $data eq "ARRAY"
ABresult
0X0
100
111
205 }
206  
207 $conf{process_request}->( { req => $req, conf => \%conf } )
208 17 ▶ if ( exists $conf{process_request} && ref $conf{process_request} eq 'CODE' );
Branch: 1 branch
BranchTrueFalseTotal
if exists $conf{"process_request"} and ref $conf{"process_request"} eq "CODE" 0 17 17
Condition33%1/3
exists $conf{"process_request"} and ref $conf{"process_request"} eq "CODE"
!ll&&!rl&&r
1700
MC/DC0%0/2
exists $conf{"process_request"} and ref $conf{"process_request"} eq "CODE"
exists $conf{"process_request"} ref $conf{"process_request"} eq "CODE"
Truth table33%1/3
A and B
A= exists $conf{"process_request"}B= ref $conf{"process_request"} eq "CODE"
ABresult
0X0
100
111
209  
210 17 $self->call_trigger( 'process_request', { req => $req, conf => \%conf } );
211  
212 # change to correct method.
213 17 $req->method( $conf{method} );
214  
215 17 my $res = eval { $self->do_request()->($req) };
216 17 ▶ confess "request died: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 17 17
217  
218 $conf{process_response}->( { req => $req, res => $res, conf => \%conf } )
219 17 ▶ if ( exists $conf{process_response} && ref $conf{process_response} eq 'CODE' );
Branch: 1 branch
BranchTrueFalseTotal
if exists $conf{"process_response"} and ref $conf{"process_response"} eq "CODE" 0 17 17
Condition33%1/3
exists $conf{"process_response"} and ref $conf{"process_response"} eq "CODE"
!ll&&!rl&&r
1700
MC/DC0%0/2
exists $conf{"process_response"} and ref $conf{"process_response"} eq "CODE"
exists $conf{"process_response"} ref $conf{"process_response"} eq "CODE"
Truth table33%1/3
A and B
A= exists $conf{"process_response"}B= ref $conf{"process_response"} eq "CODE"
ABresult
0X0
100
111
220  
221 17 $self->call_trigger( 'process_response', { req => $req, res => $res, conf => \%conf } );
222  
223 #is( $res->code, $code, $name . ' status code is ' . $code );
224 17 ▶ if ( !exists $conf{skip_response_tests} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $conf{"skip_response_tests"}) 17 0 17
225 17 ▶ confess 'response expected fail and it is successed' if $is_fail && $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
if $is_fail and $res->is_success 1 16 17
Condition100%3/3
$is_fail and $res->is_success
!ll&&!rl&&r
1511
MC/DC100%2/2
$is_fail and $res->is_success
$is_fail $res->is_success
Truth table100%3/3
A and B
A= $is_failB= $res->is_success
ABresult
0X0
100
111
226 16 ▶ confess 'response expected success and it is failed' if !$is_fail && !$res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $is_fail or $res->is_success 1 15 16
Condition100%3/3
$is_fail or $res->is_success
l!l&&r!l&&!r
1141
MC/DC100%2/2
$is_fail or $res->is_success
$is_fail $res->is_success
Truth table100%3/3
A or B
A= $is_failB= $res->is_success
ABresult
1X1
011
000
227  
228 15 ▶ confess 'response code [', $res->code, '] diverge expected [', $code, ']' if $code != $res->code;
Branch: 1 branch
BranchTrueFalseTotal
if $code != $res->code 1 14 15
229 }
230  
231 $conf{process_response_success}->( { req => $req, res => $res, conf => \%conf } )
232 14 ▶ if ( exists $conf{process_response_success} && ref $conf{process_response_success} eq 'CODE' );
Branch: 1 branch
BranchTrueFalseTotal
if exists $conf{"process_response_success"} and ref $conf{"process_response_success"} eq "CODE" 0 14 14
Condition33%1/3
exists $conf{"process_response_success"} and ref $conf{"process_response_success"} eq "CODE"
!ll&&!rl&&r
1400
MC/DC0%0/2
exists $conf{"process_response_success"} and ref $conf{"process_response_success"} eq "CODE"
exists $conf{"process_response_success"} ref $conf{"process_response_success"} eq "CODE"
Truth table33%1/3
A and B
A= exists $conf{"process_response_success"}B= ref $conf{"process_response_success"} eq "CODE"
ABresult
0X0
100
111
233  
234 14 $self->call_trigger( 'process_response_success', { req => $req, res => $res, conf => \%conf } );
235  
236 14 ▶ return '' if $code == 204;
Branch: 1 branch
BranchTrueFalseTotal
if $code == 204 1 13 14
237 13 ▶ return $res if exists $conf{method} && $conf{method} eq 'HEAD';
Branch: 1 branch
BranchTrueFalseTotal
if exists $conf{"method"} and $conf{"method"} eq "HEAD" 1 12 13
Condition66%2/3
exists $conf{"method"} and $conf{"method"} eq "HEAD"
!ll&&!rl&&r
0121
MC/DC50%1/2
exists $conf{"method"} and $conf{"method"} eq "HEAD"
exists $conf{"method"} $conf{"method"} eq "HEAD"
Truth table66%2/3
A and B
A= exists $conf{"method"}B= $conf{"method"} eq "HEAD"
ABresult
0X0
100
111
238  
239 12 my $obj = eval { $self->decode_response()->($res) };
240 12 ▶ confess("decode_response failed: $@") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 12 12
241  
242 $conf{response_decoded}->( { req => $req, res => $res, decoded => $obj, conf => \%conf } )
243 12 ▶ if ( exists $conf{response_decoded} && ref $conf{response_decoded} eq 'CODE' );
Branch: 1 branch
BranchTrueFalseTotal
if exists $conf{"response_decoded"} and ref $conf{"response_decoded"} eq "CODE" 0 12 12
Condition33%1/3
exists $conf{"response_decoded"} and ref $conf{"response_decoded"} eq "CODE"
!ll&&!rl&&r
1200
MC/DC0%0/2
exists $conf{"response_decoded"} and ref $conf{"response_decoded"} eq "CODE"
exists $conf{"response_decoded"} ref $conf{"response_decoded"} eq "CODE"
Truth table33%1/3
A and B
A= exists $conf{"response_decoded"}B= ref $conf{"response_decoded"} eq "CODE"
ABresult
0X0
100
111
244  
245 12 $self->call_trigger( 'response_decoded', { req => $req, res => $res, decoded => $obj, conf => \%conf } );
246  
247 12 ▶ if ($stashkey) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stashkey) 11 1 12
248 11 $self->stash->{$stashkey} = $obj;
249  
250 11 ▶ $self->stash( $stashkey . '.prepare_request' => $conf{prepare_request} ) if exists $conf{prepare_request};
Branch: 1 branch
BranchTrueFalseTotal
if exists $conf{"prepare_request"} 4 7 11
251  
252 11 ▶ if ( $code == 201 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code == 201) 4 7 11
253 4 ▶ $self->stash( $stashkey . '.id' => $obj->{id} ) if ref $obj eq 'HASH' && exists $obj->{id};
Branch: 1 branch
BranchTrueFalseTotal
if ref $obj eq "HASH" and exists $obj->{"id"} 4 0 4
Condition33%1/3
ref $obj eq "HASH" and exists $obj->{"id"}
!ll&&!rl&&r
004
MC/DC0%0/2
ref $obj eq "HASH" and exists $obj->{"id"}
ref $obj eq "HASH" exists $obj->{"id"}
Truth table33%1/3
A and B
A= ref $obj eq "HASH"B= exists $obj->{"id"}
ABresult
0X0
100
111
254  
255 4 my $item_url = $res->header('Location');
256  
257 4 ▶ if ( $item_url && $conf{automatic_load_item} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($item_url and $conf{'automatic_load_item'}) { } 4 0 4
elsif ($conf{'automatic_load_item'}) { } 0 0 0
Condition33%1/3
$item_url and $conf{"automatic_load_item"}
!ll&&!rl&&r
004
MC/DC0%0/2
$item_url and $conf{"automatic_load_item"}
$item_url $conf{"automatic_load_item"}
Truth table33%1/3
A and B
A= $item_urlB= $conf{"automatic_load_item"}
ABresult
0X0
100
111
258 4 $self->stash->{ $stashkey . '.url' } = $item_url;
259  
260 4 $self->rest_reload($stashkey);
261  
262 $conf{item_loaded}->( { stash => $stashkey, conf => \%conf } )
263 3 ▶ if ( exists $conf{item_loaded} && ref $conf{item_loaded} eq 'CODE' );
Branch: 1 branch
BranchTrueFalseTotal
if exists $conf{"item_loaded"} and ref $conf{"item_loaded"} eq "CODE" 0 3 3
Condition33%1/3
exists $conf{"item_loaded"} and ref $conf{"item_loaded"} eq "CODE"
!ll&&!rl&&r
300
MC/DC0%0/2
exists $conf{"item_loaded"} and ref $conf{"item_loaded"} eq "CODE"
exists $conf{"item_loaded"} ref $conf{"item_loaded"} eq "CODE"
Truth table33%1/3
A and B
A= exists $conf{"item_loaded"}B= ref $conf{"item_loaded"} eq "CODE"
ABresult
0X0
100
111
264  
265 3 $self->call_trigger( 'item_loaded', { stash => $stashkey, conf => \%conf } );
266 }
267 elsif ( $conf{automatic_load_item} ) {
268 0 confess 'requests with response code 201 should contain header Location';
269 }
270  
271 $conf{stash_added}->( { stash => $stashkey, conf => \%conf } )
272 3 ▶ if ( exists $conf{stash_added} && ref $conf{stash_added} eq 'CODE' );
Branch: 1 branch
BranchTrueFalseTotal
if exists $conf{"stash_added"} and ref $conf{"stash_added"} eq "CODE" 0 3 3
Condition33%1/3
exists $conf{"stash_added"} and ref $conf{"stash_added"} eq "CODE"
!ll&&!rl&&r
300
MC/DC0%0/2
exists $conf{"stash_added"} and ref $conf{"stash_added"} eq "CODE"
exists $conf{"stash_added"} ref $conf{"stash_added"} eq "CODE"
Truth table33%1/3
A and B
A= exists $conf{"stash_added"}B= ref $conf{"stash_added"} eq "CODE"
ABresult
0X0
100
111
273 3 $self->call_trigger( 'stash_added', { stash => $stashkey, conf => \%conf } );
274 }
275 }
276  
277 11 ▶ if ( $stashkey && exists $conf{list} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stashkey and exists $conf{"list"}) 2 9 11
Condition100%3/3
$stashkey and exists $conf{"list"}
!ll&&!rl&&r
182
MC/DC100%2/2
$stashkey and exists $conf{"list"}
$stashkey exists $conf{"list"}
Truth table100%3/3
A and B
A= $stashkeyB= exists $conf{"list"}
ABresult
0X0
100
111
278  
279 2 $self->stash( $stashkey . '.list-url' => $url );
280  
281 2 $self->rest_reload_list($stashkey);
282  
283 $conf{list_loaded}->( { stash => $stashkey, conf => \%conf } )
284 2 ▶ if ( exists $conf{list_loaded} && ref $conf{list_loaded} eq 'CODE' );
Branch: 1 branch
BranchTrueFalseTotal
if exists $conf{"list_loaded"} and ref $conf{"list_loaded"} eq "CODE" 0 2 2
Condition33%1/3
exists $conf{"list_loaded"} and ref $conf{"list_loaded"} eq "CODE"
!ll&&!rl&&r
200
MC/DC0%0/2
exists $conf{"list_loaded"} and ref $conf{"list_loaded"} eq "CODE"
exists $conf{"list_loaded"} ref $conf{"list_loaded"} eq "CODE"
Truth table33%1/3
A and B
A= exists $conf{"list_loaded"}B= ref $conf{"list_loaded"} eq "CODE"
ABresult
0X0
100
111
285  
286 2 $self->call_trigger( 'list_loaded', { stash => $stashkey, conf => \%conf } );
287  
288 }
289  
290 11 return $obj;
291 }
292  
293 6 ▶sub rest_reload {
Subroutine rest_reload: called CC=12 SCAR=25.3
Pod documented
294 6 my $self = shift;
295 6 my $stashkey = shift;
296  
297 6 my %conf = @_;
298  
299 6 ▶ my $code = exists $conf{code} ? $conf{code} : 200;
Branch: 1 branch
BranchTrueFalseTotal
exists $conf{'code'} ? : 1 5 6
300 6 $conf{code} = $code;
301  
302 6 ▶ my @headers = ( @{ $self->fixed_headers() }, @{ $conf{headers} || [] } );
Condition50%1/2
$conf{'headers'} || []
l!l
06
MC/DC0%0/1
$conf{'headers'} || []
$conf{'headers'}
Truth table50%1/2
A
A= $conf{'headers'}
Aresult
11
00
303 6 my $item_url = $self->stash->{ $stashkey . '.url' };
304  
305 6 ▶ confess "can't stash $stashkey.url is not valid" unless $item_url;
Branch: 1 branch
BranchTrueFalseTotal
unless $item_url 0 6 6
306  
307 my $prepare_request =
308 exists $self->stash->{ $stashkey . '.prepare_request' }
309 6 ▶ ? $self->stash->{ $stashkey . '.prepare_request' }
Branch: 1 branch
BranchTrueFalseTotal
exists $self->stash->{$stashkey . '.prepare_request'} ? : 6 0 6
310 : undef;
311  
312 6 ▶ confess 'prepare_request must be a coderef'
Branch: 1 branch
BranchTrueFalseTotal
if $prepare_request and ref $prepare_request ne "CODE" 1 5 6
Condition66%2/3
$prepare_request and ref $prepare_request ne "CODE"
!ll&&!rl&&r
051
MC/DC50%1/2
$prepare_request and ref $prepare_request ne "CODE"
$prepare_request ref $prepare_request ne "CODE"
Truth table66%2/3
A and B
A= $prepare_requestB= ref $prepare_request ne "CODE"
ABresult
0X0
100
111
313 if $prepare_request && ref $prepare_request ne 'CODE';
314  
315 5 my $req = GET $item_url, @headers;
316 5 $req->method('GET');
317 5 ▶ $prepare_request->($req) if $prepare_request;
Branch: 1 branch
BranchTrueFalseTotal
if $prepare_request 5 0 5
318  
319 5 $self->call_trigger( 'process_request', { req => $req, conf => \%conf } );
320 5 my $res = $self->do_request()->($req);
321  
322 5 $self->call_trigger( 'process_response', { req => $req, res => $res, conf => \%conf } );
323  
324 5 ▶ confess 'request code diverge expected' if $code != $res->code;
Branch: 1 branch
BranchTrueFalseTotal
if $code != $res->code 0 5 5
325  
326 5 $self->call_trigger( 'process_response_success', { req => $req, res => $res, conf => \%conf } );
327  
328 5 my $obj;
329 5 ▶ if ( $res->code == 200 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($res->code == 200) { } 4 1 5
elsif ($res->code == 404) { } 1 0 1
330 4 my $obj = eval { $self->decode_response()->($res) };
331 4 ▶ confess("decode_response failed: $@") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 4 4
332  
333 4 $self->call_trigger( 'response_decoded', { req => $req, res => $res, decoded => $obj, conf => \%conf } );
334  
335 4 $self->stash( $stashkey . '.get' => $obj );
336 }
337 elsif ( $res->code == 404 ) {
338  
339 1 $self->call_trigger( 'stash_removed', { stash => $stashkey, conf => \%conf } );
340  
341 # $self->stash->{ $stashkey . '.get' };
342 1 delete $self->stash->{ $stashkey . '.id' };
343 1 delete $self->stash->{ $stashkey . '.url' };
344 1 delete $self->stash->{$stashkey};
345  
346 }
347 else {
348 0 confess 'response code ' . $res->code . ' is not valid for rest_reload';
349 }
350  
351 5 return $obj;
352 }
353  
354 3 ▶sub rest_reload_list {
Subroutine rest_reload_list: called CC=11 SCAR=25.5
Pod documented
355 3 my $self = shift;
356 3 my $stashkey = shift;
357  
358 3 my %conf = @_;
359  
360 3 ▶ my $code = exists $conf{code} ? $conf{code} : 200;
Branch: 1 branch
BranchTrueFalseTotal
exists $conf{'code'} ? : 0 3 3
361 3 $conf{code} = $code;
362  
363 3 ▶ my @headers = ( @{ $self->fixed_headers() }, @{ $conf{headers} || [] } );
Condition50%1/2
$conf{'headers'} || []
l!l
03
MC/DC0%0/1
$conf{'headers'} || []
$conf{'headers'}
Truth table50%1/2
A
A= $conf{'headers'}
Aresult
11
00
364 3 my $item_url = $self->stash->{ $stashkey . '.list-url' };
365  
366 3 ▶ confess "can't stash $stashkey.list-url is not valid" unless $item_url;
Branch: 1 branch
BranchTrueFalseTotal
unless $item_url 0 3 3
367  
368 my $prepare_request =
369 exists $self->stash->{ $stashkey . '.prepare_request' }
370 3 ▶ ? $self->stash->{ $stashkey . '.prepare_request' }
Branch: 1 branch
BranchTrueFalseTotal
exists $self->stash->{$stashkey . '.prepare_request'} ? : 3 0 3
371 : undef;
372 3 ▶ confess 'prepare_request must be a coderef'
Branch: 1 branch
BranchTrueFalseTotal
if $prepare_request and ref $prepare_request ne "CODE" 0 3 3
Condition33%1/3
$prepare_request and ref $prepare_request ne "CODE"
!ll&&!rl&&r
030
MC/DC0%0/2
$prepare_request and ref $prepare_request ne "CODE"
$prepare_request ref $prepare_request ne "CODE"
Truth table33%1/3
A and B
A= $prepare_requestB= ref $prepare_request ne "CODE"
ABresult
0X0
100
111
373 if $prepare_request && ref $prepare_request ne 'CODE';
374  
375 3 my $req = GET $item_url, @headers;
376 3 ▶ $prepare_request->($req) if $prepare_request;
Branch: 1 branch
BranchTrueFalseTotal
if $prepare_request 3 0 3
377  
378 3 $self->call_trigger( 'process_request', { req => $req, conf => \%conf } );
379  
380 3 my $res = $self->do_request()->($req);
381  
382 3 $self->call_trigger( 'process_response', { req => $req, res => $res, conf => \%conf } );
383  
384 3 ▶ confess 'request code diverge expected' if $code != $res->code;
Branch: 1 branch
BranchTrueFalseTotal
if $code != $res->code 0 3 3
385  
386 3 $self->call_trigger( 'process_response_success', { req => $req, res => $res, conf => \%conf } );
387  
388 3 my $obj;
389 3 ▶ if ( $res->code == 200 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->code == 200) { } 3 0 3
390 3 my $obj = eval { $self->decode_response()->($res) };
391 3 ▶ confess("decode_response failed: $@") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 3 3
392  
393 3 $self->call_trigger( 'response_decoded', { req => $req, res => $res, decoded => $obj, conf => \%conf } );
394  
395 3 $self->stash( $stashkey . '.list' => $obj );
396 }
397 else {
398 0 confess 'response code ' . $res->code . ' is not valid for rest_reload';
399 }
400  
401 3 return $obj;
402 }
403  
404 10 ▶sub stash_ctx {
Subroutine stash_ctx: called CC=1 SCAR=0.0
Pod documented
405 10 my ( $self, $staname, $sub ) = @_;
406  
407 10 $self->call_trigger( 'before_stash_ctx', { stash => $staname } );
408  
409 10 my @ret = $sub->( $self->stash->{$staname} );
410  
411 10 $self->call_trigger( 'after_stash_ctx', { stash => $staname, results => \@ret } );
412 10 return @ret;
413 }
414  
415 1 1;
416  
417 __END__
↑ Summary