blib/lib/YATT/Lite/Response.pm

filter: Statementstmt 64.4% 49 / 76 Branchbran 42.8% 12 / 28 Conditioncond 38.4% 5 / 13 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 65.0% 13 / 20 Podpod 0.0% 0 / 7 totaltotal 52.3% 79 / 151 CC 21 SCAR 15.9
CC
21
Cov
56%
headers
12.0
from_psgi
10.5
__ANON__
6.0
CRAP 4.9
« blib/lib/YATT/Lite/CGen.pm ↑ Summary scripts/yatt »
1 package YATT::Lite::Response; sub MY () {__PACKAGE__}
2 72 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 72 ▶use warnings qw(FATAL all NONFATAL misc);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 72 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 72 ▶use mro 'c3';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 72 ▶use parent qw(YATT::Lite::Object);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 72 ▶use YATT::Lite::MFields qw/status headers body/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 72 require Encode;
11  
12 #========================================
13 # Constructors
14 #========================================
15  
16 # Wrap a PSGI response (ARRAY triple, or delayed/streaming CODE)
17 # into a YATT::Lite::Response.
18 10 ▶sub from_psgi {
Subroutine from_psgi: called CC=6 SCAR=23.5
Pod undocumented
19 10 my ($pack, $res) = @_;
20 10 ▶ if (ref $res eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $res eq "CODE") 0 10 10
21 0 $res = $pack->_run_streaming($res);
22 }
23 10 ▶ unless (ref $res eq 'ARRAY' and @$res >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $res eq "ARRAY" and @$res >= 2) 0 10 10
Condition33%1/3
ref $res eq "ARRAY" and @$res >= 2
!ll&&!rl&&r
0010
MC/DC0%0/2
ref $res eq "ARRAY" and @$res >= 2
ref $res eq "ARRAY" @$res >= 2
Truth table33%1/3
A and B
A= ref $res eq "ARRAY"B= @$res >= 2
ABresult
0X0
100
111
24 0 ▶ croak "Invalid PSGI response: ".(defined $res ? $res : '(undef)');
Branch: 1 branch
BranchTrueFalseTotal
defined $res ? : 0 0 0
25 }
26 10 my ($status, $headers, $body) = @$res;
27 10 ▶ $pack->new(status => $status
Branch: 1 branch
BranchTrueFalseTotal
$headers ? : 10 0 10
28 , headers => ($headers ? [@$headers] : [])
29 , body => $pack->_collect_body($body));
30 }
31  
32 0 ▶sub _run_streaming {
Subroutine _run_streaming: not called CC=1 SCAR=6.9
33 0 my ($pack, $app) = @_;
34 0 my ($status, $headers, $body, @chunks);
35 0 ▶ $app->(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
36 0 my ($tuple) = @_;
37 0 ($status, $headers) = @$tuple[0, 1];
38 0 ▶ if (@$tuple >= 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$tuple >= 3) 0 0 0
39 0 $body = $tuple->[2];
40 0 return;
41 }
42 0 $body = \@chunks;
43 0 return MY."::Writer"->new(\@chunks);
44 0 });
45 0 [$status, $headers, $body];
46 }
47  
48 10 ▶sub _collect_body {
Subroutine _collect_body: called CC=3 SCAR=16.3
49 10 my ($pack, $body) = @_;
50 10 ▶ if (not defined $body) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $body) { } 0 10 10
elsif (ref $body eq 'ARRAY') { } 10 0 10
51 0 [];
52 } elsif (ref $body eq 'ARRAY') {
53 10 [@$body];
54 } else {
55 # IO::Handle-ish body (including real filehandles).
56 0 require Plack::Util;
57 0 my @chunks;
58 0 ▶ Plack::Util::foreach($body, sub {push @chunks, $_[0]});
Subroutine __ANON__: not called CC=1 SCAR=6.9
59 0 \@chunks;
60 }
61 }
62  
63 #========================================
64 # Accessors
65 #========================================
66  
67 8 ▶sub status {
Subroutine status: called CC=1 SCAR=0.0
Pod undocumented
68 8 (my MY $self) = @_;
69 8 $self->{status};
70 }
71  
72 14 ▶sub is_success {
Subroutine is_success: called CC=3 SCAR=11.7
Pod undocumented
73 14 (my MY $self) = @_;
74 14 ▶ my $status = $self->{status} // 0;
Condition50%1/2
$self->{"status"} // 0
l!l
140
MC/DC0%0/1
$self->{"status"} // 0
$self->{"status"}
Truth table50%1/2
A
A= $self->{"status"}
Aresult
11
00
75 14 ▶ $status >= 200 && $status < 300;
Branch: 1 branch
BranchTrueFalseTotal
$status >= 200 and $status < 300 14 0 14
76 }
77  
78 0 ▶sub headers {
Subroutine headers: not called CC=3 SCAR=24.8
Pod undocumented
79 0 (my MY $self) = @_;
80 0 ▶ my $list = $self->{headers} // [];
Condition0%0/2
$self->{"headers"} // []
l!l
00
MC/DC0%0/1
$self->{"headers"} // []
$self->{"headers"}
Truth table0%0/2
A
A= $self->{"headers"}
Aresult
11
00
81 0 ▶ wantarray ? @$list : $list;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
82 }
83  
84 3 ▶sub header {
Subroutine header: called CC=5 SCAR=16.6
Pod undocumented
85 3 (my MY $self, my $name) = @_;
86 3 ▶ my $list = $self->{headers} // [];
Condition50%1/2
$self->{"headers"} // []
l!l
30
MC/DC0%0/1
$self->{"headers"} // []
$self->{"headers"}
Truth table50%1/2
A
A= $self->{"headers"}
Aresult
11
00
87 3 my @found;
88 3 ▶ for (my $i = 0; $i + 1 <= $#$list; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i + 1 <= $list->$#*) 3 3 6
89 3 ▶ push @found, $list->[$i+1] if lc($list->[$i]) eq lc($name);
Branch: 1 branch
BranchTrueFalseTotal
if lc $list->[$i] eq lc $name 3 0 3
90 }
91 3 ▶ wantarray ? @found : $found[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 3 3
92 }
93  
94 # Raw body: list of chunks in list context, joined octets in scalar context.
95 7 ▶sub body {
Subroutine body: called CC=3 SCAR=11.4
Pod undocumented
96 7 (my MY $self) = @_;
97 7 ▶ my $list = $self->{body} // [];
Condition50%1/2
$self->{"body"} // []
l!l
70
MC/DC0%0/1
$self->{"body"} // []
$self->{"body"}
Truth table50%1/2
A
A= $self->{"body"}
Aresult
11
00
98 7 ▶ wantarray ? @$list : join('', grep {defined} @$list);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 7 7
99 }
100  
101 # Decoded text (utf-8 unless already decoded).
102 17 ▶sub content {
Subroutine content: called CC=3 SCAR=11.3
Pod undocumented
103 17 (my MY $self) = @_;
104 17 ▶ my $raw = join '', grep {defined} @{$self->{body} // []};
Condition50%1/2
$self->{"body"} // []
l!l
170
MC/DC0%0/1
$self->{"body"} // []
$self->{"body"}
Truth table50%1/2
A
A= $self->{"body"}
Aresult
11
00
105 17 ▶ utf8::is_utf8($raw) ? $raw : Encode::decode(utf8 => $raw);
Branch: 1 branch
BranchTrueFalseTotal
utf8::is_utf8($raw) ? : 0 17 17
106 }
107  
108 #========================================
109 # Minimal writer for streaming PSGI responses.
110 #========================================
111 {
112 package YATT::Lite::Response::Writer;
113 0 ▶ sub new {
Subroutine new: not called CC=1 SCAR=6.9
114 0 my ($pack, $chunks) = @_;
115 0 bless [$chunks], $pack;
116 }
117 0 ▶ sub write {
Subroutine write: not called CC=1 SCAR=6.9
118 0 my $self = shift;
119 0 push @{$self->[0]}, @_;
120 }
121 0 ▶ sub close {}
Subroutine close: not called CC=1 SCAR=0.0
122 }
123  
124 72 1;
« blib/lib/YATT/Lite/CGen.pm ↑ Summary scripts/yatt »