lib/Net/API/REST/Query.pm

filter: Statementstmt 100% 20 / 20 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 100% 27 / 27 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« lib/Net/API/REST/DateTime.pm ↑ Summary lib/Net/API/REST/Request.pm »
1 ##----------------------------------------------------------------------------
2 ## REST API Framework - ~/lib/Net/API/REST/Query.pm
3 ## Version v1.0.0
4 ## Copyright(c) 2022 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2020/06/13
7 ## Modified 2023/06/10
8 ## All rights reserved
9 ##
10 ## This program is free software; you can redistribute it and/or modify it
11 ## under the same terms as Perl itself.
12 ##----------------------------------------------------------------------------
13 package Net::API::REST::Query;
14 BEGIN
15 {
16 2 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶ use parent qw( Apache2::API::Query );
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶ use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶ our $VERSION = 'v1.0.0';
Subroutine BEGIN: called CC=1 SCAR=0.0
21 };
22  
23 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 2 1;
27 # NOTE: pod
28 __END__
29  
30 =encoding utf8
31  
32 =head1 NAME
33  
34 Net::API::REST::Query - utf8 compliant URI query string manipulation
35  
36 =head1 SYNOPSIS
37  
38 # Constructor - using a GET query string
39 $qq = Net::API::REST::Query->new($query_string);
40 # OR Constructor - using a hashref of key => value parameters
41 $qq = Net::API::REST::Query->new($cgi->Vars);
42 # OR Constructor - using an array of successive keys and values
43 $qq = Net::API::REST::Query->new(@params);
44  
45 # Clone the current object
46 $qq2 = $qq->clone;
47  
48 # Revert back to the initial constructor state (to do it all again)
49 $qq->revert;
50  
51 # Remove all occurrences of the given parameters
52 $qq->strip('page', 'next');
53  
54 # Remove all parameters except the given ones
55 $qq->strip_except('pagesize', 'order');
56  
57 # Remove all empty/undefined parameters
58 $qq->strip_null;
59  
60 # Replace all occurrences of the given parameters
61 $qq->replace(page => $page, foo => 'bar');
62  
63 # Set the argument separator to use for output (default: unescaped '&')
64 $qq->separator(';');
65  
66 # Output the current query string
67 print "$qq"; # OR $qq->stringify;
68 # Stringify with explicit argument separator
69 $qq->stringify(';');
70  
71 # Output the current query string with a leading '?'
72 $qq->qstringify;
73 # Stringify with a leading '?' and an explicit argument separator
74 $qq->qstringify(';');
75  
76 # Get a flattened hash/hashref of the current parameters
77 # (single item parameters as scalars, multiples as an arrayref)
78 my %qq = $qq->hash;
79  
80 # Get a non-flattened hash/hashref of the current parameters
81 # (parameter => arrayref of values)
82 my %qq = $qq->hash_arrayref;
83  
84 # Get the current query string as a set of hidden input tags
85 print $qq->hidden;
86  
87 # Check whether the query has changed since construction
88 if ($qq->has_changed) {
89 print "changed version: $qq\n";
90 }
91  
92 =head1 VERSION
93  
94 v1.0.0
95  
96 =head1 DESCRIPTION
97  
98 As of version C<1.0.0>, this module fully inherits from L<Apache2::API::Query>.
99  
100 The rest below is taken from L<Apache2::API::Query> documentation and is copied here for convenience.
101  
102 =head2 CONSTRUCTOR
103  
104 Net::API::REST::Query objects can be constructed from scalar query strings ('foo=1&bar=2&bar=3'), from a hashref which has parameters as keys, and values either as scalars or arrayrefs of scalars (to handle the case of parameters with multiple values e.g. { foo => '1', bar => [ '2', '3' ] }), or arrays composed of successive parameters-value pairs e.g. ('foo', '1', 'bar', '2', 'bar', '3'). For instance:
105  
106 # Constructor - using a GET query string
107 $qq = Net::API::REST::Query->new($query_string);
108  
109 # Constructor - using an array of successive keys and values
110 $qq = Net::API::REST::Query->new(@params);
111  
112 # Constructor - using a hashref of key => value parameters,
113 # where values are either scalars or arrayrefs of scalars
114 $qq = Net::API::REST::Query->new($cgi->Vars);
115  
116 Net::API::REST::Query also handles L<CGI.pm>-style hashrefs, where multiple values are packed into a single string, separated by the "\0" (null) character.
117  
118 All keys and values are URI unescaped at construction time, and are stored and referenced unescaped. So a query string like:
119  
120 group=prod%2Cinfra%2Ctest&op%3Aset=x%3Dy
121  
122 is stored as:
123  
124 'group' => 'prod,infra,test'
125 'op:set' => 'x=y'
126  
127 You should always use the unescaped/normal variants in methods i.e.
128  
129 $qq->replace('op:set' => 'x=z');
130  
131 NOT:
132  
133 $qq->replace('op%3Aset' => 'x%3Dz');
134  
135 You can also construct a new Net::API::REST::Query object by cloning an existing one:
136  
137 $qq2 = $qq->clone;
138  
139  
140 =head2 MODIFIER METHODS
141  
142 All modifier methods change the state of the Net::API::REST::Query object in some way, and return $self, so they can be used in chained style e.g.
143  
144 $qq->revert->strip('foo')->replace(bar => 123);
145  
146 Note that Net::API::REST::Query stashes a copy of the parameter set that existed at construction time, so that any changes made by these methods can be rolled back using 'revert()'. So you don't (usually) need to keep multiple copies around to handle incompatible changes.
147  
148 =over 4
149  
150 =item revert()
151  
152 Revert the current parameter set back to that originally given at construction time i.e. discard all changes made since construction.
153  
154 =item strip($param1, $param2, ...)
155  
156 Remove all occurrences of the given parameters and their values from the current parameter set.
157  
158 =item strip_except($param1, $param2, ...)
159  
160 Remove all parameters EXCEPT those given from the current parameter
161 set.
162  
163 =item strip_null()
164  
165 Remove all parameters that have a value of undef from the current
166 parameter set.
167  
168 =item replace($param1 => $value1, $param2, $value2, ...)
169  
170 Replace the values of the given parameters in the current parameter set with these new ones. Parameter names must be scalars, but values can be either scalars or arrayrefs of scalars, when multiple values are desired.
171  
172 Note that 'replace' can also be used to add or append, since there's no requirement that the parameters already exist in the current parameter
173 set.
174  
175 =item strip_like($regex)
176  
177 Remove all parameters whose names match the given (qr-quoted) regex e.g.
178  
179 $qq->strip_like(qr/^utm/)
180  
181 Does NOT match against parameter values.
182  
183 =item separator($separator)
184  
185 Set the argument separator to use for output. Default: '&'.
186  
187 =back
188  
189 =head2 ACCESSOR METHODS
190  
191 =over 4
192  
193 =item has_changed()
194  
195 If the query is actually changed by any of the modifier methods (strip, strip_except, strip_null, strip_like, or replace) it sets an internal changed flag which can be access by:
196  
197 $qq->has_changed
198  
199 revert() resets the has_changed flag to false.
200  
201 =back
202  
203 =head2 OUTPUT METHODS
204  
205 =over 4
206  
207 =item "$qq", stringify(), stringify($separator)
208  
209 Return the current parameter set as a conventional param=value query string, using $separator as the separator if given. e.g.
210  
211 foo=1&bar=2&bar=3
212  
213 Note that all parameters and values are URI escaped by stringify(), so that query-string reserved characters do not occur within elements. For instance, a parameter set of:
214  
215 'group' => 'prod,infra,test'
216 'op:set' => 'x=y'
217  
218 will be stringified as:
219  
220 group=prod%2Cinfra%2Ctest&op%3Aset=x%3Dy
221  
222 =item qstringify(), qstringify($separator)
223  
224 Convenience method to stringify with a leading '?' e.g.
225  
226 ?foo=1&bar=2&bar=3
227  
228 =item hash()
229  
230 Return a hash (in list context) or hashref (in scalar context) of the current parameter set. Single-item parameters have scalar values, while while multiple-item parameters have arrayref values e.g.
231  
232 {
233 foo => 1,
234 bar => [ 2, 3 ],
235 }
236  
237 =item hash_arrayref()
238  
239 Return a hash (in list context) or hashref (in scalar context) of the current parameter set. All values are returned as arrayrefs, including those with single values e.g.
240  
241 {
242 foo => [ 1 ],
243 bar => [ 2, 3 ],
244 }
245  
246 =item hidden()
247  
248 Returns the current parameter set as a concatenated string of hidden input tags, one per parameter-value e.g.
249  
250 <input type="hidden" name="foo" value="1" />
251 <input type="hidden" name="bar" value="2" />
252 <input type="hidden" name="bar" value="3" />
253  
254 =back
255  
256 =head1 AUTHOR
257  
258 Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
259  
260 =head1 SEE ALSO
261  
262 L<Apache2::API::Query>
263  
264 =head1 COPYRIGHT & LICENSE
265  
266 Copyright (c) 2018-2019 DEGUEST Pte. Ltd.
267  
268 You can use, copy, modify and redistribute this package and associated
269 files under the same terms as Perl itself.
270  
271 =cut
« lib/Net/API/REST/DateTime.pm ↑ Summary lib/Net/API/REST/Request.pm »