blib/lib/WWW/GoDaddy/REST/Util.pm

filter: Statementstmt 95.8% 70 / 73 Branchbran 83.3% 15 / 18 Conditioncond 90.9% 10 / 11 MC/DCmcdc 83.3% 5 / 6 Subroutinesub 100% 13 / 13 Podpod 100% 7 / 7 totaltotal 93.7% 120 / 128 CC 17 SCAR 15.0
CC
17
Cov
93%
add_filters_to_url
7.0
json_instance
6.5
build_complex_query_url
6.0
CRAP 4.5
« blib/lib/WWW/GoDaddy/REST/Resource.pm ↑ Summary Build »
1 package WWW::GoDaddy::REST::Util;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 ▶use JSON qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 use Sub::Exporter -setup => {
8 exports => [
9 qw( abs_url
10 add_filters_to_url
11 build_complex_query_url
12 is_json
13 json_decode
14 json_encode
15 json_instance
16 )
17 ]
18 5 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
19 5 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 5 ▶use URI::QueryParam;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 sub is_json {
23 23 ▶ my $json = shift;
Subroutine is_json: called CC=2 SCAR=6.9
Pod documented
24 23 my $handler = json_instance(@_);
25  
26 23 eval { my $perl = json_decode($json); };
27 23 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 10 13 23
28 10 return 0;
29 }
30 else {
31 13 return 1;
32 }
33 }
34  
35 sub json_encode {
36 40 ▶ my $perl = shift;
Subroutine json_encode: called CC=1 SCAR=0.0
Pod documented
37 40 my $handler = json_instance(@_);
38 40 return $handler->encode($perl);
39 }
40  
41 sub json_decode {
42 56 ▶ my $json = shift;
Subroutine json_decode: called CC=1 SCAR=0.0
Pod documented
43 56 my $handler = json_instance(@_);
44 56 return $handler->decode($json);
45 }
46  
47 sub json_instance {
48  
49 137 ▶ my $inst = JSON->new;
Subroutine json_instance: called CC=5 SCAR=18.7
Pod documented
50  
51 137 ▶ if ( @_ == 1 && UNIVERSAL::isa( $_[0], "JSON" ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1 and UNIVERSAL::isa($_[0], 'JSON')) { } 18 119 137
elsif (@_) { } 0 119 119
Condition66%2/3
@_ == 1 and UNIVERSAL::isa($_[0], "JSON")
!ll&&!rl&&r
119018
MC/DC50%1/2
@_ == 1 and UNIVERSAL::isa($_[0], "JSON")
@_ == 1 UNIVERSAL::isa($_[0], "JSON")
Truth table66%2/3
A and B
A= @_ == 1B= UNIVERSAL::isa($_[0], "JSON")
ABresult
0X0
100
111
52 18 return $_[0];
53 }
54 elsif (@_) {
55 0 ▶ while ( my ( $key, $value ) = each %{@_} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{@_;}) 0 0 0
56 0 $inst->property( $key => $value );
57 }
58 }
59 else {
60 119 $inst->convert_blessed(1);
61 119 $inst->allow_nonref(1);
62 }
63 119 return $inst;
64 }
65  
66 sub abs_url {
67 106 ▶ my $api_base = shift;
Subroutine abs_url: called CC=1 SCAR=0.0
Pod documented
68 106 my $url = shift;
69  
70 106 $url =~ s|^/||;
71 106 $api_base =~ s|/*$|/|;
72  
73 106 return URI->new_abs( $url, $api_base );
74 }
75  
76 sub add_filters_to_url {
77 17 ▶ my ( $url, $filters ) = @_;
Subroutine add_filters_to_url: called CC=7 SCAR=19.5
Pod documented
78  
79 17 my $uri = URI->new($url);
80 17 foreach my $field ( sort keys %{$filters} ) {
81 11 my $field_filters = $filters->{$field};
82  
83 11 ▶ next unless $field_filters;
Branch: 1 branch
BranchTrueFalseTotal
unless $field_filters 1 10 11
84  
85 10 ▶ if ( ref($field_filters) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $field_filters eq 'ARRAY') { } 4 6 10
86  
87 # a query could look like so:
88 # {
89 # 'myField' => [
90 # { modifier => 'ne', value => 'apple' },
91 # { value => 'orange' } # implicit 'eq'
92 # ]
93 # }
94 4 foreach my $filter ( @{$field_filters} ) {
95 5 ▶ my $modifier = $filter->{modifier} || 'eq';
Condition100%2/2
$filter->{'modifier'} || 'eq'
l!l
41
MC/DC100%1/1
$filter->{'modifier'} || 'eq'
$filter->{'modifier'}
Truth table100%2/2
A
A= $filter->{'modifier'}
Aresult
11
00
96 5 my $value = $filter->{value};
97 5 ▶ if ( $modifier eq 'eq' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($modifier eq 'eq') { } 2 3 5
98 2 $uri->query_param_append( $field => $value );
99 }
100 else {
101 3 $uri->query_param_append( sprintf( '%s_%s', $field, $modifier ) => $value );
102 }
103 }
104 }
105 else {
106  
107 # a query could look like so:
108 # {
109 # 'myField' => 'apple'
110 # }
111 6 $uri->query_param_append( $field => $field_filters );
112 }
113 }
114 17 return $uri->as_string;
115 }
116  
117 sub build_complex_query_url {
118 9 ▶ my ( $url, $filter, $params ) = @_;
Subroutine build_complex_query_url: called CC=6 SCAR=17.9
Pod documented
119  
120 9 ▶ $filter ||= {};
Condition100%2/2
$filter ||= {}
l!l
81
MC/DC100%1/1
$filter ||= {}
$filter
Truth table100%2/2
A
A= $filter
Aresult
11
00
121 9 ▶ $params ||= {};
Condition100%2/2
$params ||= {}
l!l
54
MC/DC100%1/1
$params ||= {}
$params
Truth table100%2/2
A
A= $params
Aresult
11
00
122  
123 9 $url = add_filters_to_url( $url, $filter );
124  
125 9 ▶ if ( exists $params->{'sort'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params->{"sort"}) 4 5 9
126 4 ▶ $params->{'order'} ||= 'asc';
Condition100%2/2
$params->{"order"} ||= "asc"
l!l
31
MC/DC100%1/1
$params->{"order"} ||= "asc"
$params->{"order"}
Truth table100%2/2
A
A= $params->{"order"}
Aresult
11
00
127 }
128  
129 9 my $uri = URI->new($url);
130 9 ▶ while ( my ( $key, $value ) = each %{$params} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$params;}) 8 9 17
131 8 $uri->query_param( $key => $value );
132 }
133  
134 9 return $uri->as_string;
135  
136 }
137  
138 5 1;
139  
140 =head1 NAME
141  
142 WWW::GoDaddy::REST::Util - Mostly URL tweaking utilities for this package
143  
144 =head1 SYNOPSIS
145  
146 use WWW::GoDaddy::REST::Util qw/ abs_url add_filters_to_url /;
147  
148 # http://example.com/v1/asdf
149 abs_url('http://example.com/v1','/asdf');
150  
151 # http://example.com?sort=asc&fname=Fred
152 add_filters_to_url('http://example.com?sort=asc',{ 'fname' => [ { 'value': 'Fred' } ] });
153  
154 =head1 DESCRIPTION
155  
156 Utilities used commonly in this package. Most have to do with URL manipulation.
157  
158 =head1 FUNCTIONS
159  
160 =over 4
161  
162 =item is_json
163  
164 Given a json string, return true if it is parsable, false otherwise.
165  
166 If you need to control the parameters to the L<JSON> module, simply
167 pass additional parameters. These will be passed unchanged to C<json_instance>.
168  
169 Example:
170  
171 my $yes = is_json('"asdf"');
172 my $yes = is_json('{"key":"value"}');
173 my $no = is_json('dafsafsadfsdaf');
174  
175 =item json_decode
176  
177 Given a json string, return the perl data structure. This will C<die()> if it
178 can not be parsed.
179  
180 If you need to control the parameters to the L<JSON> module, simply
181 pass additional parameters. These will be passed unchanged to C<json_instance>.
182  
183 Example:
184  
185 my $hashref = json_decode('{"key":"value"}');
186  
187 =item json_encode
188  
189 Given a perl data structure, return the json string. This will C<die()> if it
190 can not be serialized.
191  
192 If you need to control the parameters to the L<JSON> module, simply
193 pass additional parameters. These will be passed unchanged to C<json_instance>.
194  
195 Example:
196  
197 my $json = json_encode({ 'key' => 'value' });
198  
199 =item json_instance
200  
201 Returns C<JSON> instance. If no parameters are given the following
202 defaults are set: C<convert_blessed>, C<allow_nonref>.
203  
204 If called with one parameter, it is assumed to be a C<JSON> instance
205 and this is returned instead of building a new one.
206  
207 If called with more than one parameter, it is assumed to be key/value
208 pairs and will be passed to the JSON C<property> method two by two.
209  
210 Example:
211  
212 $j = json_instance(); #defaults
213 $j = json_instance( JSON->new ); #pass through
214 $j = json_instance( 'convert_blessed' => 1, 'allow_nonref' => 1 ); # set properies
215  
216 =item abs_url
217  
218 Given a base and path fragment, generate an absolute url with the two
219 joined.
220  
221 Example:
222  
223 # http://example.com/v1/asdf
224 abs_url('http://example.com/v1','/asdf');
225  
226 =item add_filters_to_url
227  
228 Given a url and a query filter, generate a url with the filter
229 query parameters added.
230  
231 Filter syntax can be seen in the docs for L<WWW::GoDaddy::REST>.
232  
233 Example:
234  
235 add_filters_to_url('http://example.com?sort=asc',{ 'fname' => [ { 'value': 'Fred' } ] });
236 # http://example.com?sort=asc&fname=Fred
237  
238 =item build_complex_query_url
239  
240 Return a modified URL string given a URL, an optional filter spec, and optional
241 query parameter hash.
242  
243 If you specify a sort, then an order parameter will be filled in if not present, and
244 and sort or order query parameters in the input string will be replaced.
245  
246 All other query parameters (filters etc) will be appended to the query parameters
247 of the input URL instead of replacing.
248  
249 Example:
250  
251 build_complex_query_url(
252 'http://example.com',
253 {
254 'foo' => 'bar'
255 },
256 {
257 'sort' => 'surname'
258 }
259 );
260 # http://example.com?foo=bar&sort=surname&order=asc
261  
262 =back
263  
264 =head1 EXPORTS
265  
266 None by default.
267  
268 =head1 AUTHOR
269  
270 David Bartle, C<< <davidb@mediatemple.net> >>
271  
272 =head1 COPYRIGHT & LICENSE
273  
274 Copyright (c) 2014 Go Daddy Operating Company, LLC
275  
276 Permission is hereby granted, free of charge, to any person obtaining a
277 copy of this software and associated documentation files (the "Software"),
278 to deal in the Software without restriction, including without limitation
279 the rights to use, copy, modify, merge, publish, distribute, sublicense,
280 and/or sell copies of the Software, and to permit persons to whom the
281 Software is furnished to do so, subject to the following conditions:
282  
283 The above copyright notice and this permission notice shall be included in
284 all copies or substantial portions of the Software.
285  
286 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
287 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
288 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
289 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
290 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
291 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
292 DEALINGS IN THE SOFTWARE.
293  
294 =cut
295  
« blib/lib/WWW/GoDaddy/REST/Resource.pm ↑ Summary Build »