blib/lib/Clustericious/Plugin/AutodataHandler.pm

filter: Statementstmt 79.5% 66 / 83 Branchbran 45.0% 9 / 20 Conditioncond 40.7% 11 / 27 MC/DCmcdc 18.7% 3 / 16 Subroutinesub 80.0% 12 / 15 Podpod 100% 1 / 1 totaltotal 62.9% 102 / 162 CC 24 SCAR 23.6
CC
24
Cov
66%
__ANON__
30.0
__ANON__
20.0
register
11.6
CRAP 10.6
« blib/lib/Test/Clustericious/Command.pm ↑ Summary blib/lib/Test/Clustericious/Log.pm »
1 package Clustericious::Plugin::AutodataHandler;
2  
3 46 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 46 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 46 ▶use base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 46 ▶use Mojo::ByteStream 'b';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 46 ▶use Clustericious::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 46 ▶use PerlX::Maybe qw( maybe );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 46 ▶use Path::Class qw( dir );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # ABSTRACT: Handle data types automatically
12 46 our $VERSION = '1.29'; # VERSION
13  
14  
15 sub _default_coders
16 100 ▶{
Subroutine _default_coders: called CC=1 SCAR=0.0
17 my %coders =
18 844 map { $_ => 1 }
19 844 map { $_ =~ s/\.pm$//; $_ }
20 844 map { $_->basename }
21 844 grep { ! $_->is_dir }
22 422 map { $_->children( no_hidden => 1 ) }
23 1352 grep { -d $_ }
24 100 map { dir $_, 'Clustericious', 'Coder' } @INC;
25 100 [ keys %coders ];
26 }
27  
28 sub register
29 100 ▶{
Subroutine register: called CC=7 SCAR=24.5
Pod documented
30 100 my ($self, $app, $conf) = @_;
31  
32 my @coders = $app->isa('Clustericious::App')
33 ? $app->config->coders( default => __PACKAGE__->_default_coders )
34 100 ▶ : @{ $conf->{coders} // __PACKAGE__->_default_coders };
Branch: 1 branch
BranchTrueFalseTotal
$app->isa('Clustericious::App') ? : 98 2 100
Condition33%1/3
$conf->{"coders"} // __PACKAGE__->_default_coders
l!l&&r!l&&!r
020
MC/DC0%0/2
$conf->{"coders"} // __PACKAGE__->_default_coders
$conf->{"coders"} __PACKAGE__->_default_coders
Truth table33%1/3
A // B
A= $conf->{"coders"}B= __PACKAGE__->_default_coders
ABresult
1X1
011
000
35
36 my %types = (
37 'application/x-www-form-urlencoded' => {
38 0 ▶ decode => sub { my ($data, $c) = @_; $c->req->params->to_hash }
Subroutine __ANON__: not called CC=1 SCAR=6.9
39 }
40 100 );
41 100 my %formats;
42
43 100 foreach my $coder (@coders)
44 {
45 200 require join('/', qw( Clustericious Coder ), "$coder.pm");
46 200 my $coder = join('::', qw( Clustericious Coder ), $coder)->coder;
47 $types{$coder->{type}} = {
48 maybe encode => $coder->{encode},
49 maybe decode => $coder->{decode},
50 },
51 $formats{$coder->{format}} = $coder->{type}
52 200 ▶ if $coder->{format};
Branch: 1 branch
BranchTrueFalseTotal
if $coder->{"format"} 200 0 200
53 }
54  
55 100 ▶ my $default_decode = $conf->{default_decode} // 'application/x-www-form-urlencoded';
Condition50%1/2
$conf->{"default_decode"} // "application/x-www-form-urlencoded"
l!l
0100
MC/DC0%0/1
$conf->{"default_decode"} // "application/x-www-form-urlencoded"
$conf->{"default_decode"}
Truth table50%1/2
A
A= $conf->{"default_decode"}
Aresult
11
00
56 100 ▶ my $default_encode = $conf->{default_encode} // 'application/json'; # TODO: not used
Condition50%1/2
$conf->{"default_encode"} // "application/json"
l!l
0100
MC/DC0%0/1
$conf->{"default_encode"} // "application/json"
$conf->{"default_encode"}
Truth table50%1/2
A
A= $conf->{"default_encode"}
Aresult
11
00
57  
58 8 ▶ my $find_type = sub {
Subroutine __ANON__: called CC=8 SCAR=21.3
59 8 my ($c) = @_;
60  
61 8 my $headers = $c->tx->req->content->headers;
62  
63 8 ▶ foreach my $type (map { /^([^;]*)/ } # get only stuff before ;
Condition75%3/4
$headers->header('Accept') || ''
l!l
26
$headers->content_type || ''
l!l
08
MC/DC100%1/1
$headers->header('Accept') || ''
$headers->header('Accept')
MC/DC0%0/1
$headers->content_type || ''
$headers->content_type
Truth table100%2/2
A
A= $headers->header('Accept')
Aresult
11
00
Truth table50%1/2
A
A= $headers->content_type
Aresult
11
00
64 split(',', $headers->header('Accept') || ''),
65 $headers->content_type || '')
66 {
67 8 ▶ return $type if $types{$type} and $types{$type}->{encode};
Branch: 1 branch
BranchTrueFalseTotal
if $types{$type} and $types{$type}{"encode"} 2 6 8
Condition66%2/3
$types{$type} and $types{$type}{"encode"}
!ll&&!rl&&r
602
MC/DC50%1/2
$types{$type} and $types{$type}{"encode"}
$types{$type} $types{$type}{"encode"}
Truth table66%2/3
A and B
A= $types{$type}B= $types{$type}{"encode"}
ABresult
0X0
100
111
68 }
69  
70 6 ▶ my $format = $c->stash->{format} // 'json';
Condition50%1/2
$c->stash->{"format"} // "json"
l!l
06
MC/DC0%0/1
$c->stash->{"format"} // "json"
$c->stash->{"format"}
Truth table50%1/2
A
A= $c->stash->{"format"}
Aresult
11
00
71 6 ▶ $format = 'json' unless $formats{$format};
Branch: 1 branch
BranchTrueFalseTotal
unless $formats{$format} 0 6 6
72  
73 6 $formats{$format};
74 100 };
75  
76  
77 8 ▶ $app->renderer->add_handler('autodata' => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
78 8 my ($r, $c, $output, $data) = @_;
79  
80 8 my $type = $find_type->($c);
81 8 ▶ LOGDIE "no encoder for $type" unless $types{$type}->{encode};
Branch: 1 branch
BranchTrueFalseTotal
unless $types{$type}{"encode"} 0 8 8
82 8 $$output = $types{$type}->{encode}->($c->stash("autodata"));
83 8 $c->tx->res->headers->content_type($type);
84 100 });
85  
86 0 ▶ my $parse_autodata = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
87 0 my ($c) = @_;
88  
89 0 ▶ my $content_type = $c->req->headers->content_type || $default_decode;
Condition0%0/3
$c->req->headers->content_type || $default_decode
l!l&&r!l&&!r
000
MC/DC0%0/2
$c->req->headers->content_type || $default_decode
$c->req->headers->content_type $default_decode
Truth table0%0/3
A || B
A= $c->req->headers->content_typeB= $default_decode
ABresult
1X1
011
000
90 0 ▶ if ($content_type =~ /^([^;]+);/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content_type =~ /^([^;]+);/) 0 0 0
91 # strip charset
92 0 $content_type = $1;
93 }
94 0 ▶ my $entry = $types{$content_type} || $types{$default_decode};
Condition0%0/3
$types{$content_type} || $types{$default_decode}
l!l&&r!l&&!r
000
MC/DC0%0/2
$types{$content_type} || $types{$default_decode}
$types{$content_type} $types{$default_decode}
Truth table0%0/3
A || B
A= $types{$content_type}B= $types{$default_decode}
ABresult
1X1
011
000
95  
96 # TODO: avoid passing $c in, only used by
97 # application/x-www-form-urlencoded above
98 0 $c->stash->{autodata} = $entry->{decode}->($c->req->body, $c);
99 100 };
100  
101 100 $app->plugins->on( parse_autodata => $parse_autodata );
102  
103 0 ▶ $app->plugins->on( add_autodata_type => sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
104 0 my($plugins, $args) = @_;
105  
106 0 ▶ LOGDIE "No extension provided" unless defined $args->{extension};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"extension"} 0 0 0
107 0 my $ext = $args->{extension};
108 0 ▶ my $mime = $args->{mime_type} // 'application/x-' . $ext;
Condition0%0/2
$args->{"mime_type"} // 'application/x-' . $ext
l!l
00
MC/DC0%0/1
$args->{"mime_type"} // 'application/x-' . $ext
$args->{"mime_type"}
Truth table0%0/2
A
A= $args->{"mime_type"}
Aresult
11
00
109  
110 0 $formats{$ext} = $mime;
111
112 0 ▶ if(defined $args->{encode}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"encode"}) 0 0 0
113 0 $types{$mime}->{encode} = $args->{encode};
114 }
115
116 0 ▶ if(defined $args->{decode}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"decode"}) 0 0 0
117 0 $types{$mime}->{decode} = $args->{decode};
118 }
119 100 });
120  
121 100 $app->helper( parse_autodata => $parse_autodata );
122
123 23 ▶ $app->hook(before_render => sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
124 23 my($c, $args) = @_;
125 23 ▶ $c->stash->{handler} = "autodata" if exists($c->stash->{autodata}) || exists($args->{autodata});
Branch: 1 branch
BranchTrueFalseTotal
if exists $c->stash->{"autodata"} or exists $args->{"autodata"} 12 11 23
Condition66%2/3
exists $c->stash->{"autodata"} or exists $args->{"autodata"}
l!l&&r!l&&!r
12011
MC/DC50%1/2
exists $c->stash->{"autodata"} or exists $args->{"autodata"}
exists $c->stash->{"autodata"} exists $args->{"autodata"}
Truth table66%2/3
A or B
A= exists $c->stash->{"autodata"}B= exists $args->{"autodata"}
ABresult
1X1
011
000
126 100 });
127 }
128  
129 46 1;
130  
131 __END__
« blib/lib/Test/Clustericious/Command.pm ↑ Summary blib/lib/Test/Clustericious/Log.pm »