blib/lib/Kelp/Response.pm

filter: Statementstmt 93.5% 116 / 124 Branchbran 86.6% 26 / 30 Conditioncond 78.5% 22 / 28 MC/DCmcdc 43.7% 7 / 16 Subroutinesub 91.1% 31 / 34 Podpod 86.3% 19 / 22 totaltotal 87.0% 221 / 254 CC 28 SCAR 8.7
CC
28
Cov
90%
_render_ref
4.2
render_binary
4.0
render_error
4.0
CRAP 2.4
« blib/lib/Kelp/Module.pm ↑ Summary blib/lib/Kelp/Context.pm »
1 package Kelp::Response;
2  
3 30 ▶use Kelp::Base 'Plack::Response';
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 30 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 30 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 30 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 30 ▶use HTTP::Status qw(status_message);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 30 ▶use Kelp::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 30 our @CARP_NOT = qw(Kelp);
12  
13 0 ▶attr -app => sub { croak "app is required" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
14 30 attr charset => undef;
15 30 attr rendered => 0;
16 30 attr partial => 0;
17  
18 sub new
19 262 ▶{
Subroutine new: called CC=2 SCAR=6.9
Pod documented
20 262 my ($class, %args) = @_;
21 262 my $self = $class->SUPER::new();
22 262 $self->{$_} = $args{$_} for keys %args;
23 262 return $self;
24 }
25  
26 sub set_content_type
27 250 ▶{
Subroutine set_content_type: called CC=2 SCAR=6.9
Pod documented
28 250 my ($self, $type, $charset) = @_;
29 250 $self->content_type($type);
30 250 ▶ $self->charset($charset) if $charset;
Branch: 1 branch
BranchTrueFalseTotal
if $charset 246 4 250
31 250 return $self;
32 }
33  
34 sub charset_encode
35 235 ▶{
Subroutine charset_encode: called CC=2 SCAR=6.9
Pod documented
36 235 my ($self, $string) = @_;
37 235 ▶ return Kelp::Util::charset_encode(
Condition100%3/3
Kelp::Util::effective_charset($self->charset) // $self->app->charset
l!l&&r!l&&!r
23311
MC/DC100%2/2
Kelp::Util::effective_charset($self->charset) // $self->app->charset
Kelp::Util::effective_charset($self->charset) $self->app->charset
Truth table100%3/3
A // B
A= Kelp::Util::effective_charset($self->charset)B= $self->app->charset
ABresult
1X1
011
000
38 Kelp::Util::effective_charset($self->charset) // $self->app->charset,
39 $string,
40 );
41 }
42  
43 sub _apply_charset
44 257 ▶{
Subroutine _apply_charset: called CC=3 SCAR=11.0
45 257 my ($self) = @_;
46 257 my $charset = $self->charset;
47 257 ▶ return unless $charset;
Branch: 1 branch
BranchTrueFalseTotal
unless $charset 21 236 257
48  
49 236 my $ct = $self->content_type;
50  
51 236 ▶ croak 'Cannot apply charset to response without content_type'
Branch: 1 branch
BranchTrueFalseTotal
unless $ct 0 236 236
52 unless $ct;
53  
54 # content_type is actually an array, getting it in scalar context only
55 # yields the actual type without charset. It will be split after setting it
56 # like this
57 236 $self->content_type("$ct; charset=$charset");
58 }
59  
60 sub text
61 33 ▶{
Subroutine text: called CC=2 SCAR=7.0
Pod documented
62 33 my $self = shift;
63 33 ▶ $self->set_content_type('text/plain', $self->charset || $self->app->charset);
Condition66%2/3
$self->charset || $self->app->charset
l!l&&r!l&&!r
3300
MC/DC0%0/2
$self->charset || $self->app->charset
$self->charset $self->app->charset
Truth table66%2/3
A || B
A= $self->charsetB= $self->app->charset
ABresult
1X1
011
000
64 33 return $self;
65 }
66  
67 sub html
68 179 ▶{
Subroutine html: called CC=2 SCAR=6.9
Pod documented
69 179 my $self = shift;
70 179 ▶ $self->set_content_type('text/html', $self->charset || $self->app->charset);
Condition100%3/3
$self->charset || $self->app->charset
l!l&&r!l&&!r
101681
MC/DC100%2/2
$self->charset || $self->app->charset
$self->charset $self->app->charset
Truth table100%3/3
A || B
A= $self->charsetB= $self->app->charset
ABresult
1X1
011
000
71 179 return $self;
72 }
73  
74 sub json
75 33 ▶{
Subroutine json: called CC=2 SCAR=7.0
Pod documented
76 33 my $self = shift;
77 33 ▶ $self->set_content_type('application/json', $self->charset || $self->app->charset);
Condition66%2/3
$self->charset || $self->app->charset
l!l&&r!l&&!r
2310
MC/DC0%0/2
$self->charset || $self->app->charset
$self->charset $self->app->charset
Truth table66%2/3
A || B
A= $self->charsetB= $self->app->charset
ABresult
1X1
011
000
78 33 return $self;
79 }
80  
81 sub xml
82 2 ▶{
Subroutine xml: called CC=2 SCAR=7.0
Pod documented
83 2 my $self = shift;
84 2 ▶ $self->set_content_type('application/xml', $self->charset || $self->app->charset);
Condition66%2/3
$self->charset || $self->app->charset
l!l&&r!l&&!r
110
MC/DC0%0/2
$self->charset || $self->app->charset
$self->charset $self->app->charset
Truth table66%2/3
A || B
A= $self->charsetB= $self->app->charset
ABresult
1X1
011
000
85 2 return $self;
86 }
87  
88 sub finalize
89 257 ▶{
Subroutine finalize: called CC=2 SCAR=6.9
Pod documented
90 257 my $self = shift;
91  
92 257 $self->_apply_charset;
93  
94 257 my $arr = $self->SUPER::finalize(@_);
95 257 ▶ pop @$arr if $self->partial;
Branch: 1 branch
BranchTrueFalseTotal
if $self->partial 1 256 257
96 257 return $arr;
97 }
98  
99 sub set_header
100 1 ▶{
Subroutine set_header: called CC=1 SCAR=0.0
Pod documented
101 1 my $self = shift;
102 1 $self->SUPER::header(@_);
103 1 return $self;
104 }
105  
106 sub no_cache
107 0 ▶{
Subroutine no_cache: not called CC=1 SCAR=6.9
Pod documented
108 0 my $self = shift;
109 0 $self->set_header('Cache-Control' => 'no-cache, no-store, must-revalidate');
110 0 $self->set_header('Pragma' => 'no-cache');
111 0 $self->set_header('Expires' => '0');
112 0 return $self;
113 }
114  
115 sub set_code
116 238 ▶{
Subroutine set_code: called CC=1 SCAR=0.0
Pod documented
117 238 my $self = shift;
118 238 $self->SUPER::code(@_);
119 238 return $self;
120 }
121  
122 sub render
123 240 ▶{
Subroutine render: called CC=3 SCAR=11.0
Pod documented
124 240 my ($self, $body) = @_;
125  
126 240 ▶ my $method = ref $body ? '_render_ref' : '_render_nonref';
Branch: 1 branch
BranchTrueFalseTotal
ref $body ? : 27 213 240
127 240 $body = $self->$method($body);
128  
129 # Set code 200 if the code has not been set
130 235 ▶ $self->set_code(200) unless $self->code;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->code 161 74 235
131  
132 235 $self->body($self->charset_encode($body));
133 235 $self->rendered(1);
134 235 return $self;
135 }
136  
137 # override to change how references are serialized
138 sub _render_ref
139 27 ▶{
Subroutine _render_ref: called CC=4 SCAR=14.3
140 27 my ($self, $body) = @_;
141 27 my $ct = $self->content_type;
142  
143 27 ▶ if (!$ct || $ct =~ m{^application/json}i) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $ct or $ct =~ m[^application/json]i) { } 27 0 27
Condition66%2/3
not $ct or $ct =~ m[^application/json]i
l!l&&r!l&&!r
2250
MC/DC0%0/2
not $ct or $ct =~ m[^application/json]i
not $ct $ct =~ m[^application/json]i
Truth table66%2/3
not(A) or B
A= not $ctB= $ct =~ m[^application/json]i
ABresult
1X1
011
000
144 27 ▶ $self->json if !$ct;
Branch: 1 branch
BranchTrueFalseTotal
unless $ct 22 5 27
145 27 return $self->app->get_encoder(json => 'internal')->encode($body);
146 }
147 else {
148 0 croak "Don't know how to handle reference for $ct in response (forgot to serialize?)";
149 }
150 }
151  
152 # override to change how non-references are handled
153 sub _render_nonref
154 213 ▶{
Subroutine _render_nonref: called CC=2 SCAR=6.9
155 213 my ($self, $body) = @_;
156 213 ▶ $self->html if !$self->content_type;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->content_type 176 37 213
157  
158 213 return $body;
159 }
160  
161 sub render_binary
162 2 ▶{
Subroutine render_binary: called CC=4 SCAR=14.0
Pod documented
163 2 my ($self, $body) = @_;
164 2 ▶ $body //= '';
Condition50%1/2
$body //= ""
l!l
20
MC/DC0%0/1
$body //= ""
$body
Truth table50%1/2
A
A= $body
Aresult
11
00
165  
166 # Set code 200 if the code has not been set
167 2 ▶ $self->set_code(200) unless $self->code;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->code 2 0 2
168  
169 2 ▶ if (!$self->content_type) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->content_type) 1 1 2
170 1 croak "Content-type must be explicitly set for binaries";
171 }
172  
173 1 $self->body($body);
174 1 $self->rendered(1);
175 1 return $self;
176 }
177  
178 sub render_error
179 68 ▶{
Subroutine render_error: called CC=4 SCAR=13.9
Pod documented
180 68 my ($self, $code, $error) = @_;
181  
182 68 ▶ $code //= 500;
Condition100%2/2
$code //= 500
l!l
5216
MC/DC100%1/1
$code //= 500
$code
Truth table100%2/2
A
A= $code
Aresult
11
00
183 68 ▶ $error //= status_message($code) // 'Error';
Condition75%3/4
$error //= &status_message($code) // "Error"
l!l
2642
&status_message($code) // "Error"
l!l
420
MC/DC100%1/1
$error //= &status_message($code) // "Error"
$error
Truth table100%2/2
A
A= $error
Aresult
11
00
184  
185 68 $self->set_code($code);
186  
187 # Look for a template and if not found, then show a generic text
188 68 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
189 68 local $SIG{__DIE__}; # Silence StackTrace
190 68 $self->template(
191 "error/$code", {
192 error => $error
193 }
194 );
195 }
196 24 ▶ catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
197 24 $self->text->render("$code - $error");
198 68 };
199  
200 68 return $self;
201 }
202  
203 sub render_exception
204 11 ▶{
Subroutine render_exception: called CC=2 SCAR=6.9
Pod undocumented
205 11 my ($self, $exception) = @_;
206  
207 # If the error is 500, do the same thing normal errors do: provide more
208 # info on non-production
209 11 ▶ return $self->render_500($exception->body)
Branch: 1 branch
BranchTrueFalseTotal
if $exception->code == 500 2 9 11
210 if $exception->code == 500;
211  
212 9 return $self->render_error($exception->code);
213 }
214  
215 sub render_401
216 0 ▶{
Subroutine render_401: not called CC=1 SCAR=6.9
Pod undocumented
217 0 $_[0]->render_error(401);
218 }
219  
220 sub render_403
221 2 ▶{
Subroutine render_403: called CC=1 SCAR=0.0
Pod undocumented
222 2 $_[0]->render_error(403);
223 }
224  
225 sub render_404
226 15 ▶{
Subroutine render_404: called CC=1 SCAR=0.0
Pod documented
227 15 $_[0]->render_error(404);
228 }
229  
230 sub render_500
231 38 ▶{
Subroutine render_500: called CC=4 SCAR=13.9
Pod documented
232 38 my ($self, $error) = @_;
233  
234 # Do not leak information on production!
235 38 ▶ if ($self->app->is_production) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->app->is_production) 14 24 38
236 14 return $self->render_error;
237 }
238  
239 # if render_500 gets blessed object as error, stringify it
240 24 ▶ $error = "$error" if blessed $error;
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($error) 6 18 24
241 24 ▶ $error //= 'No error, something is wrong';
Condition100%2/2
$error //= "No error, something is wrong"
l!l
204
MC/DC100%1/1
$error //= "No error, something is wrong"
$error
Truth table100%2/2
A
A= $error
Aresult
11
00
242  
243 # at this point, error will never be in HTML, since the exception body
244 # would have to be HTML itself. Try to nest it inside a template. NOTE: We
245 # don't currently handle ref errors here which aren't objects
246 24 return $self->render_error(500, $error);
247 }
248  
249 sub redirect
250 7 ▶{
Subroutine redirect: called CC=1 SCAR=0.0
Pod documented
251 7 my $self = shift;
252 7 $self->rendered(1);
253 7 $self->SUPER::redirect(@_);
254 }
255  
256 sub redirect_to
257 7 ▶{
Subroutine redirect_to: called CC=1 SCAR=0.0
Pod documented
258 7 my ($self, $where, $args, $code) = @_;
259 7 my $url = $self->app->url_for($where, %$args);
260 7 $self->redirect($url, $code);
261 }
262  
263 sub template
264 74 ▶{
Subroutine template: called CC=2 SCAR=7.0
Pod documented
265 74 my ($self, $template, $vars, @rest) = @_;
266  
267 # Do we have a template module loaded?
268 74 ▶ croak "No template module loaded"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->app->can("template") 0 74 74
269 unless $self->app->can('template');
270  
271 # run template in current controller context
272 74 my $output = $self->app->context->run_method('template', $template, $vars, @rest);
273 50 $self->render($output);
274 }
275  
276 30 1;
277  
278 __END__
« blib/lib/Kelp/Module.pm ↑ Summary blib/lib/Kelp/Context.pm »