Slick/Context.pm

filter: Statementstmt 69.5% 80 / 115 Branchbran 20.0% 2 / 10 Conditioncond 20.0% 1 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 61.5% 24 / 39 Podpod 85.7% 12 / 14 totaltotal 63.9% 119 / 186 CC 8 SCAR 19.1
CC
8
Cov
64%
_decode_content
42.0
redirect
2.0
__ANON__
2.0
CRAP 6.8
« Slick/Annotation.pm ↑ Summary Slick/RouteMap.pm »
1 package Slick::Context;
2  
3 1 ▶use 5.036;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Slick::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Types::Standard qw(Str HashRef);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Module::Runtime qw(require_module);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use URI::Query;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use URL::Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use JSON::Tiny qw(encode_json decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use YAML::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Time::HiRes;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 has id => (
16 is => 'ro',
17 isa => Str,
18 lazy => 1,
19 0 ▶ default => sub { return Slick::Util->four_digit_number; }
Subroutine __ANON__: not called CC=1 SCAR=6.9
20 1 );
21  
22 has stash => (
23 is => 'rw',
24 isa => HashRef,
25 lazy => 1,
26 12 ▶ default => sub { return { 'slick.errors' => [] }; }
Subroutine __ANON__: called CC=1 SCAR=0.0
27 1 );
28  
29 1 has request => (
30 is => 'ro',
31 required => 1
32 );
33  
34 has response => (
35 is => 'rw',
36 isa => HashRef,
37 12 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
38 return {
39 12 status => 200,
40 body => [''],
41 headers => []
42 };
43 }
44 1 );
45  
46 has queries => (
47 is => 'ro',
48 isa => HashRef,
49 lazy => 1,
50 0 ▶ default => sub { return {}; }
Subroutine __ANON__: not called CC=1 SCAR=6.9
51 1 );
52  
53 has params => (
54 is => 'ro',
55 isa => HashRef,
56 lazy => 1,
57 0 ▶ default => sub { return {}; }
Subroutine __ANON__: not called CC=1 SCAR=6.9
58 1 );
59  
60 has known_types => (
61 is => 'rw',
62 isa => HashRef,
63 12 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=5.1
64 return +{
65 0 ▶ 'application/json' => sub { return decode_json(shift); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
66 0 ▶ 'text/json' => sub { return decode_json(shift); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
67 'application/json; encoding=utf8' =>
68 0 ▶ sub { return decode_json(shift); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
69 'application/yaml' =>
70 0 ▶ sub { return YAML::Tiny->read_string(shift); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
71 0 ▶ 'text/yaml' => sub { return YAML::Tiny->read_string(shift); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
72 'application/x-yaml' =>
73 0 ▶ sub { return YAML::Tiny->read_string(shift); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
74 'application/x-www-form-urlencoded' =>
75 0 ▶ sub { return url_decode_utf8(shift); }
Subroutine __ANON__: not called CC=1 SCAR=6.9
76 12 };
77 }
78 1 );
79  
80 has _initiated_time => (
81 is => 'ro',
82 12 ▶ default => sub { return time; }
Subroutine __ANON__: called CC=1 SCAR=0.0
83 1 );
84  
85 0 ▶sub _decode_content {
Subroutine _decode_content: not called CC=6 SCAR=37.4
86 0 my $self = shift;
87  
88 return $self->known_types->{ $self->request->content_type }
89 ->( $self->request->content )
90 0 ▶ if exists $self->known_types->{ $self->request->content_type };
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->known_types->{$self->request->content_type} 0 0 0
91  
92 0 ▶ if ( $self->request->content_type =~ 'json' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->request->content_type =~ /json/u) { } 0 0 0
elsif ($self->request->content_type =~ /x-www-form-urlencoded/u) { } 0 0 0
elsif ($self->request->content_type =~ /yaml/u or $self->request->content_type =~ m[application/x-yaml]u) { } 0 0 0
93 0 return decode_json( $self->request->content );
94 }
95 elsif ( $self->request->content_type =~ 'x-www-form-urlencoded' ) {
96 0 return url_decode_utf8( $self->request->content );
97 }
98 ▶ elsif (( $self->request->content_type =~ 'yaml' )
Condition0%0/3
$self->request->content_type =~ /yaml/u or $self->request->content_type =~ m[application/x-yaml]u
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->request->content_type =~ /yaml/u or $self->request->content_type =~ m[application/x-yaml]u
$self->request->content_type =~ /yaml/u $self->request->content_type =~ m[application/x-yaml]u
Truth table0%0/3
A or B
A= $self->request->content_type =~ /yaml/uB= $self->request->content_type =~ m[application/x-yaml]u
ABresult
1X1
011
000
99 || ( $self->request->content_type =~ 'application/x-yaml' ) )
100 {
101 0 return YAML::Tiny->read_string( $self->request->content );
102 }
103 else {
104 0 return $self->request->content;
105 }
106 }
107  
108 12 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
109 12 my $self = shift;
110  
111 12 $self->{queries} = URI::Query->new( $self->request->query_string )->hash;
112  
113 12 return $self;
114 }
115  
116 1 ▶sub param {
Subroutine param: called CC=1 SCAR=0.0
Pod documented
117 1 return shift->params->{ shift() };
118 }
119  
120 1 ▶sub query {
Subroutine query: called CC=1 SCAR=0.0
Pod documented
121 1 return shift->queries->{ shift() };
122 }
123  
124 12 ▶sub to_psgi {
Subroutine to_psgi: called CC=2 SCAR=6.9
Pod documented
125 12 my $self = shift;
126 12 my $response = $self->response;
127  
128 $response->{status} = 500
129 12 ▶ if $self->stash->{'slick.errors'}->@*;
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->stash->{"slick.errors"}} 1 11 12
130  
131 12 return [ $response->{status}, $response->{headers}, $response->{body} ];
132 }
133  
134 0 ▶sub from_psgi {
Subroutine from_psgi: not called CC=1 SCAR=6.9
Pod undocumented
135 0 my $self = shift;
136 0 my $response = shift;
137  
138 0 $self->response->{status} = $response->[0];
139 0 $self->response->{headers} = $response->[1];
140 0 $self->response->{body} = $response->[2];
141  
142 0 return $self;
143 }
144  
145 1 ▶sub redirect {
Subroutine redirect: called CC=2 SCAR=7.0
Pod documented
146 1 my ( $self, $location, $status ) = @_;
147  
148 1 ▶ $self->status( $status // 303 );
Condition50%1/2
$status // 303
l!l
01
MC/DC0%0/1
$status // 303
$status
Truth table50%1/2
A
A= $status
Aresult
11
00
149 1 $self->header( Location => $location );
150  
151 1 return $self;
152 }
153  
154 7 ▶sub header {
Subroutine header: called CC=1 SCAR=0.0
Pod documented
155 7 my ( $self, $key, $value ) = @_;
156  
157 7 my %headers = @{ $self->response->{headers} };
158 7 $headers{$key} = $value;
159 7 $self->response->{headers} = [%headers];
160  
161 7 return $self;
162 }
163  
164 6 ▶sub status {
Subroutine status: called CC=1 SCAR=0.0
Pod documented
165 6 my $self = shift;
166 6 my $status = shift;
167  
168 6 $self->response->{status} = $status;
169  
170 6 return $self;
171 }
172  
173 4 ▶sub json {
Subroutine json: called CC=1 SCAR=0.0
Pod documented
174 4 my $self = shift;
175 4 my $body = shift;
176  
177 4 require_module('JSON::Tiny');
178  
179 4 $self->header( 'Content-Type', 'application/json; encoding=utf8' );
180 4 $self->body( encode_json $body);
181  
182 4 return $self;
183 }
184  
185 0 ▶sub yaml {
Subroutine yaml: not called CC=1 SCAR=6.9
Pod documented
186 0 my $self = shift;
187 0 my $body = shift;
188  
189 0 require_module('YAML::Tiny');
190  
191 0 $self->header( 'Content-Type', 'application/yaml; encoding=utf8' );
192 0 $self->body( YAML::Tiny->new($body)->write_string );
193  
194 0 return $self;
195 }
196  
197 2 ▶sub text {
Subroutine text: called CC=1 SCAR=0.0
Pod documented
198 2 my $self = shift;
199 2 my $body = shift;
200  
201 2 $self->body($body);
202 2 $self->header( 'Content-Type', 'text/plain; encoding=utf8' );
203  
204 2 return $self;
205 }
206  
207 0 ▶sub html {
Subroutine html: not called CC=1 SCAR=6.9
Pod documented
208 0 my $self = shift;
209 0 my $body = shift;
210  
211 0 $self->body($body);
212 0 $self->header( 'Content-Type', 'text/html; encoding=utf8' );
213  
214 0 return $self;
215 }
216  
217 9 ▶sub body {
Subroutine body: called CC=1 SCAR=0.0
Pod documented
218 9 my $self = shift;
219 9 my $body = shift;
220  
221 9 $self->response->{body} = [$body];
222  
223 9 return $self;
224 }
225  
226 0 ▶sub content {
Subroutine content: not called CC=1 SCAR=6.9
Pod documented
227 0 return state $val = shift->_decode_content;
228 }
229  
230 1 1;
231  
232 =encoding utf8
233  
234 =head1 NAME
235  
236 Slick::Context
237  
238 =head1 SYNOPSIS
239  
240 L<Slick::Context> is an L<Moo> wrapper around the HTTP lifecycle. It encompases a L<Plack::Request>
241 and a bunch of other helpers to make it easy to handle HTTP in L<Slick>.
242  
243 =head1 API
244  
245 =head2 content
246  
247 $s->post('/foo', sub {
248 my ($app, $context) = @_;
249 my $data = $context->content;
250 });
251  
252 Decodes the body of the L<Plack::Request> via the following:
253  
254 =over 2
255  
256 =item * C<JSON> => L<JSON::Tiny>
257  
258 =item * C<YAML> => L<YAML::Tiny>
259  
260 =item * C<URL ENCODED> => L<URL::Encode>
261  
262 =item * C<OTHER> => returns as text/bytes
263  
264 =back
265  
266 =head2 body
267  
268 $s->get('/foo', sub {
269 my ($app, $context) = @_;
270 $context->body('Foo!');
271 });
272  
273 Sets the body of the response to whatever is provided. Note:
274 You'll probably want to use L<"html">, L<"json">, L<"text"> or L<"yaml"> instead of this.
275  
276 =head2 html
277  
278 $s->get('/foo', sub {
279 my ($app, $context) = @_;
280 $context->body('<h1>Foo!</h1>');
281 });
282  
283 Sets the body of the response and sets the C<Content-Type> header to C<text/html>.
284 Returns the context.
285  
286 Note, you should have C<use utf8;> enabled.
287  
288 =head2 json
289  
290 $s->get('/foo', sub {
291 my ($app, $context) = @_;
292 $context->json({ hello => 'world' });
293 });
294  
295 Sets the body of the response and sets the C<Content-Type> header to C<application/json>.
296 Returns the context.
297  
298 Note, you should have C<use utf8;> enabled.
299  
300 =head2 yaml
301  
302 $s->get('/foo', sub {
303 my ($app, $context) = @_;
304 $context->yaml([ { hello => 'world' });
305 });
306  
307 Sets the body of the response and sets the C<Content-Type> header to C<application/yaml>.
308 Returns the context.
309  
310 Note, you should have C<use utf8;> enabled.
311  
312 =head2 text
313  
314 $s->get('/foo', sub {
315 my ($app, $context) = @_;
316 $context->text("Hello World!");
317 });
318  
319 Sets the body of the response and sets the C<Content-Type> header to C<text/plain>.
320 Returns the context.
321  
322 Note, you should have C<use utf8;> enabled.
323  
324 =head2 to_psgi
325  
326 Converts the L<Slick::Context> to L<Plack> response. In the format:
327  
328 [$status, [ @headers ], [ @body ]]
329  
330 =head2 status
331  
332 $s->get('/foo', sub {
333 my ($app, $context) = @_;
334 $context->status(201);
335 });
336  
337 Sets the status code of the response, returns the context.
338  
339 =head2 redirect
340  
341 $s->get('/foo', sub {
342 my ($app, $context) = @_;
343 $context->redirect("/foo");
344 });
345  
346 Sets the response to redirect to a given location, optionally provide another status code as a second argument if you don't want C<303>.
347 Returns the context object.
348  
349 =head2 header
350  
351 $s->get('/foo', sub {
352 my ($app, $context) = @_;
353 $context->header(Foo => 'bar');
354 });
355  
356 Sets a header on the underlying response C<HashRef>. Returns the context.
357  
358 =head2 response
359  
360 Returns the response C<HashRef> that will be used to create the PSGI response via L<"to_psgi">.
361  
362 =head2 query
363  
364 $s->get('/foo', sub {
365 my ($app, $context) = @_;
366 $context->query('bar');
367 });
368  
369 Returns the value of a specified query parameter key, returns undef if there is no such key.
370  
371 See L<"queries"> for the raw query parameter C<HashRef>.
372  
373 =head2 queries
374  
375 $s->get('/foo', sub {
376 my ($app, $context) = @_;
377 $context->queries->{'bar'};
378 });
379  
380 Returns the query parameters as a C<HashRef>.
381  
382 =head2 param
383  
384 $s->get('/foo', sub {
385 my ($app, $context) = @_;
386 $context->param('bar');
387 });
388  
389 Returns the value of a specified path parameter key, returns undef if there is no such key.
390  
391 See L<"params"> for the raw path parameter C<HashRef>.
392  
393 =head2 params
394  
395 $s->get('/foo/{bar}', sub {
396 my ($app, $context) = @_;
397 $context->params->{'bar'};
398 });
399  
400 Returns the path parameters as a C<HashRef>.
401  
402 =head2 stash
403  
404 Returns a transient C<HashRef> that is persistent per request, this is for inter-layer communication.
405  
406 =head2 id
407  
408 Returns an arbitrary tracing ID in the form of a 4-digit number.
409  
410 =head1 See also
411  
412 =over2
413  
414 =item * L<Slick>
415  
416 =item * L<Slick::Database>
417  
418 =item * L<Slick::DatabaseExecutor>
419  
420 =item * L<Slick::DatabaseExecutor::MySQL>
421  
422 =item * L<Slick::DatabaseExecutor::Pg>
423  
424 =item * L<Slick::EventHandler>
425  
426 =item * L<Slick::Events>
427  
428 =item * L<Slick::Methods>
429  
430 =item * L<Slick::RouteMap>
431  
432 =item * L<Slick::Util>
433  
434 =back
435  
436 =cut
« Slick/Annotation.pm ↑ Summary Slick/RouteMap.pm »