Terse.pm

filter: Statementstmt 26.8% 67 / 250 Branchbran 10.8% 15 / 138 Conditioncond 10.8% 10 / 92 MC/DCmcdc 7.6% 4 / 52 Subroutinesub 43.2% 16 / 37 Podpod 81.8% 9 / 11 totaltotal 20.8% 121 / 580 CC 108 SCAR 49.4
CC
108
Cov
19%
run
650.0
_build_terse
110.0
logError
72.0
CRAP 139.1
« Terse/Controller.pm ↑ Summary Terse/App.pm »
1 package Terse;
2 3 our $VERSION = '0.122';
3 3 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Plack::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Plack::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Scalar::Util qw/reftype/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Time::HiRes qw(gettimeofday);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Terse::WebSocket;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Want qw/want/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use Digest::SHA;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 3 our ($JSON, %PRIVATE);
17 BEGIN {
18 3 ▶ $JSON = JSON->new->utf8->canonical(1)->allow_blessed->convert_blessed;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 %PRIVATE = (
20 3 map { $_ => 1 }
21 qw/new run logger logInfo logError websocket delayed_response build_terse content_type raiseError graft pretty serialize DESTROY TO_JSON AUTOLOAD to_app/
22 );
23 }
24  
25 sub new {
26 4 ▶ my ($pkg, %args) = @_;
Subroutine new: called CC=5 SCAR=21.9
Pod documented
27
28 4 ▶ $pkg = ref $pkg if ref $pkg;
Branch: 1 branch
BranchTrueFalseTotal
if ref $pkg 0 4 4
29
30 4 ▶ if (delete $args{private}) {
Branch: 1 branch
BranchTrueFalseTotal
if (delete $args{"private"}) 0 4 4
31 0 for my $key (keys %args) {
32 0 ▶ if ($key !~ m/^_/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($key =~ /^_/) 0 0 0
33 0 $args{"_$key"} = delete $args{$key};
34 }
35 }
36 }
37  
38 4 return bless \%args, $pkg;
39 }
40  
41 sub run {
42 0 ▶ my ($pkg, %args) = @_;
Subroutine run: not called CC=25 SCAR=64.8
Pod documented
43  
44 0 my $j = $pkg->new(
45 private => 1,
46 login => 'login',
47 logout => 'logout',
48 auth => 'auth',
49 insecure_session => 0,
50 content_type => 'application/json',
51 request_class => 'Plack::Request',
52 websocket_class => 'Terse::WebSocket',
53 sock => 'psgix.io',
54 stream_check => 'psgi.streaming',
55 %args
56 );
57  
58 0 $j->_build_terse();
59
60 0 $j->request = $j->{_request_class}->new($args{plack_env});
61 0 $j->response = $pkg->new(
62 authenticated => \0,
63 error => \0,
64 errors => [],
65 );
66  
67 0 my $content_type = $j->request->content_type;
68 0 ▶ if ($content_type && $content_type =~ m/application\/json/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content_type and $content_type =~ m[application/json]) { } 0 0 0
Condition0%0/3
$content_type and $content_type =~ m[application/json]
!ll&&!rl&&r
000
MC/DC0%0/2
$content_type and $content_type =~ m[application/json]
$content_type $content_type =~ m[application/json]
Truth table0%0/3
A and B
A= $content_typeB= $content_type =~ m[application/json]
ABresult
0X0
100
111
69 0 ▶ $j->graft('params', $j->request->raw_body || "{}");
Condition0%0/2
$j->request->raw_body || '{}'
l!l
00
MC/DC0%0/1
$j->request->raw_body || '{}'
$j->request->raw_body
Truth table0%0/2
A
A= $j->request->raw_body
Aresult
11
00
70 } else {
71 0 ▶ $j->params = {%{$j->request->parameters || {}}};
Branch: 1 branch
BranchTrueFalseTotal
$j->request->parameters or {} 0 0 0
72 }
73  
74 0 ▶ unless ((reftype($j->params) || "") eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless ((builtin::reftype($j->params) || "") eq "HASH") 0 0 0
Condition0%0/2
builtin::reftype($j->params) || ''
l!l
00
MC/DC0%0/1
builtin::reftype($j->params) || ''
builtin::reftype($j->params)
Truth table0%0/2
A
A= builtin::reftype($j->params)
Aresult
11
00
75 0 $j->response->raiseError('Invalid parameters', 400);
76 0 return $j->_response($j->response);
77 }
78
79 0 $j->sid = $j->request->cookies->{sid};
80
81 0 ▶ unless ($j->sid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($j->sid) 0 0 0
82 0 my $h = Digest::SHA->new(256);
83 0 my @us = gettimeofday;
84 0 push @us, map { $j->request->env->{$_} } grep {
85 0 $_ =~ /^HTTP(?:_|$)/;
86 0 } keys %{ $j->request->env };
87 0 $h->add(@us);
88 0 $j->sid = $h->hexdigest;
89 }
90  
91 $j->sid = {
92 value => $j->is_logout ? "" : $j->sid,
93 path => $j->request->uri,
94 secure => !$j->{_insecure_session},
95 0 ▶ };
Branch: 1 branch
BranchTrueFalseTotal
$j->is_logout ? : 0 0 0
96  
97 0 my $auth = $j->{_auth};
98
99 0 my ($session) = $j->_dispatch($auth, $pkg->new());
100
101 0 my $req = $j->params->req;
102 0 ▶ $req =~ /^([a-z][0-9a-zA-Z_]{1,31})$/; $req = $1 // '';
Condition0%0/2
$1 // ""
l!l
00
MC/DC0%0/1
$1 // ""
$1
Truth table0%0/2
A
A= $1
Aresult
11
00
103 0 ▶ $req = $j->{_application}->preprocess_req($req, $j) if $j->{_application}->can('preprocess_req');
Branch: 1 branch
BranchTrueFalseTotal
if $j->{"_application"}->can("preprocess_req") 0 0 0
104 0 ▶ if (!$req || !$session || $PRIVATE{$req}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $req && $session or $Terse::PRIVATE{$req}) 0 0 0
Condition0%0/6
not $req && $session or $Terse::PRIVATE{$req}
l!l&&r!l&&!r
000
$req && $session
!ll&&!rl&&r
000
MC/DC0%0/3
not $req && $session or $Terse::PRIVATE{$req}
$req $session $Terse::PRIVATE{$req}
Truth table0%0/4
not(A && B) or C
A= $reqB= $sessionC= $Terse::PRIVATE{$req}
ABCresult
0XX1
10X1
1111
1100
105 0 $j->response->raiseError('Invalid request', 400);
106 0 return $j->_response($j->response);
107 }
108  
109 0 $j->req = $req;
110 0 $j->response->authenticated = \1;
111 0 $j->session = $session;
112  
113 0 ▶ $j->sid->expires = (ref $j->session && $j->session->expires) || (time + 24 * 60 * 60)
Branch: 1 branch
BranchTrueFalseTotal
unless $j->sid->expires 0 0 0
Condition0%0/6
ref $j->session && $j->session->expires || time + 86400
l!l&&r!l&&!r
000
ref $j->session and $j->session->expires
!ll&&!rl&&r
000
MC/DC0%0/3
ref $j->session && $j->session->expires || time + 86400
ref $j->session $j->session->expires time + 86400
Truth table0%0/5
A and B || C
A= ref $j->sessionB= $j->session->expiresC= time + 86400
ABCresult
11X1
0X11
1011
0X00
1000
114 if (!$j->sid->expires);
115  
116 ($j->is_login, $j->is_logout) = (
117 $j->{_login} eq $req,
118 0 $j->{_logout} eq $req
119 );
120  
121 0 my ($out) = $j->_dispatch($req);
122
123 0 ▶ return $j->_response($j->response) if $j->response->error;
Branch: 1 branch
BranchTrueFalseTotal
if $j->response->error 0 0 0
124  
125 0 ▶ $j->session = $out if ( $j->is_login || $j->is_logout );
Branch: 1 branch
BranchTrueFalseTotal
if $j->is_login or $j->is_logout 0 0 0
Condition0%0/3
$j->is_login or $j->is_logout
l!l&&r!l&&!r
000
MC/DC0%0/2
$j->is_login or $j->is_logout
$j->is_login $j->is_logout
Truth table0%0/3
A or B
A= $j->is_loginB= $j->is_logout
ABresult
1X1
011
000
126  
127 0 ▶ ($j->session) = $j->_dispatch($auth, $j->session) if $j->response->authenticated;
Branch: 1 branch
BranchTrueFalseTotal
if $j->response->authenticated 0 0 0
128  
129 0 ▶ if ((!$j->response->authenticated || !$j->session) && !($j->is_login || $j->is_logout)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($j->response->authenticated and $j->session or ($j->is_login or $j->is_logout)) 0 0 0
Condition0%0/9
$j->response->authenticated and $j->session or ($j->is_login or $j->is_logout)
l!l&&r!l&&!r
000
$j->response->authenticated and $j->session
!ll&&!rl&&r
000
$j->is_login or $j->is_logout
l!l&&r!l&&!r
000
MC/DC0%0/4
$j->response->authenticated and $j->session or ($j->is_login or $j->is_logout)
$j->response->authenticated $j->session $j->is_login $j->is_logout
Truth table0%0/7
A and B or C or D
A= $j->response->authenticatedB= $j->sessionC= $j->is_loginD= $j->is_logout
ABCDresult
11XX1
0X1X1
0X011
101X1
10011
0X000
10000
130 0 $j->response->raiseError('Unauthenticated during the request', 400);
131 0 return $j->_response($j->response);
132 }
133
134 0 return $j->_response($j->response, $j->sid, $j->content_type);
135 }
136  
137 sub to_app {
138 0 ▶ my ($self, $new, $run) = @_;
Subroutine to_app: not called CC=2 SCAR=17.9
Pod undocumented
139 0 ▶ my $app = $self->new($new ? %{ $new } : ());
Branch: 1 branch
BranchTrueFalseTotal
$new ? : 0 0 0
140 return sub {
141 0 ▶ my ($env) = (shift);
Subroutine __ANON__: not called CC=2 SCAR=17.9
142 Terse->run(
143 plack_env => $env,
144 application => $app,
145 0 ▶ ($env->{'psgix.logger'} ? (logger => $env->{'psgix.logger'}) : ()),
Branch: 1 branch
BranchTrueFalseTotal
$env->{'psgix.logger'} ? : 0 0 0
146 );
147 0 };
148 };
149
150 sub logger {
151 0 ▶ my ($self, $logger) = @_;
Subroutine logger: not called CC=2 SCAR=17.9
Pod documented
152 0 ▶ $self->{_logger} = $logger if ($logger);
Branch: 1 branch
BranchTrueFalseTotal
if $logger 0 0 0
153 0 return $self->{_logger};
154 }
155  
156 sub logError {
157 0 ▶ my ($self, $message, $status, $no_response) = @_;
Subroutine logError: not called CC=8 SCAR=42.8
Pod documented
158 $self->{_application}
159 0 ▶ ? $self->response->raiseError($message, $status)
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_application'} ? : 0 0 0
160 : $self->raiseError($message, $status);
161 0 ▶ $message = { message => $message } if (!ref $message);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $message 0 0 0
162 $message = $self->{_application}->_logError($message, $status)
163 0 ▶ if ($self->{_application} && $self->{_application}->can('_logError'));
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_application"} and $self->{"_application"}->can("_logError") 0 0 0
Condition0%0/3
$self->{"_application"} and $self->{"_application"}->can("_logError")
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"_application"} and $self->{"_application"}->can("_logError")
$self->{"_application"} $self->{"_application"}->can("_logError")
Truth table0%0/3
A and B
A= $self->{"_application"}B= $self->{"_application"}->can("_logError")
ABresult
0X0
100
111
164 ref $self->{_logger} eq 'CODE'
165 ? $self->{_logger}->('error', $message)
166 : $self->{_logger}->error($message)
167 0 ▶ if $self->{_logger};
Branch: 2 branches
BranchTrueFalseTotal
if $self->{"_logger"} 0 0 0
ref $self->{'_logger'} eq 'CODE' ? : 0 0 0
168 0 ▶ $self->response->no_response = 1 if $no_response;
Branch: 1 branch
BranchTrueFalseTotal
if $no_response 0 0 0
169 0 return $self;
170 }
171  
172 sub logInfo {
173 0 ▶ my ($self, $message) = @_;
Subroutine logInfo: not called CC=6 SCAR=37.4
Pod documented
174 0 ▶ $message = { message => $message } if (!ref $message);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $message 0 0 0
175 $message = $self->{_application}->_logInfo($message)
176 0 ▶ if ($self->{_application} && $self->{_application}->can('_logInfo'));
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_application"} and $self->{"_application"}->can("_logInfo") 0 0 0
Condition0%0/3
$self->{"_application"} and $self->{"_application"}->can("_logInfo")
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"_application"} and $self->{"_application"}->can("_logInfo")
$self->{"_application"} $self->{"_application"}->can("_logInfo")
Truth table0%0/3
A and B
A= $self->{"_application"}B= $self->{"_application"}->can("_logInfo")
ABresult
0X0
100
111
177 ref $self->{_logger} eq 'CODE'
178 ? $self->{_logger}->('info', $message)
179 : $self->{_logger}->info($message)
180 0 ▶ if $self->{_logger};
Branch: 2 branches
BranchTrueFalseTotal
if $self->{"_logger"} 0 0 0
ref $self->{'_logger'} eq 'CODE' ? : 0 0 0
181 0 return $self;
182 }
183  
184 sub raiseError {
185 0 ▶ my ($self, $message, $code) = @_;
Subroutine raiseError: not called CC=5 SCAR=34.0
Pod documented
186 0 ▶ return $self->response->raiseError($message, $code) if $self->{_application};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_application"} 0 0 0
187 0 $self->{error} = \1;
188 0 ▶ if ((reftype($message) || '') eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ((builtin::reftype($message) || '') eq 'ARRAY') { } 0 0 0
Condition0%0/2
builtin::reftype($message) || ''
l!l
00
MC/DC0%0/1
builtin::reftype($message) || ''
builtin::reftype($message)
Truth table0%0/2
A
A= builtin::reftype($message)
Aresult
11
00
189 0 push @{$self->{errors}}, @{$message};
190 } else {
191 0 push @{$self->{errors}}, $message;
192 }
193 0 ▶ $self->{status_code} = $code if ($code);
Branch: 1 branch
BranchTrueFalseTotal
if $code 0 0 0
194 0 return $self;
195 }
196  
197 sub graft {
198 0 ▶ my ($self, $name, $json) = @_;
Subroutine graft: not called CC=2 SCAR=17.9
Pod documented
199  
200 0 $self->{$name} = eval {
201 0 $JSON->decode($json);
202 };
203  
204 0 ▶ return 0 if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
205  
206 0 return $self->_bless_tree($self->{$name});
207 }
208  
209 0 ▶sub pretty { $_[0]->{_pretty} = 1; $_[0]; }
Subroutine pretty: not called CC=1 SCAR=6.9
Pod documented
210  
211 sub serialize {
212 0 ▶ my ($self, $die) = @_;
Subroutine serialize: not called CC=5 SCAR=34.0
Pod documented
213 0 ▶ my $pretty = !!(reftype $self eq 'HASH' && $self->{_pretty});
Condition0%0/3
builtin::reftype($self) eq 'HASH' && $self->{'_pretty'}
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::reftype($self) eq 'HASH' && $self->{'_pretty'}
builtin::reftype($self) eq 'HASH' $self->{'_pretty'}
Truth table0%0/3
A && B
A= builtin::reftype($self) eq 'HASH'B= $self->{'_pretty'}
ABresult
0X0
100
111
214 0 my $out = eval {
215 0 $JSON->pretty($pretty)->encode($self);
216 };
217 0 ▶ die $@ if ($@ && $die);
Branch: 1 branch
BranchTrueFalseTotal
if $@ and $die 0 0 0
Condition0%0/3
$@ and $die
!ll&&!rl&&r
000
MC/DC0%0/2
$@ and $die
$@ $die
Truth table0%0/3
A and B
A= $@B= $die
ABresult
0X0
100
111
218 0 ▶ return $out || $@;
Condition0%0/3
$out || $@
l!l&&r!l&&!r
000
MC/DC0%0/2
$out || $@
$out $@
Truth table0%0/3
A || B
A= $outB= $@
ABresult
1X1
011
000
219 }
220  
221 sub _build_terse {
222 0 ▶ my ($t) = @_;
Subroutine _build_terse: not called CC=10 SCAR=47.0
223  
224 0 ▶ if (! $t->{_application}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($t->{"_application"}) 0 0 0
225 0 $t->response->raiseError('No application passed to run', 500);
226 0 return $t->_response($t->response);
227 }
228  
229 $t->{websocket} = sub {
230 0 ▶ my ($self, %args) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
231 0 my $websocket = $t->{_websocket_class}->new($self);
232 0 ▶ if (!ref $websocket) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $websocket) 0 0 0
233 0 $args{error}->($t, $websocket);
234 0 return;
235 }
236 $t->{_delayed_response} = sub {
237 0 ▶ my $responder = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
238 0 $websocket->start($t, \%args, $responder);
239 0 };
240 0 return $websocket;
241 0 ▶ } unless $t->{websocket} || !$t->{_websocket_class};
Branch: 1 branch
BranchTrueFalseTotal
unless $t->{"websocket"} or not $t->{"_websocket_class"} 0 0 0
Condition0%0/3
$t->{"websocket"} or not $t->{"_websocket_class"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$t->{"websocket"} or not $t->{"_websocket_class"}
$t->{"websocket"} not $t->{"_websocket_class"}
Truth table0%0/3
A or not(B)
A= $t->{"websocket"}B= not $t->{"_websocket_class"}
ABresult
1X1
011
000
242  
243 $t->{delayed_response} = sub {
244 0 ▶ my ($self, $response, $sid, $ct, $status) = @_;
Subroutine __ANON__: not called CC=5 SCAR=34.0
245 0 ▶ $sid ||= $self->sid;
Condition0%0/3
$sid ||= $self->sid
l!l&&r!l&&!r
000
MC/DC0%0/2
$sid ||= $self->sid
$sid $self->sid
Truth table0%0/3
A ||= B
A= $sidB= $self->sid
ABresult
1X1
011
000
246 0 ▶ $status ||= 200;
Condition0%0/2
$status ||= 200
l!l
00
MC/DC0%0/1
$status ||= 200
$status
Truth table0%0/2
A
A= $status
Aresult
11
00
247 0 ▶ $ct ||= 'application/json';
Condition0%0/2
$ct ||= "application/json"
l!l
00
MC/DC0%0/1
$ct ||= "application/json"
$ct
Truth table0%0/2
A
A= $ct
Aresult
11
00
248 return $self->{_application}->delayed_response_handle(
249 $self, $response, $sid, $ct, $status
250 0 ▶ ) if $self->{_application_has_delayed_response_handler};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_application_has_delayed_response_handler"} 0 0 0
251 $self->{_delayed_response} = sub {
252 0 ▶ my $responder = shift;
Subroutine __ANON__: not called CC=6 SCAR=37.4
253 0 my $res = $self->_build_response($sid, $ct, $status);
254 0 $res = [splice @{$res->finalize}, 0, 2];
255 0 my $writer = $responder->($res);
256 0 $response = eval { $response->($writer); };
257 0 ▶ if ($@ || $self->response->error) {
Branch: 2 branches
BranchTrueFalseTotal
if ($@ or $self->response->error) { } 0 0 0
elsif ($response) { } 0 0 0
Condition0%0/3
$@ or $self->response->error
l!l&&r!l&&!r
000
MC/DC0%0/2
$@ or $self->response->error
$@ $self->response->error
Truth table0%0/3
A or B
A= $@B= $self->response->error
ABresult
1X1
011
000
258 0 ▶ $res->[0] = $self->response->status_code || 500;
Condition0%0/2
$self->response->status_code || 500
l!l
00
MC/DC0%0/1
$self->response->status_code || 500
$self->response->status_code
Truth table0%0/2
A
A= $self->response->status_code
Aresult
11
00
259 0 ▶ $self->raiseError($@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
260 0 push @{$res}, [$self->response->serialize];
261 0 return $responder->($res);
262 }
263 elsif ($response) {
264 0 $writer->write($response->serialize);
265 }
266 0 $writer->close;
267 0 };
268 0 $self;
269 0 ▶ } unless $t->{delayed_response};
Branch: 1 branch
BranchTrueFalseTotal
unless $t->{"delayed_response"} 0 0 0
270  
271 0 ▶ $t->{_application}->build_terse($t) if $t->{_application}->can('build_terse');
Branch: 1 branch
BranchTrueFalseTotal
if $t->{"_application"}->can("build_terse") 0 0 0
272 0 $t->{_application_has_dispatcher} = !! $t->{_application}->can('dispatch');
273 0 $t->{_application_has_response_handler} = !! $t->{_application}->can('response_handle');
274 0 $t->{_application_has_delayed_response_handler} = !! $t->{_application}->can('delayed_response_handle');
275  
276 $t->{_build_response} = sub {
277 0 ▶ my ($self, $sid, $content, $status) = @_;
Subroutine __ANON__: not called CC=5 SCAR=34.0
278 0 ▶ my $res = $self->request->new_response($self->response->{status_code} ||= $status);
Condition0%0/3
$self->response->{"status_code"} ||= $status
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->response->{"status_code"} ||= $status
$self->response->{"status_code"} $status
Truth table0%0/3
A ||= B
A= $self->response->{"status_code"}B= $status
ABresult
1X1
011
000
279 0 ▶ $res->cookies($self->cookies) if $self->cookies;
Branch: 1 branch
BranchTrueFalseTotal
if $self->cookies 0 0 0
280 0 ▶ $res->headers({%{$self->headers}}) if $self->headers;
Branch: 1 branch
BranchTrueFalseTotal
if $self->headers 0 0 0
281 0 ▶ $res->cookies->{sid} = {%{$sid}} if $sid;
Branch: 1 branch
BranchTrueFalseTotal
if $sid 0 0 0
282 0 $res->content_type($content);
283 0 return $res;
284 0 ▶ } unless $t->{_build_response};
Branch: 1 branch
BranchTrueFalseTotal
unless $t->{"_build_response"} 0 0 0
285  
286 $t->{content_type} = sub {
287 0 ▶ $_[0]->{_content_type} = $_[1] if $_[1];
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] 0 0 0
288 0 return $_[0]->{_content_type};
289 0 ▶ } unless $t->{content_type};
Branch: 1 branch
BranchTrueFalseTotal
unless $t->{"content_type"} 0 0 0
290  
291 $t->{_response} = sub {
292 0 ▶ my ($self, $response_body, $sid, $ct, $status) = @_;
Subroutine __ANON__: not called CC=5 SCAR=34.0
293 0 ▶ return $self->{_application}->response_handle(@_) if $self->{_application_has_response_handler};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_application_has_response_handler"} 0 0 0
294 0 ▶ $ct ||= 'application/json';
Condition0%0/2
$ct ||= "application/json"
l!l
00
MC/DC0%0/1
$ct ||= "application/json"
$ct
Truth table0%0/2
A
A= $ct
Aresult
11
00
295 0 my $res = $self->{_delayed_response};
296 0 ▶ return $res if ($res);
Branch: 1 branch
BranchTrueFalseTotal
if $res 0 0 0
297 0 ▶ $res = $self->_build_response($sid, $ct, $status || 200);
Condition0%0/2
$status || 200
l!l
00
MC/DC0%0/1
$status || 200
$status
Truth table0%0/2
A
A= $status
Aresult
11
00
298 0 $res->body($response_body->serialize());
299 0 return $res->finalize;
300 0 ▶ } unless $t->{_response};
Branch: 1 branch
BranchTrueFalseTotal
unless $t->{"_response"} 0 0 0
301  
302 $t->{_dispatch} = sub {
303 0 ▶ my ($self, $method, @params) = @_;
Subroutine __ANON__: not called CC=4 SCAR=30.0
304 my @out = $self->{_application_has_dispatcher} ? eval {
305 0 $self->{_application}->dispatch($method, $self, @params)
306 0 ▶ } : eval {
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_application_has_dispatcher'} ? : 0 0 0
307 0 ▶ unless ($self->{_application}->can($method)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_application"}->can($method)) 0 0 0
308 0 $self->response->raiseError('Invalid request - ' . $method, 400);
309 0 return;
310 }
311 0 $self->{_application}->$method($self, @params);
312 };
313 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
314 0 $self->response->raiseError(['Error while dispatching the request', $@], 400);
315 0 return;
316 }
317 0 return @out;
318 0 ▶ } unless $t->{_dispatch};
Branch: 1 branch
BranchTrueFalseTotal
unless $t->{"_dispatch"} 0 0 0
319
320 0 return $t;
321 }
322  
323 sub _bless_tree {
324 6 ▶ my ($self, $node) = @_;
Subroutine _bless_tree: called CC=8 SCAR=22.5
325 6 my $refnode = ref $node;
326 6 ▶ return unless $refnode eq 'HASH' || $refnode eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless $refnode eq "HASH" or $refnode eq "ARRAY" 2 4 6
Condition66%2/3
$refnode eq "HASH" or $refnode eq "ARRAY"
l!l&&r!l&&!r
402
MC/DC50%1/2
$refnode eq "HASH" or $refnode eq "ARRAY"
$refnode eq "HASH" $refnode eq "ARRAY"
Truth table66%2/3
A or B
A= $refnode eq "HASH"B= $refnode eq "ARRAY"
ABresult
1X1
011
000
327 4 ▶ if ($refnode eq 'HASH'){
Branch: 1 branch
BranchTrueFalseTotal
if ($refnode eq "HASH") 4 0 4
328 4 ▶ bless $node, $node->{_inherit} ? ref $self : __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
$node->{'_inherit'} ? : 0 4 4
329 4 $self->_bless_tree($node->{$_}) for keys %$node;
330 }
331 4 ▶ if ($refnode eq 'ARRAY'){
Branch: 1 branch
BranchTrueFalseTotal
if ($refnode eq "ARRAY") 0 4 4
332 0 bless $node, ref $self;
333 0 $self->_bless_tree($_) for @$node;
334 }
335 4 $node;
336 }
337  
338 sub TO_JSON {
339 0 ▶ my $self = shift;
Subroutine TO_JSON: not called CC=7 SCAR=40.3
Pod undocumented
340 0 ▶ return [@$self] if reftype $self eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if builtin::reftype($self) eq "ARRAY" 0 0 0
341 0 ▶ return 'cannot stringify application object' if $self->{_application};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_application"} 0 0 0
342 0 my $output = {};
343 0 my $nodebug = ! $self->{_debug};
344 0 for(keys %$self){
345 0 my $skip;
346 0 ▶ $skip++ if $_ =~ /^_/ && $nodebug;
Branch: 1 branch
BranchTrueFalseTotal
if $_ =~ /^_/ and $nodebug 0 0 0
Condition0%0/3
$_ =~ /^_/ and $nodebug
!ll&&!rl&&r
000
MC/DC0%0/2
$_ =~ /^_/ and $nodebug
$_ =~ /^_/ $nodebug
Truth table0%0/3
A and B
A= $_ =~ /^_/B= $nodebug
ABresult
0X0
100
111
347 0 ▶ next if $skip;
Branch: 1 branch
BranchTrueFalseTotal
if $skip 0 0 0
348 0 $output->{$_} = $self->{$_};
349 }
350 0 return $output;
351 }
352  
353 ▶sub DESTROY {}
Subroutine DESTROY: not called CC=1 SCAR=0.0
354  
355 sub AUTOLOAD : lvalue {
356 20 ▶ my $classname = ref $_[0];
Subroutine AUTOLOAD: called CC=12 SCAR=26.6
357 20 my $validname = '[_a-zA-Z][\:a-zA-Z0-9_]*';
358 20 our $AUTOLOAD =~ /^${classname}::($validname)$/;
359 20 my $key = $1;
360 20 ▶ die "illegal key name, must be of $validname form\n$AUTOLOAD" unless $key;
Branch: 1 branch
BranchTrueFalseTotal
unless $key 0 20 20
361 20 ▶ my $miss = Want::want('REF OBJECT') ? {} : '';
Branch: 1 branch
BranchTrueFalseTotal
Want::want('REF OBJECT') ? : 4 16 20
362 20 my $retval = $_[0]->{$key};
363 20 ▶ return $retval->(@_) if (ref $retval eq 'CODE');
Branch: 1 branch
BranchTrueFalseTotal
if ref $retval eq "CODE" 0 20 20
364 20 ▶ die "illegal use of AUTOLOAD $classname -> $key - too many arguments" if (scalar @_ > 2);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 2 0 20 20
365 20 ▶ my $isBool = Want::want('SCALAR BOOL') && ((reftype($retval) // '') eq 'SCALAR');
Condition20%1/5
Want::want('SCALAR BOOL') && (builtin::reftype($retval) // '') eq 'SCALAR'
!ll&&!rl&&r
2000
builtin::reftype($retval) // ""
l!l
00
MC/DC0%0/2
Want::want('SCALAR BOOL') && (builtin::reftype($retval) // '') eq 'SCALAR'
Want::want('SCALAR BOOL') builtin::reftype($retval)
Truth table33%1/3
A && B
A= Want::want('SCALAR BOOL')B= builtin::reftype($retval)
ABresult
0X0
100
111
366 20 ▶ return $$retval if $isBool;
Branch: 1 branch
BranchTrueFalseTotal
if $isBool 0 20 20
367 20 ▶ $_[0]->{$key} = $_[1] // $retval // $miss;
Condition83%5/6
($_[1] // $retval) // $miss
l!l&&r!l&&!r
7211
$_[1] // $retval
l!l&&r!l&&!r
0713
MC/DC66%2/3
($_[1] // $retval) // $miss
$_[1] $retval $miss
Truth table75%3/4
A // B // C
A= $_[1]B= $retvalC= $miss
ABCresult
1XX1
01X1
0011
0000
368 20 ▶ $_[0]->_bless_tree($_[0]->{$key}) if ref $_[0]->{$key} eq 'HASH' || ref $_[0]->{$key} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0]{$key} eq "HASH" or ref $_[0]{$key} eq "ARRAY" 4 16 20
Condition66%2/3
ref $_[0]{$key} eq "HASH" or ref $_[0]{$key} eq "ARRAY"
l!l&&r!l&&!r
4016
MC/DC50%1/2
ref $_[0]{$key} eq "HASH" or ref $_[0]{$key} eq "ARRAY"
ref $_[0]{$key} eq "HASH" ref $_[0]{$key} eq "ARRAY"
Truth table66%2/3
A or B
A= ref $_[0]{$key} eq "HASH"B= ref $_[0]{$key} eq "ARRAY"
ABresult
1X1
011
000
369 20 $_[0]->{$key};
370 }
371  
372 3 1;
373  
374 __END__
« Terse/Controller.pm ↑ Summary Terse/App.pm »