Chouette/Context.pm

filter: Statementstmt 63.3% 45 / 71 Branchbran 20.0% 4 / 20 Conditioncond 26.6% 4 / 15 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 65.0% 13 / 20 Podpod 0.0% 0 / 11 totaltotal 45.8% 66 / 144 SCAR 41.6
CC
19
Cov
50%
_json_clean
42.0
task
20.0
respond
11.3
CRAP 64.1
« Chouette.pm ↑ Summary
1 package Chouette::Context;
2  
3 1 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use Plack::Request::WithEncoding;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Plack::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Log::Defer;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12 sub new {
13 1 ▶ my ($class, %args) = @_;
Subroutine new: called CC=1 SCAR=0.4
Pod undocumented
14  
15 1 my $self = \%args;
16 1 bless $self, $class;
17  
18 1 $self->{req} = Plack::Request::WithEncoding->new($self->{env});
19 1 $self->{req}->env->{'plack.request.withencoding.encoding'} = 'utf-8';
20  
21  
22 1 my $raw_logger = $self->{chouette}->{raw_logger};
23  
24 $self->{log_defer_obj} = Log::Defer->new({ cb => sub {
25 1 ▶ my $msg = shift;
Subroutine __ANON__: called CC=3 SCAR=14.8
26  
27 1 my $encoded_msg = eval { encode_json($msg) };
28  
29 1 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
30 0 $encoded_msg = eval { encode_json(_json_clean($msg)) };
31  
32 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
33 0 $encoded_msg = "Failed to JSON clean: " . Dumper($msg);
34 }
35 }
36  
37 1 $raw_logger->log("$encoded_msg\n");
38 1 }});
39  
40 1 return $self;
41 }
42  
43  
44 0 ▶sub config { shift->{chouette}->{config} }
Subroutine config: not called CC=1 SCAR=6.9
Pod undocumented
45 2 ▶sub logger { shift->{log_defer_obj} }
Subroutine logger: called CC=1 SCAR=0.0
Pod undocumented
46 1 ▶sub req { shift->{req} }
Subroutine req: called CC=1 SCAR=0.0
Pod undocumented
47 0 ▶sub res { die "Plack response object not yet supported" }
Subroutine res: not called CC=1 SCAR=6.9
Pod undocumented
48 0 ▶sub route_params { shift->{route_params} // {} }
Subroutine route_params: not called CC=2 SCAR=17.9
Pod undocumented
Condition0%0/2
+(shift())->{"route_params"} // {}
l!l
00
MC/DC0%0/1
+(shift())->{"route_params"} // {}
+(shift())->{"route_params"}
Truth table0%0/2
A
A= +(shift())->{"route_params"}
Aresult
11
00
49  
50  
51 sub respond_raw {
52 1 ▶ my ($self, $http_code, $mime_type, $body) = @_;
Subroutine respond_raw: called CC=2 SCAR=7.1
Pod undocumented
53  
54 1 ▶ if (defined $self->{responder}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'responder'}) { } 1 0 1
55 1 $self->logger->info("HTTP response: $http_code (" . length($body) . " bytes of $mime_type)");
56 1 $self->{responder}->([$http_code, ["Content-Type" => $mime_type], [$body]]);
57 1 undef $self->{responder};
58 } else {
59 0 $self->logger->info("Not replying with $http_code because something else already replied.");
60 }
61  
62 1 return $self->{chouette}->{_done_gensym};
63 }
64  
65  
66 sub DESTROY {
67 1 ▶ my $self = shift;
Subroutine DESTROY: called CC=2 SCAR=9.2
68  
69 1 ▶ if (defined $self->{responder}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"responder"}) 0 1 1
70 0 $self->logger->error("no response was sent, sending 500");
71 0 $self->respond({ error => 'internal server error' }, 500);
72 }
73 }
74  
75  
76 sub respond {
77 1 ▶ my ($self, $body, $http_code) = @_;
Subroutine respond: called CC=6 SCAR=24.3
Pod undocumented
78  
79 1 ▶ if (defined $http_code && $http_code != 200 && ref($body) eq 'HASH' && exists $body->{error}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $http_code and $http_code != 200 and ref $body eq "HASH" and exists $body->{"error"}) 0 1 1
Condition33%3/9
defined $http_code and $http_code != 200 and ref $body eq "HASH" and exists $body->{"error"}
!ll&&!rl&&r
100
defined $http_code and $http_code != 200 and ref $body eq "HASH"
!ll&&!rl&&r
100
defined $http_code and $http_code != 200
!ll&&!rl&&r
100
MC/DC0%0/4
defined $http_code and $http_code != 200 and ref $body eq "HASH" and exists $body->{"error"}
defined $http_code $http_code != 200 ref $body eq "HASH" exists $body->{"error"}
Truth table20%1/5
A and B and C and D
A= defined $http_codeB= $http_code != 200C= ref $body eq "HASH"D= exists $body->{"error"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
80 0 $self->logger->warn("sending JSON error: $body->{error}");
81 }
82  
83 1 ▶ return $self->respond_raw($http_code // 200, 'application/json', encode_json($body));
Condition50%1/2
$http_code // 200
l!l
01
MC/DC0%0/1
$http_code // 200
$http_code
Truth table50%1/2
A
A= $http_code
Aresult
11
00
84 }
85  
86  
87  
88 sub done {
89 0 ▶ die shift->{chouette}->{_done_gensym};
Subroutine done: not called CC=1 SCAR=6.9
Pod undocumented
90 }
91  
92  
93 sub generate_token {
94 0 ▶ shift->{chouette}->generate_token();
Subroutine generate_token: not called CC=1 SCAR=6.9
Pod undocumented
95 }
96  
97  
98  
99 sub task {
100 0 ▶ my ($self, $task_name, %checkout_opts) = @_;
Subroutine task: not called CC=4 SCAR=30.0
Pod undocumented
101  
102 0 ▶ return $self->{task_checkouts}->{$task_name} if $self->{task_checkouts}->{$task_name};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"task_checkouts"}{$task_name} 0 0 0
103  
104 0 ▶ my $client = $self->{chouette}->{task_clients}->{$task_name} // die "no such task: '$task_name'";
Condition0%0/2
$self->{"chouette"}{"task_clients"}{$task_name} // die("no such task: '${task_name}'")
l!l
00
MC/DC0%0/1
$self->{"chouette"}{"task_clients"}{$task_name} // die("no such task: '${task_name}'")
$self->{"chouette"}{"task_clients"}{$task_name}
Truth table0%0/2
A
A= $self->{"chouette"}{"task_clients"}{$task_name}
Aresult
11
00
105 0 my $checkout = $client->checkout(log_defer_object => $self->logger, %checkout_opts);
106  
107 0 ▶ $self->{task_checkouts}->{$task_name} = $checkout if $self->{chouette}->{task_checkout_caching}->{$task_name};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"chouette"}{"task_checkout_caching"}{$task_name} 0 0 0
108  
109 0 return $checkout;
110 }
111  
112  
113  
114  
115  
116 ########
117  
118  
119 sub _json_clean {
120 0 ▶ my $x = shift;
Subroutine _json_clean: not called CC=6 SCAR=37.4
121  
122 0 ▶ if (ref $x) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $x) 0 0 0
123 0 ▶ if (ref $x eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $x eq 'ARRAY') { } 0 0 0
elsif (ref $x eq 'HASH') { } 0 0 0
124 0 $x->[$_] = _json_clean($x->[$_]) for 0 .. @$x-1;
125 } elsif (ref $x eq 'HASH') {
126 0 $x->{$_} = _json_clean($x->{$_}) for keys %$x;
127 } else {
128 0 $x = "Unable to JSON encode: " . Dumper($x);
129 }
130 }
131  
132 0 return $x;
133 }
134  
135  
136  
137 1 1;
« Chouette.pm ↑ Summary