DBIC/Role/JsonParams.pm

filter: Statementstmt 100% 34 / 34 Branchbran 100% 8 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod 0.0% 0 / 2 totaltotal 96.0% 49 / 51 CC 7 SCAR 11.0
CC
7
Cov
100%
decode_rich_parameters
5.0
param
3.0
BEGIN
1.0
CRAP 3.0
« DBIC/Route.pm ↑ Summary DBIC/Router.pm »
1 package WebAPI::DBIC::Role::JsonParams;
2 25 $WebAPI::DBIC::Role::JsonParams::VERSION = '0.004002';
3  
4 25 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 25 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 25 ▶use Hash::MultiValue;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 25 ▶use JSON::MaybeXS qw(JSON);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10  
11 25 requires 'request';
12  
13  
14 25 my $json = JSON->new->allow_nonref;
15  
16  
17 25 has parameters => (
18 is => 'rw',
19 lazy => 1,
20 builder => '_build_parameters',
21 );
22  
23 149 ▶sub _build_parameters {
Subroutine _build_parameters: called CC=1 SCAR=0.0
24 149 my $self = shift;
25 149 return $self->decode_rich_parameters($self->request->query_parameters);
26 }
27  
28  
29 529 ▶sub param { ## no critic (RequireArgUnpacking)
Subroutine param: called CC=3 SCAR=11.0
Pod undocumented
30 529 my $self = shift;
31  
32 529 ▶ return keys %{ $self->parameters } if @_ == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 0 149 380 529
33  
34 380 my $key = shift;
35 380 ▶ return $self->parameters->{$key} unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 226 154 380
36 154 return $self->parameters->get_all($key);
37 }
38  
39  
40 149 ▶sub decode_rich_parameters { # perhaps should live in a util library and be imported
Subroutine decode_rich_parameters: called CC=5 SCAR=16.1
Pod undocumented
41 149 my ($class, $raw_params) = @_;
42  
43 # Note that this is transparent to duplicate query parameter names
44 # i.e., foo=7&foo=8&foo~json=9 will result in the same set of duplicate
45 # parameters as if the parameters were foo=7&foo=8&foo=9
46  
47 149 my @params;
48 149 for my $key_raw (keys %$raw_params) {
49  
50 # parameter names with a ~json suffix have JSON encoded values
51 154 my $is_json;
52 154 ▶ (my $key_base = $key_raw) =~ s/~json$//
Branch: 1 branch
BranchTrueFalseTotal
(my $key_base = $key_raw) =~ s/~json$// and $is_json = 1 20 134 154
53 and $is_json = 1;
54  
55 154 for my $v ($raw_params->get_all($key_raw)) {
56 154 ▶ $v = $json->decode($v) if $is_json;
Branch: 1 branch
BranchTrueFalseTotal
if $is_json 20 134 154
57 154 push @params, $key_base, $v;
58 }
59 }
60  
61 149 return Hash::MultiValue->new(@params);
62 }
63  
64  
65  
66 25 1;
67  
68 __END__
« DBIC/Route.pm ↑ Summary DBIC/Router.pm »