Data/Printer/Filter/Web.pm

filter: Statementstmt 73.4% 72 / 98 Branchbran 44.7% 17 / 38 Conditioncond 33.3% 5 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 56.2% 9 / 16 Podpod n/a totaltotal 58.1% 103 / 177 CC 26 SCAR 21.9
CC
26
Cov
62%
__ANON__
16.1
_maybe_show_request
12.2
__ANON__
9.6
CRAP 8.9
« Data/Printer/Filter/SCALAR.pm ↑ Summary Data/Printer/Theme.pm »
1 package Data::Printer::Filter::Web;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Data::Printer::Filter;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 ####################
7 ### JSON parsers
8 ### Heavily inspired by nuba++'s excellent Data::Printer::Filter::JSON
9 #############################################
10  
11 24 ▶sub _parse_json_boolean {
Subroutine _parse_json_boolean: called CC=2 SCAR=6.9
12 24 my ($value, $ddp) = @_;
13 24 ▶ my @colors = ($value eq 'true'
Branch: 1 branch
BranchTrueFalseTotal
$value eq 'true' ? : 12 12 24
14 ? ('filter_web_json_true', '#ccffcc')
15 : ('filter_web_json_false', '#ffcccc')
16 );
17 24 return $ddp->maybe_colorize($value, @colors);
18 }
19  
20 # JSON::NotString is from JSON::Parser (JSON 1.x)
21 4 ▶filter 'JSON::NotString' => sub { _parse_json_boolean($_[0]->{value}, $_[1]) };
Subroutine __ANON__: called CC=1 SCAR=0.0
22  
23 # JSON::Typist
24 0 ▶filter 'JSON::Typist::String' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
25 0 my ($obj, $ddp) = @_;
26 0 require Data::Printer::Common;
27 0 my $ret = Data::Printer::Common::_process_string($ddp, "$obj", 'string');
28 0 my $quote = $ddp->maybe_colorize($ddp->scalar_quotes, 'quotes');
29 0 return $quote . $ret . $quote;
30 1 };
31  
32 0 ▶filter 'JSON::Typist::Number' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
33 0 return $_[1]->maybe_colorize($_[0], 'number');
34 1 };
35  
36 # NOTE: boolean is used by Pegex::JSON
37 1 foreach my $json (qw(
38 JSON::DWIW::Boolean JSON::PP::Boolean JSON::SL::Boolean
39 JSON::XS::Boolean boolean JSON::Tiny::_Bool
40 Mojo::JSON::_Bool Cpanel::JSON::XS::Boolean
41 )) {
42 20 ▶ filter "$json" => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
43 20 my ($obj, $ddp) = @_;
44 # because JSON boolean objects are just repeated all over
45 # the place, we must remove them from our "seen" table:
46 20 $ddp->unsee($obj);
47  
48 20 ▶ return _parse_json_boolean(($$obj == 1 ? 'true' : 'false'), $ddp);
Branch: 1 branch
BranchTrueFalseTotal
$$obj == 1 ? : 10 10 20
49 8 };
50 }
51  
52 1 for my $json (qw( JSON::JOM::Value JSON::JOM::Array JSON::JOM::Object )) {
53 0 ▶ filter "$json" => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
54 0 my ($obj, $ddp) = @_;
55 0 return $ddp->parse($obj->TO_JSON);
56 3 };
57 }
58  
59 ####################
60 ### Cookie parsers
61 #############################################
62  
63 0 ▶filter 'Mojo::Cookie' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
64 0 my ($obj, $ddp) = @_;
65 0 ▶ return _format_cookie({
Branch: 1 branch
BranchTrueFalseTotal
$obj->can('host_only') ? : 0 0 0
66 expires => scalar $obj->expires,
67 max_age => $obj->max_age,
68 domain => $obj->domain,
69 path => $obj->path,
70 secure => $obj->secure,
71 http_only => $obj->httponly,
72 host_only => ($obj->can('host_only') ? $obj->host_only : 0),
73 name => $obj->name,
74 value => $obj->value,
75 class => 'Mojo::Cookie',
76 }, $ddp);
77 1 };
78  
79 0 ▶filter 'Dancer::Cookie' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
80 0 my ($obj, $ddp) = @_;
81 0 return _format_cookie({
82 expires => scalar $obj->expires,
83 domain => $obj->domain,
84 path => $obj->path,
85 secure => $obj->secure,
86 http_only => $obj->http_only,
87 name => $obj->name,
88 value => $obj->value,
89 class => 'Dancer::Cookie',
90 }, $ddp);
91 1 };
92  
93 0 ▶filter 'Dancer2::Core::Cookie' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
94 0 my ($obj, $ddp) = @_;
95 0 return _format_cookie({
96 expires => scalar $obj->expires,
97 domain => $obj->domain,
98 path => $obj->path,
99 secure => $obj->secure,
100 http_only => $obj->http_only,
101 name => $obj->name,
102 value => $obj->value,
103 class => 'Dancer2::Core::Cookie',
104 }, $ddp);
105 1 };
106  
107 0 ▶sub _format_cookie {
Subroutine _format_cookie: not called CC=2 SCAR=17.9
108 0 my ($data, $ddp) = @_;
109 return $ddp->maybe_colorize(
110 $data->{name} . '='
111 . Data::Printer::Common::_process_string($ddp, $data->{value})
112 . '; expires=' . $data->{expires}
113 . '; domain=' . $data->{domain}
114 . '; path=' . $data->{path}
115 . ('; secure'x!!$data->{secure})
116 . ('; http-only'x!!$data->{http_only})
117 . ('; host-only'x!!$data->{host_only})
118 . (defined $data->{max_age} ? '; max-age=' . $data->{max_age} : '')
119 , 'filter_web_cookie', '#0b3e21'
120 0 ▶ ) . ' (' . $ddp->maybe_colorize($data->{class}, 'class') . ')';
Branch: 1 branch
BranchTrueFalseTotal
defined $data->{'max_age'} ? : 0 0 0
121 }
122  
123 ####################
124 ### HTTP parsers
125 #############################################
126  
127 1 ▶filter 'HTTP::Request' => sub {
Subroutine __ANON__: called CC=8 SCAR=22.6
128 1 my ($obj, $ddp) = @_;
129 1 my $output = $ddp->maybe_colorize($obj->method, 'filter_web_method', '#fefe33')
130 . ' '
131 . $ddp->maybe_colorize($obj->uri, 'filter_web_uri', '#fefe88')
132 ;
133  
134 1 ▶ if ($ddp->extra_config->{filter_web}{show_class_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ddp->extra_config->{"filter_web"}{"show_class_name"}) 0 1 1
135 0 $output .= ' (' . $ddp->maybe_colorize(ref $obj, 'class') . ')';
136 }
137  
138 my $expand_headers = !exists $ddp->extra_config->{filter_web}{expand_headers}
139 1 ▶ || $ddp->extra_config->{filter_web}{expand_headers};
Condition33%1/3
!exists $ddp->extra_config->{'filter_web'}{'expand_headers'} || $ddp->extra_config->{'filter_web'}{'expand_headers'}
l!l&&r!l&&!r
100
MC/DC0%0/2
!exists $ddp->extra_config->{'filter_web'}{'expand_headers'} || $ddp->extra_config->{'filter_web'}{'expand_headers'}
!exists $ddp->extra_config->{'filter_web'}{'expand_headers'} $ddp->extra_config->{'filter_web'}{'expand_headers'}
Truth table33%1/3
not(A) || B
A= !exists $ddp->extra_config->{'filter_web'}{'expand_headers'}B= $ddp->extra_config->{'filter_web'}{'expand_headers'}
ABresult
1X1
011
000
140  
141 1 my $content = $obj->decoded_content;
142 1 ▶ if ($expand_headers || $content) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expand_headers or $content) 1 0 1
Condition33%1/3
$expand_headers or $content
l!l&&r!l&&!r
100
MC/DC0%0/2
$expand_headers or $content
$expand_headers $content
Truth table33%1/3
A or B
A= $expand_headersB= $content
ABresult
1X1
011
000
143 1 $output .= ' {';
144 1 $ddp->indent;
145 1 ▶ if ($expand_headers) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expand_headers) 1 0 1
146 1 ▶ if ($obj->headers->can('flatten')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->headers->can("flatten")) 1 0 1
147 1 my %headers = $obj->headers->flatten;
148 1 $output .= $ddp->newline . 'headers: ' . $ddp->parse(\%headers);
149 }
150 }
151 1 ▶ if ($content) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content) 1 0 1
152 1 $output .= $ddp->newline . 'content: '
153 . Data::Printer::Common::_process_string($ddp, $content, 'string');
154 }
155 1 $ddp->outdent;
156 1 $output .= $ddp->newline . '}';
157 }
158 1 return $output;
159 1 };
160  
161 1 ▶filter 'HTTP::Response' => sub {
Subroutine __ANON__: called CC=12 SCAR=27.8
162 1 my ($obj, $ddp) = @_;
163 1 my $output = _maybe_show_request($obj, $ddp);
164  
165 1 ▶ if (!exists $ddp->extra_config->{filter_web}{show_redirect}
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $ddp->extra_config->{"filter_web"}{"show_redirect"} or $ddp->extra_config->{"filter_web"}{"show_redirect"}) 1 0 1
Condition33%1/3
not exists $ddp->extra_config->{"filter_web"}{"show_redirect"} or $ddp->extra_config->{"filter_web"}{"show_redirect"}
l!l&&r!l&&!r
100
MC/DC0%0/2
not exists $ddp->extra_config->{"filter_web"}{"show_redirect"} or $ddp->extra_config->{"filter_web"}{"show_redirect"}
not exists $ddp->extra_config->{"filter_web"}{"show_redirect"} $ddp->extra_config->{"filter_web"}{"show_redirect"}
Truth table33%1/3
not(A) or B
A= not exists $ddp->extra_config->{"filter_web"}{"show_redirect"}B= $ddp->extra_config->{"filter_web"}{"show_redirect"}
ABresult
1X1
011
000
166 || $ddp->extra_config->{filter_web}{show_redirect}
167 ) {
168 1 foreach my $redir ($obj->redirects) {
169 1 $output .= "\x{e2}\x{a4}\x{bf} "
170 . $redir->code . ' ' . $redir->message
171 . ' (' . $redir->header('location') . ')'
172 . $ddp->newline;
173 }
174 }
175  
176 1 my %colors = (
177 1 => ['filter_web_response_info' , '#3333fe'],
178 2 => ['filter_web_response_success' , '#33fe33'],
179 3 => ['filter_web_response_redirect', '#fefe33'],
180 4 => ['filter_web_response_error' , '#fe3333'],
181 5 => ['filter_web_response_error' , '#fe3333'],
182 );
183 1 my $status_key = substr($obj->code, 0, 1);
184 $output .= $ddp->maybe_colorize(
185 $obj->status_line,
186 0 ▶ (exists $colors{$status_key} ? @{$colors{$status_key}} : @{$colors{1}})
Branch: 1 branch
BranchTrueFalseTotal
exists $colors{$status_key} ? : 1 0 1
187 );
188  
189 1 ▶ if ($ddp->extra_config->{filter_web}{show_class_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ddp->extra_config->{"filter_web"}{"show_class_name"}) 0 1 1
190 0 $output .= ' (' . $ddp->maybe_colorize(ref $obj, 'class') . ')';
191 }
192  
193 my $expand_headers = !exists $ddp->extra_config->{filter_web}{expand_headers}
194 1 ▶ || $ddp->extra_config->{filter_web}{expand_headers};
Condition33%1/3
!exists $ddp->extra_config->{'filter_web'}{'expand_headers'} || $ddp->extra_config->{'filter_web'}{'expand_headers'}
l!l&&r!l&&!r
100
MC/DC0%0/2
!exists $ddp->extra_config->{'filter_web'}{'expand_headers'} || $ddp->extra_config->{'filter_web'}{'expand_headers'}
!exists $ddp->extra_config->{'filter_web'}{'expand_headers'} $ddp->extra_config->{'filter_web'}{'expand_headers'}
Truth table33%1/3
not(A) || B
A= !exists $ddp->extra_config->{'filter_web'}{'expand_headers'}B= $ddp->extra_config->{'filter_web'}{'expand_headers'}
ABresult
1X1
011
000
195  
196 1 my $content = $obj->decoded_content;
197 1 ▶ if ($expand_headers || $content) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expand_headers or $content) 1 0 1
Condition33%1/3
$expand_headers or $content
l!l&&r!l&&!r
100
MC/DC0%0/2
$expand_headers or $content
$expand_headers $content
Truth table33%1/3
A or B
A= $expand_headersB= $content
ABresult
1X1
011
000
198 1 $output .= ' {';
199 1 $ddp->indent;
200 1 ▶ if ($expand_headers) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expand_headers) 1 0 1
201 1 ▶ if ($obj->headers->can('flatten')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->headers->can("flatten")) 1 0 1
202 1 my %headers = $obj->headers->flatten;
203 1 $output .= $ddp->newline . 'headers: ' . $ddp->parse(\%headers);
204 }
205 }
206 1 ▶ if ($content) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content) 1 0 1
207 1 $output .= $ddp->newline . 'content: '
208 . Data::Printer::Common::_process_string($ddp, $content, 'string');
209 }
210 1 $ddp->outdent;
211 1 $output .= $ddp->newline . '}';
212 }
213 1 return $output;
214 1 };
215  
216 1 ▶sub _maybe_show_request {
Subroutine _maybe_show_request: called CC=4 SCAR=25.0
217 1 my ($obj, $ddp) = @_;
218 1 ▶ return '' unless $ddp->extra_config->{filter_web}{show_request_in_response};
Branch: 1 branch
BranchTrueFalseTotal
unless $ddp->extra_config->{"filter_web"}{"show_request_in_response"} 1 0 1
219  
220 0 my ($redir) = $obj->redirects;
221 0 my $output = 'Request: ';
222 0 my $request;
223 0 ▶ if ($redir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($redir) { } 0 0 0
224 0 $request = $redir->request;
225 }
226 else {
227 0 $request = $obj->request;
228 }
229 0 ▶ return $output . ($request ? $ddp->parse($request) : '-');
Branch: 1 branch
BranchTrueFalseTotal
$request ? : 0 0 0
230 }
231  
232  
233 1 1;
234 __END__
« Data/Printer/Filter/SCALAR.pm ↑ Summary Data/Printer/Theme.pm »