| 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
Condition33%1/3
$conf->{"coders"} // __PACKAGE__->_default_coders
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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"
MC/DC0%0/1
$conf->{"default_decode"} // "application/x-www-form-urlencoded"
$conf->{"default_decode"}
Truth table50%1/2
A
A= $conf->{"default_decode"}
| |||||||||||||||||||||||||||||
| 56 | 100 | ▶ | my $default_encode = $conf->{default_encode} // 'application/json'; # TODO: not used | ||||||||||||||||||||||||||
|
Condition50%1/2
$conf->{"default_encode"} // "application/json"
MC/DC0%0/1
$conf->{"default_encode"} // "application/json"
$conf->{"default_encode"}
Truth table50%1/2
A
A= $conf->{"default_encode"}
| |||||||||||||||||||||||||||||
| 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') || ''
$headers->content_type || ''
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')
Truth table50%1/2
A
A= $headers->content_type
| |||||||||||||||||||||||||||||
| 64 | split(',', $headers->header('Accept') || ''), | ||||||||||||||||||||||||||||
| 65 | $headers->content_type || '') | ||||||||||||||||||||||||||||
| 66 | { | ||||||||||||||||||||||||||||
| 67 | 8 | ▶ | return $type if $types{$type} and $types{$type}->{encode}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$types{$type} and $types{$type}{"encode"}
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"}
| |||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 6 | ▶ | my $format = $c->stash->{format} // 'json'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$c->stash->{"format"} // "json"
MC/DC0%0/1
$c->stash->{"format"} // "json"
$c->stash->{"format"}
Truth table50%1/2
A
A= $c->stash->{"format"}
| |||||||||||||||||||||||||||||
| 71 | 6 | ▶ | $format = 'json' unless $formats{$format}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
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
| |||||||||||||||||||||||||||||
| 90 | 0 | ▶ | if ($content_type =~ /^([^;]+);/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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}
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}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
MC/DC0%0/1
$args->{"mime_type"} // 'application/x-' . $ext
$args->{"mime_type"}
Truth table0%0/2
A
A= $args->{"mime_type"}
| |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 0 | $formats{$ext} = $mime; | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 0 | ▶ | if(defined $args->{encode}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 0 | $types{$mime}->{encode} = $args->{encode}; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 0 | ▶ | if(defined $args->{decode}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
exists $c->stash->{"autodata"} or exists $args->{"autodata"}
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"}
| |||||||||||||||||||||||||||||
| 126 | 100 | }); | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 46 | 1; | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | __END__ | ||||||||||||||||||||||||||||