| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package WebAPI::DBIC::Role::JsonParams; | 
| 2 |  |  |  |  |  |  | $WebAPI::DBIC::Role::JsonParams::VERSION = '0.003002'; | 
| 3 |  |  |  |  |  |  |  | 
| 4 | 2 |  |  | 2 |  | 11397400 | use Moo::Role; | 
|  | 2 |  |  |  |  | 46180 |  | 
|  | 2 |  |  |  |  | 9 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 2 |  |  | 2 |  | 641 | use Carp qw(croak); | 
|  | 2 |  |  |  |  | 9 |  | 
|  | 2 |  |  |  |  | 197 |  | 
| 7 | 2 |  |  | 2 |  | 1188 | use Hash::MultiValue; | 
|  | 2 |  |  |  |  | 4296 |  | 
|  | 2 |  |  |  |  | 23 |  | 
| 8 | 2 |  |  | 2 |  | 1312 | use JSON::MaybeXS qw(JSON); | 
|  | 2 |  |  |  |  | 1329 |  | 
|  | 2 |  |  |  |  | 678 |  | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | requires 'request'; | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | my $json = JSON->new->allow_nonref; | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | has parameters => ( | 
| 18 |  |  |  |  |  |  | is => 'rw', | 
| 19 |  |  |  |  |  |  | lazy => 1, | 
| 20 |  |  |  |  |  |  | builder => '_build_parameters', | 
| 21 |  |  |  |  |  |  | ); | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | sub _build_parameters { | 
| 24 | 0 |  |  | 0 |  |  | my $self = shift; | 
| 25 | 0 |  |  |  |  |  | return $self->decode_rich_parameters($self->request->query_parameters); | 
| 26 |  |  |  |  |  |  | } | 
| 27 |  |  |  |  |  |  |  | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | sub param { ## no critic (RequireArgUnpacking) | 
| 30 | 0 |  |  | 0 | 0 |  | my $self = shift; | 
| 31 |  |  |  |  |  |  |  | 
| 32 | 0 | 0 |  |  |  |  | return keys %{ $self->parameters } if @_ == 0; | 
|  | 0 |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  |  | 
| 34 | 0 |  |  |  |  |  | my $key = shift; | 
| 35 | 0 | 0 |  |  |  |  | return $self->parameters->{$key} unless wantarray; | 
| 36 | 0 |  |  |  |  |  | return $self->parameters->get_all($key); | 
| 37 |  |  |  |  |  |  | } | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  |  | 
| 40 |  |  |  |  |  |  | sub decode_rich_parameters { # perhaps should live in a util library and be imported | 
| 41 | 0 |  |  | 0 | 0 |  | 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 | 0 |  |  |  |  |  | my @params; | 
| 48 | 0 |  |  |  |  |  | for my $key_raw (keys %$raw_params) { | 
| 49 |  |  |  |  |  |  |  | 
| 50 |  |  |  |  |  |  | # parameter names with a ~json suffix have JSON encoded values | 
| 51 | 0 |  |  |  |  |  | my $is_json; | 
| 52 | 0 | 0 |  |  |  |  | (my $key_base = $key_raw) =~ s/~json$// | 
| 53 |  |  |  |  |  |  | and $is_json = 1; | 
| 54 |  |  |  |  |  |  |  | 
| 55 | 0 |  |  |  |  |  | for my $v ($raw_params->get_all($key_raw)) { | 
| 56 | 0 | 0 |  |  |  |  | $v = $json->decode($v) if $is_json; | 
| 57 | 0 |  |  |  |  |  | push @params, $key_base, $v; | 
| 58 |  |  |  |  |  |  | } | 
| 59 |  |  |  |  |  |  | } | 
| 60 |  |  |  |  |  |  |  | 
| 61 | 0 |  |  |  |  |  | return Hash::MultiValue->new(@params); | 
| 62 |  |  |  |  |  |  | } | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  |  | 
| 65 |  |  |  |  |  |  |  | 
| 66 |  |  |  |  |  |  | 1; | 
| 67 |  |  |  |  |  |  |  | 
| 68 |  |  |  |  |  |  | __END__ | 
| 69 |  |  |  |  |  |  |  | 
| 70 |  |  |  |  |  |  | =pod | 
| 71 |  |  |  |  |  |  |  | 
| 72 |  |  |  |  |  |  | =encoding UTF-8 | 
| 73 |  |  |  |  |  |  |  | 
| 74 |  |  |  |  |  |  | =head1 NAME | 
| 75 |  |  |  |  |  |  |  | 
| 76 |  |  |  |  |  |  | WebAPI::DBIC::Role::JsonParams | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | =head1 VERSION | 
| 79 |  |  |  |  |  |  |  | 
| 80 |  |  |  |  |  |  | version 0.003002 | 
| 81 |  |  |  |  |  |  |  | 
| 82 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 83 |  |  |  |  |  |  |  | 
| 84 |  |  |  |  |  |  | Provides methods to handle request parameters that have an encoding specified. | 
| 85 |  |  |  |  |  |  |  | 
| 86 |  |  |  |  |  |  | For example, given a request like C<</foo?bar~json={k:"hello"}>> the C<bar> | 
| 87 |  |  |  |  |  |  | parameter will be a reference to a hash containing a single element. | 
| 88 |  |  |  |  |  |  |  | 
| 89 |  |  |  |  |  |  | =head1 NAME | 
| 90 |  |  |  |  |  |  |  | 
| 91 |  |  |  |  |  |  | WebAPI::DBIC::Resource::Role::JsonParams - provides a param method that handles JSON | 
| 92 |  |  |  |  |  |  |  | 
| 93 |  |  |  |  |  |  | =head1 AUTHOR | 
| 94 |  |  |  |  |  |  |  | 
| 95 |  |  |  |  |  |  | Tim Bunce <Tim.Bunce@pobox.com> | 
| 96 |  |  |  |  |  |  |  | 
| 97 |  |  |  |  |  |  | =head1 COPYRIGHT AND LICENSE | 
| 98 |  |  |  |  |  |  |  | 
| 99 |  |  |  |  |  |  | This software is copyright (c) 2015 by Tim Bunce. | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  | This is free software; you can redistribute it and/or modify it under | 
| 102 |  |  |  |  |  |  | the same terms as the Perl 5 programming language system itself. | 
| 103 |  |  |  |  |  |  |  | 
| 104 |  |  |  |  |  |  | =cut |