| 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
| |||||||||||
| 33 | |||||||||||
| 34 | 380 | my $key = shift; | |||||||||
| 35 | 380 | ▶ | return $self->parameters->{$key} unless wantarray; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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
| |||||||||||
| 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
| |||||||||||
| 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__ | ||||||||||