blib/lib/Raisin/Middleware/Formatter.pm

filter: Statementstmt 100% 113 / 113 Branchbran 94.1% 32 / 34 Conditioncond 48.1% 13 / 27 MC/DCmcdc 33.3% 4 / 12 Subroutinesub 100% 19 / 19 Podpod 80.0% 4 / 5 totaltotal 88.1% 185 / 210 CC 29 SCAR 17.7
CC
29
Cov
91%
_is_a_handle
19.3
format_from_header
6.0
allowed_formats_for_requested_route
5.0
CRAP 5.9
« blib/lib/Raisin/Request.pm ↑ Summary blib/lib/Raisin/Plugin.pm »
1 #!perl
2 #PODNAME: Raisin::Middleware::Formatter
3 #ABSTRACT: A parser/formatter middleware for L<Raisin>.
4  
5 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 package Raisin::Middleware::Formatter;
9 16 $Raisin::Middleware::Formatter::VERSION = '0.94';
10 16 ▶use parent 'Plack::Middleware';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 16 ▶use File::Basename qw(fileparse);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 16 ▶use HTTP::Status qw(:constants);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 16 ▶use Scalar::Util qw{ blessed reftype openhandle };
Subroutine BEGIN: called CC=1 SCAR=0.0
15 16 ▶use Plack::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 16 ▶use Plack::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 16 ▶use Plack::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 16 ▶use Plack::Util::Accessor qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
19 default_format
20 format
21 encoder
22 decoder
23 raisin
24 16 );
25  
26 50 ▶sub call {
Subroutine call: called CC=4 SCAR=13.9
Pod documented
27 50 my ($self, $env) = @_;
28  
29 # Pre-process
30 50 my $req = Plack::Request->new($env);
31  
32 50 ▶ if ($req->content) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->content) 20 30 50
33 20 my %media_types_map_flat_hash = $self->decoder->media_types_map_flat_hash;
34  
35 20 my ($ctype) = split /;/, $req->content_type, 2;
36 20 my $format = $media_types_map_flat_hash{$ctype};
37 20 ▶ unless ($format) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($format) 1 19 20
38 1 Raisin::log(info => "unsupported media type: ${ \$req->content_type }");
39 1 return Plack::Response->new(HTTP_UNSUPPORTED_MEDIA_TYPE)->finalize;
40 }
41 19 $env->{'raisinx.decoder'} = $format;
42  
43 19 my $d = Plack::Util::load_class($self->decoder->for($format));
44 19 $env->{'raisinx.body_params'} = $d->deserialize($req);
45 }
46  
47 49 my $format = $self->negotiate_format($req);
48 49 ▶ unless ($format) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($format) 3 46 49
49 3 return Plack::Response->new(HTTP_NOT_ACCEPTABLE)->finalize;
50 }
51 46 $env->{'raisinx.encoder'} = $format;
52  
53 46 my $res = $self->app->($env);
54 # Post-process
55 46 ▶ Plack::Util::response_cb($res, sub {
Subroutine __ANON__: called CC=4 SCAR=14.4
56  
57 46 my $res = shift;
58 46 my $r = Plack::Response->new(@$res);
59  
60 # The application may decide on the fly to return a different
61 # content type than we negotiated above. In that case it becomes
62 # responsible for updating $env appropriately, and also
63 # specifying the encoder to use.
64 46 my $format = $env->{'raisinx.encoder'};
65  
66 # If the body is a data structure of some sort, finalize it now,
67 # BUT NOT if it's a file handle (broadly construed). In that case
68 # treat it as a deferred response.
69 46 ▶ if (ref $r->body && !_is_a_handle($r->body)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $r->body and not _is_a_handle($r->body)) 46 0 46
Condition33%1/3
ref $r->body and not _is_a_handle($r->body)
!ll&&!rl&&r
0046
MC/DC0%0/2
ref $r->body and not _is_a_handle($r->body)
ref $r->body not _is_a_handle($r->body)
Truth table33%1/3
A and not(B)
A= ref $r->bodyB= not _is_a_handle($r->body)
ABresult
0X0
100
111
70 46 my $s = Plack::Util::load_class($self->encoder->for($format));
71  
72 46 ▶ $r->content_type($s->content_type) unless $r->content_type;
Branch: 1 branch
BranchTrueFalseTotal
unless $r->content_type 46 0 46
73 46 $r->body($s->serialize($r->body));
74 }
75  
76 46 @$res = @{ $r->finalize };
77 46 return;
78 46 });
79 }
80  
81 # Test whether the argument is a "handle," meaning that it's either
82 # a built-in handle or an IO::Handle-like object. It's a file handle
83 # if fileno or Scalar::Util::openhandle think it is, or if it supports
84 # a "getline" and a "close" method.
85 46 ▶sub _is_a_handle {
Subroutine _is_a_handle: called CC=7 SCAR=29.6
86 46 my ($var) = @_;
87  
88 return
89 46 ▶ ( ( reftype $var // '' ) eq 'GLOB' && ( defined fileno($var) || defined openhandle($var) ) )
Condition29%5/17
(builtin::reftype($var) // '') eq 'GLOB' && (defined fileno $var || defined &openhandle($var)) || builtin::blessed($var) && $var->can('getline') && $var->can('close')
l!l&&r!l&&!r
0046
(builtin::reftype($var) // "") eq "GLOB" and defined fileno $var || defined &openhandle($var)
!ll&&!rl&&r
4600
builtin::reftype($var) // ""
l!l
460
defined fileno $var or defined &openhandle($var)
l!l&&r!l&&!r
000
builtin::blessed($var) and $var->can("getline") and $var->can("close")
!ll&&!rl&&r
4600
builtin::blessed($var) and $var->can("getline")
!ll&&!rl&&r
4600
MC/DC0%0/6
(builtin::reftype($var) // '') eq 'GLOB' && (defined fileno $var || defined &openhandle($var)) || builtin::blessed($var) && $var->can('getline') && $var->can('close')
builtin::reftype($var) defined fileno $var defined &openhandle($var) builtin::blessed($var) $var->can("getline") $var->can("close")
Truth table0%0/10
A and B or C || D and E and F
A= builtin::reftype($var)B= defined fileno $varC= defined &openhandle($var)D= builtin::blessed($var)E= $var->can("getline")F= $var->can("close")
ABCDEFresult
11XXXX1
101XXX1
0XX1111
1001111
0XX0XX0
0XX10X0
0XX1100
1000XX0
10010X0
1001100
90 ||
91 ( blessed $var && $var->can('getline') && $var->can('close') )
92 ;
93 }
94  
95 50 ▶sub _accept_header_set { length(shift || '') }
Subroutine _accept_header_set: called CC=2 SCAR=6.9
Condition100%2/2
shift() || ''
l!l
2426
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
96 71 ▶sub _path_has_extension {
Subroutine _path_has_extension: called CC=1 SCAR=0.0
97 71 my $path = shift;
98 71 my (undef, undef, $suffix) = fileparse($path, qr/\..[^.]*$/);
99 71 $suffix;
100 }
101  
102 62 ▶sub negotiate_format {
Subroutine negotiate_format: called CC=4 SCAR=13.9
Pod documented
103 62 my ($self, $req) = @_;
104  
105 62 my @allowed_formats = $self->allowed_formats_for_requested_route($req);
106  
107 # PRECEDENCE:
108 # - known extension
109 # - headers
110 # - default
111 62 my @wanted_formats = do {
112 62 my $ext_format = $self->format_from_extension($req->path);
113 62 ▶ if ($ext_format) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ext_format) { } 12 50 62
elsif (_accept_header_set($req->header('Accept'))) { } 24 26 50
114 12 $ext_format;
115 }
116 elsif (_accept_header_set($req->header('Accept'))) {
117 # In case of wildcard matches, we default to first allowed format
118 24 $self->format_from_header($req->header('Accept'), $allowed_formats[0]);
119 }
120 else {
121 26 $self->default_format;
122 }
123 };
124  
125 my @matching_formats = grep {
126 62 my $format = $_;
127 61 ▶ grep { $format && $format eq $_ } @allowed_formats
Condition100%3/3
$format && $format eq $_
!ll&&!rl&&r
311091
MC/DC100%2/2
$format && $format eq $_
$format $format eq $_
Truth table100%3/3
A && B
A= $formatB= $format eq $_
ABresult
0X0
100
111
128 } @wanted_formats;
129  
130 62 shift @matching_formats;
131 }
132  
133 68 ▶sub format_from_extension {
Subroutine format_from_extension: called CC=4 SCAR=13.9
Pod documented
134 68 my ($self, $path) = @_;
135 68 ▶ return unless $path;
Branch: 1 branch
BranchTrueFalseTotal
unless $path 1 67 68
136  
137 67 my $ext = _path_has_extension($path);
138 67 ▶ return unless $ext;
Branch: 1 branch
BranchTrueFalseTotal
unless $ext 49 18 67
139  
140 # Trim leading dot in the extension.
141 18 $ext = substr($ext, 1);
142  
143 18 my %media_types_map_flat_hash = $self->encoder->media_types_map_flat_hash;
144 18 my $format = $media_types_map_flat_hash{$ext};
145 18 ▶ return unless $format;
Branch: 1 branch
BranchTrueFalseTotal
unless $format 3 15 18
146  
147 15 $format;
148 }
149  
150 34 ▶sub format_from_header {
Subroutine format_from_header: called CC=6 SCAR=17.9
Pod documented
151 34 my ($self, $accept, $assumed_wildcard_format) = @_;
152 34 ▶ return unless $accept;
Branch: 1 branch
BranchTrueFalseTotal
unless $accept 1 33 34
153  
154 33 my %media_types_map_flat_hash = $self->encoder->media_types_map_flat_hash;
155 # Add a default format as a `*/*`
156 33 $media_types_map_flat_hash{'*/*'} = $assumed_wildcard_format;
157  
158 33 my @media_types;
159 33 for my $type (split /\s*,\s*/, $accept) {
160 49 my ($media, $params) = split /;/, $type, 2;
161 # Cleaning up media type by deleting a Vendor tree
162 49 $media =~ s/vnd\.[^+]+\+//g;
163  
164 49 ▶ next unless my $format = $media_types_map_flat_hash{$media};
Branch: 1 branch
BranchTrueFalseTotal
unless my $format = $media_types_map_flat_hash{$media} 11 38 49
165  
166 38 ▶ my $q = ($params // '') =~ /q=([\d\.]+)/ ? $1 : 1;
Branch: 1 branch
BranchTrueFalseTotal
($params // '') =~ /q=([\d\.]+)/ ? : 8 30 38
Condition100%2/2
$params // ""
l!l
929
MC/DC100%1/1
$params // ""
$params
Truth table100%2/2
A
A= $params
Aresult
11
00
167  
168 38 push @media_types, { format => $format, q => $q };
169 }
170  
171 33 map { $_->{format} } sort { $b->{q} <=> $a->{q} } @media_types;
172 }
173  
174 62 ▶sub allowed_formats_for_requested_route {
Subroutine allowed_formats_for_requested_route: called CC=5 SCAR=16.1
Pod undocumented
175 62 my ($self, $req) = @_;
176 # Global format has been forced upon entire app
177 62 ▶ return $self->format if $self->format;
Branch: 1 branch
BranchTrueFalseTotal
if $self->format 6 56 62
178  
179 # Route specific `produces` restrictions
180 56 ▶ if ( $self->raisin ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->raisin) 33 23 56
181 33 my $route = $self->raisin->routes->find($req->method, $req->path);
182 33 ▶ return @{$route->{produces}} if $route->{produces};
Branch: 1 branch
BranchTrueFalseTotal
if $route->{"produces"} 4 29 33
183 }
184  
185 # Prefer Default, allow all others
186 52 my @allowed = keys %{ $self->encoder->all };
187 52 ▶ unshift @allowed, $self->default_format if $self->default_format;
Branch: 1 branch
BranchTrueFalseTotal
if $self->default_format 43 9 52
188 52 return @allowed;
189 }
190  
191 16 1;
192  
193 __END__
« blib/lib/Raisin/Request.pm ↑ Summary blib/lib/Raisin/Plugin.pm »