File Coverage

blib/lib/Catalyst/TraitFor/Request/QueryFromJSONY.pm
Criterion Covered Total %
statement 6 12 50.0
branch 0 2 0.0
condition n/a
subroutine 2 4 50.0
pod n/a
total 8 18 44.4


line stmt bran cond sub pod time code
1             package Catalyst::TraitFor::Request::QueryFromJSONY;
2              
3 1     1   982 use Moose::Role;
  1         537593  
  1         3  
4 1     1   7005 use JSONY;
  1         33877  
  1         226  
5              
6             our $VERSION = '0.001';
7              
8             has query_data => (
9             is=>'ro',
10             lazy=>1,
11             predicate=>'has_query_data',
12             builder=>'_build_query_data');
13              
14             sub _build_query_data {
15 0     0     return shift->_query_data_from('q');
16             }
17              
18             sub _query_data_from {
19 0     0     my ($self, $param) = @_;
20 0           my $q = $self->query_parameters;
21 0 0         if(exists $q->{$param}) {
22 0           return JSONY->new->load($q->{$param});
23             } else {
24 0           return;
25             }
26             }
27              
28             1;
29              
30             =head1 NAME
31              
32             Catalyst::TraitFor::Request::QueryFromJSONY - Handle complex query parameters using JSONY
33              
34             =head1 SYNOPSIS
35              
36             For L<Catalyst> v5.90090+
37              
38             package MyApp;
39              
40             use Catalyst;
41              
42             MyApp->request_class_traits(['Catalyst::TraitFor::Request::QueryFromJSONY']);
43             MyApp->setup;
44              
45             For L<Catalyst> older than v5.90090
46              
47             package MyApp;
48              
49             use Catalyst;
50             use CatalystX::RoleApplicator;
51              
52             MyApp->apply_request_class_roles('Catalyst::TraitFor::Request::QueryFromJSONY');
53             MyApp->setup;
54              
55             In a controller:
56              
57             package MyApp::Controller::Example;
58              
59             use Moose;
60             use MooseX::MethodAttributes;
61             use Data::Dumper;
62              
63             sub echo :Local {
64             my ($self, $c) = @_;
65             $c->res->body( Dumper $c->req->query_data );
66             }
67              
68             Example test case:
69              
70             ok my $res = request GET "/example/echo?q={'id':100,'age':['>',10]}";
71             is_deeply eval $res->content, {
72             'id' => 100,
73             'age' => [ '>', 10 ]
74             };
75              
76             =head1 DESCRIPTION
77              
78             This is an early access release of this module. Experimentation as to the best
79             approach is ongoing.
80              
81             There are cases when you'd like to express complex data structures in your URL
82             query part (tha bit after the '?'). There's been a number of attempts at this,
83             this module is yet another. In this version we allow for a query parameter 'q'
84             to be a L<JSONY> serialized string (L<JSONY> is basically JSON relaxed a bit to
85             reduce a bit of verbosity and smooth over common errors that are more pedantic
86             that useful). We deserialize this string and place its value in 'query_data'.
87              
88             This only happens if you request the query_data attribute, so there's no overhead
89             to simply having this installed.
90              
91             You can have other 'classic' query parameters mixed in with the 'q' parameter, but
92             for no only 'q' is deserialized. The original value of 'q' is preserved in the
93             original query_parameter method.
94              
95             =head1 METHODS
96              
97             This role defines the following methods.
98              
99             =head2 query_data
100              
101             If a query parameter 'q' exists, deserialize that using L<JSONY> and return the
102             data references (could be a hashref, or arrayref depending on the query construction.
103              
104             =head2 has_query_data
105              
106             Returns true if query_data is present in the request.
107              
108             =head1 AUTHOR
109            
110             John Napiorkowski L<email:jjnapiork@cpan.org>
111            
112             =head1 SEE ALSO
113            
114             L<Catalyst>, L<Catalyst::Request>, L<JSONY>
115              
116             =head1 COPYRIGHT & LICENSE
117            
118             Copyright 2015, John Napiorkowski L<email:jjnapiork@cpan.org>
119            
120             This library is free software; you can redistribute it and/or modify it under
121             the same terms as Perl itself.
122              
123             =cut