blib/lib/Geo/GeoNames.pm

filter: Statementstmt 84.9% 124 / 146 Branchbran 66.0% 37 / 56 Conditioncond 73.6% 28 / 38 MC/DCmcdc 23.8% 5 / 21 Subroutinesub 88.0% 22 / 25 Podpod 100% 7 / 7 totaltotal 76.1% 223 / 293 CC 47 SCAR 23.1
CC
47
Cov
79%
_build_request_url
20.2
_do_search
17.6
new
11.2
CRAP 10.0
↑ Summary
1 package Geo::GeoNames;
2 6 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Mojo::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 6 ▶use vars qw($DEBUG $CACHE);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 6 our $VERSION = '1.09';
14  
15 6 our %searches = (
16 cities => 'cities?',
17 country_code => 'countrycode?type=xml&',
18 country_info => 'countryInfo?',
19 earthquakes => 'earthquakesJSON?',
20 find_nearby_placename => 'findNearbyPlaceName?',
21 find_nearby_postalcodes => 'findNearbyPostalCodes?',
22 find_nearby_streets => 'findNearbyStreets?',
23 find_nearby_weather => 'findNearByWeatherXML?',
24 find_nearby_wikipedia => 'findNearbyWikipedia?',
25 find_nearby_wikipedia_by_postalcode => 'findNearbyWikipedia?',
26 find_nearest_address => 'findNearestAddress?',
27 find_nearest_intersection => 'findNearestIntersection?',
28 postalcode_country_info => 'postalCodeCountryInfo?',
29 postalcode_search => 'postalCodeSearch?',
30 search => 'search?',
31 wikipedia_bounding_box => 'wikipediaBoundingBox?',
32 wikipedia_search => 'wikipediaSearch?',
33 get => 'get?',
34 );
35  
36 # r = required
37 # o = optional
38 # rc = required - only one of the fields marked with rc is allowed. At least one must be present
39 # om = optional, multiple entries allowed
40 # d = deprecated - will be removed in later versions
41 6 our %valid_parameters = (
42 search => {
43 'q' => 'rc',
44 name => 'rc',
45 name_equals => 'rc',
46 maxRows => 'o',
47 startRow => 'o',
48 country => 'om',
49 continentCode => 'o',
50 adminCode1 => 'o',
51 adminCode2 => 'o',
52 adminCode3 => 'o',
53 fclass => 'omd',
54 featureClass => 'om',
55 featureCode => 'om',
56 lang => 'o',
57 type => 'o',
58 style => 'o',
59 isNameRequired => 'o',
60 tag => 'o',
61 username => 'r',
62 name_startsWith => 'o',
63 countryBias => 'o',
64 cities => 'om',
65 operator => 'o',
66 searchlang => 'o',
67 charset => 'o',
68 fuzzy => 'o',
69 north => 'o',
70 west => 'o',
71 east => 'o',
72 south => 'o',
73 orderby => 'o',
74 },
75 postalcode_search => {
76 postalcode => 'rc',
77 placename => 'rc',
78 country => 'o',
79 maxRows => 'o',
80 style => 'o',
81 username => 'r',
82 },
83 find_nearby_postalcodes => {
84 lat => 'r',
85 lng => 'r',
86 radius => 'o',
87 maxRows => 'o',
88 style => 'o',
89 country => 'o',
90 username => 'r',
91 },
92 postalcode_country_info => {
93 username => 'r',
94 },
95 find_nearby_placename => {
96 lat => 'r',
97 lng => 'r',
98 radius => 'o',
99 style => 'o',
100 maxRows => 'o',
101 username => 'r',
102 },
103 find_nearest_address => {
104 lat => 'r',
105 lng => 'r',
106 username => 'r',
107 },
108 find_nearest_intersection => {
109 lat => 'r',
110 lng => 'r',
111 username => 'r',
112 },
113 find_nearby_streets => {
114 lat => 'r',
115 lng => 'r',
116 username => 'r',
117 },
118 find_nearby_wikipedia => {
119 lang => 'o',
120 lat => 'r',
121 lng => 'r',
122 radius => 'o',
123 maxRows => 'o',
124 country => 'o',
125 username => 'r',
126 },
127 find_nearby_wikipedia_by_postalcode => {
128 postalcode => 'r',
129 country => 'r',
130 radius => 'o',
131 maxRows => 'o',
132 username => 'r',
133 },
134 wikipedia_search => {
135 'q' => 'r',
136 lang => 'o',
137 title => 'o',
138 maxRows => 'o',
139 username => 'r',
140 },
141 wikipedia_bounding_box => {
142 south => 'r',
143 north => 'r',
144 east => 'r',
145 west => 'r',
146 lang => 'o',
147 maxRows => 'o',
148 username => 'r',
149 },
150 country_info => {
151 country => 'o',
152 lang => 'o',
153 username => 'r',
154 },
155 country_code => {
156 lat => 'r',
157 lng => 'r',
158 lang => 'o',
159 radius => 'o',
160 username => 'r',
161 },
162 find_nearby_weather => {
163 lat => 'r',
164 lng => 'r',
165 username => 'r',
166 },
167 cities => {
168 north => 'r',
169 south => 'r',
170 east => 'r',
171 west => 'r',
172 lang => 'o',
173 maxRows => 'o',
174 username => 'r',
175 },
176 earthquakes => {
177 north => 'r',
178 south => 'r',
179 east => 'r',
180 west => 'r',
181 date => 'o',
182 minMagnutide => 'o',
183 maxRows => 'o',
184 username => 'r',
185 },
186 get => {
187 geonameId => 'r',
188 lang => 'o',
189 style => 'o',
190 username => 'r',
191 },
192 );
193  
194 sub new {
195 10 ▶ my( $class, %hash ) = @_;
Subroutine new: called CC=11 SCAR=24.1
Pod documented
196  
197 10 my $self = bless { _functions => \%searches }, $class;
198  
199 10 ▶ croak <<"HERE" unless length $hash{username};
Branch: 1 branch
BranchTrueFalseTotal
unless length $hash{"username"} 2 8 10
200 You must specify a GeoNames username to use Geo::GeoNames.
201 See http://www.geonames.org/export/web-services.html
202 HERE
203  
204 8 $self->username( $hash{username} );
205 8 ▶ $self->url( $hash{url} // $self->default_url );
Condition66%2/3
$hash{"url"} // $self->default_url
l!l&&r!l&&!r
170
MC/DC0%0/2
$hash{"url"} // $self->default_url
$hash{"url"} $self->default_url
Truth table66%2/3
A // B
A= $hash{"url"}B= $self->default_url
ABresult
1X1
011
000
206  
207 croak 'Illegal ua object, needs either a Mojo::UserAgent or an LWP::UserAgent derived object'
208 8 ▶ if exists $hash{ua} && !(ref $hash{ua} && blessed($hash{ua}) && ( $hash{ua}->isa('Mojo::UserAgent') || $hash{ua}->isa('LWP::UserAgent') ) );
Branch: 1 branch
BranchTrueFalseTotal
if exists $hash{"ua"} and not ref $hash{"ua"} && builtin::blessed($hash{"ua"}) && ($hash{"ua"}->isa("Mojo::UserAgent") || $hash{"ua"}->isa("LWP::UserAgent")) 3 5 8
Condition100%12/12
exists $hash{"ua"} and not ref $hash{"ua"} && builtin::blessed($hash{"ua"}) && ($hash{"ua"}->isa("Mojo::UserAgent") || $hash{"ua"}->isa("LWP::UserAgent"))
!ll&&!rl&&r
323
ref $hash{'ua'} && builtin::blessed($hash{'ua'}) && ($hash{'ua'}->isa('Mojo::UserAgent') || $hash{'ua'}->isa('LWP::UserAgent'))
!ll&&!rl&&r
212
ref $hash{"ua"} and builtin::blessed($hash{"ua"})
!ll&&!rl&&r
113
$hash{"ua"}->isa("Mojo::UserAgent") or $hash{"ua"}->isa("LWP::UserAgent")
l!l&&r!l&&!r
111
MC/DC100%5/5
exists $hash{"ua"} and not ref $hash{"ua"} && builtin::blessed($hash{"ua"}) && ($hash{"ua"}->isa("Mojo::UserAgent") || $hash{"ua"}->isa("LWP::UserAgent"))
exists $hash{"ua"} ref $hash{"ua"} builtin::blessed($hash{"ua"}) $hash{"ua"}->isa("Mojo::UserAgent") $hash{"ua"}->isa("LWP::UserAgent")
Truth table100%6/6
A and not(B and C && D or E)
A= exists $hash{"ua"}B= ref $hash{"ua"}C= builtin::blessed($hash{"ua"})D= $hash{"ua"}->isa("Mojo::UserAgent")E= $hash{"ua"}->isa("LWP::UserAgent")
ABCDEresult
0XXXX0
1111X0
111010
10XXX1
110XX1
111001
209 5 ▶ $self->ua($hash{ua} || $self->default_ua );
Condition66%2/3
$hash{'ua'} || $self->default_ua
l!l&&r!l&&!r
230
MC/DC0%0/2
$hash{'ua'} || $self->default_ua
$hash{'ua'} $self->default_ua
Truth table66%2/3
A || B
A= $hash{'ua'}B= $self->default_ua
ABresult
1X1
011
000
210  
211 5 ▶ (exists($hash{debug})) ? $DEBUG = $hash{debug} : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $hash{'debug'} ? : 0 5 5
212 5 ▶ (exists($hash{cache})) ? $CACHE = $hash{cache} : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $hash{'cache'} ? : 0 5 5
213 5 $self->{_functions} = \%searches;
214  
215 5 return $self;
216 }
217  
218 sub username {
219 9 ▶ my( $self, $username ) = @_;
Subroutine username: called CC=2 SCAR=6.9
Pod documented
220  
221 9 ▶ $self->{username} = $username if @_ == 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 2 8 1 9
222  
223 9 $self->{username};
224 }
225  
226 sub ua {
227 5 ▶ my( $self, $ua ) = @_;
Subroutine ua: called CC=2 SCAR=7.1
Pod documented
228  
229 5 ▶ $self->{ua} = $ua if @_ == 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 2 5 0 5
230  
231 5 $self->{ua};
232 }
233  
234 sub default_ua {
235 3 ▶ my $ua = Mojo::UserAgent->new;
Subroutine default_ua: called CC=1 SCAR=0.2
Pod documented
236 3 ▶ $ua->on( error => sub { carp "Can't get request" } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
237 3 $ua;
238 }
239 7 ▶sub default_url { 'http://api.geonames.org' }
Subroutine default_url: called CC=1 SCAR=0.0
Pod documented
240  
241 sub url {
242 9 ▶ my( $self, $url ) = @_;
Subroutine url: called CC=2 SCAR=6.9
Pod documented
243  
244 9 ▶ $self->{url} = $url if @_ == 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 2 8 1 9
245  
246 9 $self->{url};
247 }
248  
249 sub _build_request_url {
250 1 ▶ my( $self, $request, @args ) = @_;
Subroutine _build_request_url: called CC=16 SCAR=30.1
251 1 my $hash = { @args, username => $self->username };
252 1 my $request_url = $self->url . '/' . $searches{$request};
253  
254 # check to see that mandatory arguments are present
255 1 my $conditional_mandatory_flag = 0;
256 1 my $conditional_mandatory_required = 0;
257 1 foreach my $arg (keys %{$valid_parameters{$request}}) {
258 31 my $flags = $valid_parameters{$request}->{$arg};
259 31 ▶ if($flags =~ /d/ && exists($hash->{$arg})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags =~ /d/ and exists $hash->{$arg}) 0 31 31
Condition66%2/3
$flags =~ /d/ and exists $hash->{$arg}
!ll&&!rl&&r
3010
MC/DC0%0/2
$flags =~ /d/ and exists $hash->{$arg}
$flags =~ /d/ exists $hash->{$arg}
Truth table66%2/3
A and B
A= $flags =~ /d/B= exists $hash->{$arg}
ABresult
0X0
100
111
260 0 carp("Argument $arg is deprecated.");
261 }
262 31 $flags =~ s/d//g;
263 31 ▶ if($flags eq 'r' && !exists($hash->{$arg})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags eq "r" and not exists $hash->{$arg}) 0 31 31
Condition66%2/3
$flags eq "r" and not exists $hash->{$arg}
!ll&&!rl&&r
3010
MC/DC0%0/2
$flags eq "r" and not exists $hash->{$arg}
$flags eq "r" not exists $hash->{$arg}
Truth table66%2/3
A and not(B)
A= $flags eq "r"B= not exists $hash->{$arg}
ABresult
0X0
100
111
264 0 carp("Mandatory argument $arg is missing!");
265 }
266 31 ▶ if($flags !~ /m/ && exists($hash->{$arg}) && ref($hash->{$arg})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $flags =~ /m/ and exists $hash->{$arg} and ref $hash->{$arg}) 0 31 31
Condition83%5/6
not $flags =~ /m/ and exists $hash->{$arg} and ref $hash->{$arg}
!ll&&!rl&&r
2920
not $flags =~ /m/ and exists $hash->{$arg}
!ll&&!rl&&r
5242
MC/DC0%0/3
not $flags =~ /m/ and exists $hash->{$arg} and ref $hash->{$arg}
not $flags =~ /m/ exists $hash->{$arg} ref $hash->{$arg}
Truth table75%3/4
not(A) and B and C
A= not $flags =~ /m/B= exists $hash->{$arg}C= ref $hash->{$arg}
ABCresult
0XX0
10X0
1100
1111
267 0 carp("Argument $arg cannot have multiple values.");
268 }
269 31 ▶ if($flags eq 'rc') {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags eq "rc") 3 28 31
270 3 $conditional_mandatory_required = 1;
271 3 ▶ if(exists($hash->{$arg})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $hash->{$arg}) 1 2 3
272 1 $conditional_mandatory_flag++;
273 }
274 }
275 }
276  
277 1 ▶ if($conditional_mandatory_required == 1 && $conditional_mandatory_flag != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conditional_mandatory_required == 1 and $conditional_mandatory_flag != 1) 0 1 1
Condition33%1/3
$conditional_mandatory_required == 1 and $conditional_mandatory_flag != 1
!ll&&!rl&&r
010
MC/DC0%0/2
$conditional_mandatory_required == 1 and $conditional_mandatory_flag != 1
$conditional_mandatory_required == 1 $conditional_mandatory_flag != 1
Truth table33%1/3
A and B
A= $conditional_mandatory_required == 1B= $conditional_mandatory_flag != 1
ABresult
0X0
100
111
278 0 carp("Invalid number of mandatory arguments (there can be only one)");
279 }
280 1 foreach my $key (sort keys(%$hash)) {
281 2 ▶ carp("Invalid argument $key") if(!defined($valid_parameters{$request}->{$key}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Geo::GeoNames::valid_parameters{$request}{$key} 0 2 2
282 2 ▶ my @vals = ref($hash->{$key}) ? @{$hash->{$key}} : $hash->{$key};
Branch: 1 branch
BranchTrueFalseTotal
ref $hash->{$key} ? : 0 2 2
283 6 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
284 2 $request_url .= join('', map { "$key=$_&" } sort @vals );
285 }
286  
287 1 chop($request_url); # loose the trailing &
288 1 return $request_url;
289 }
290  
291 sub _parse_xml_result {
292 3 ▶ require XML::Simple;
Subroutine _parse_xml_result: called CC=10 SCAR=23.0
293 3 my( $self, $geonamesresponse, $single_result ) = @_;
294 3 my @result;
295 3 my $xmlsimple = XML::Simple->new;
296 3 my $xml = $xmlsimple->XMLin( $geonamesresponse, KeyAttr => [], ForceArray => 1 );
297  
298 3 ▶ if ($xml->{'status'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xml->{"status"}) 1 2 3
299 1 carp "GeoNames error: " . $xml->{'status'}->[0]->{message};
300 1 return [];
301 }
302  
303 2 ▶ $xml = { geoname => [ $xml ], totalResultsCount => '1' } if $single_result;
Branch: 1 branch
BranchTrueFalseTotal
if $single_result 1 1 2
304  
305 2 my $i = 0;
306 2 foreach my $element (keys %{$xml}) {
307 5 ▶ next if (ref($xml->{$element}) ne "ARRAY");
Branch: 1 branch
BranchTrueFalseTotal
if ref $xml->{$element} ne "ARRAY" 2 3 5
308 3 foreach my $list (@{$xml->{$element}}) {
309 3 ▶ next if (ref($list) ne "HASH");
Branch: 1 branch
BranchTrueFalseTotal
if ref $list ne "HASH" 1 2 3
310 2 foreach my $attribute (%{$list}) {
311 96 ▶ next if !defined($list->{$attribute}->[0]);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $list->{$attribute}[0] 48 48 96
312 48 ▶ $result[$i]->{$attribute} = (scalar @{$list->{$attribute}} == 1 ? $list->{$attribute}->[0] : $list->{$attribute});
Branch: 1 branch
BranchTrueFalseTotal
scalar @{$list->{$attribute};} == 1 ? : 47 1 48
313 }
314 2 $i++;
315 }
316 }
317 2 return \@result;
318 }
319  
320 sub _parse_json_result {
321 1 ▶ require JSON;
Subroutine _parse_json_result: called CC=1 SCAR=0.0
322 1 my( $self, $geonamesresponse ) = @_;
323 1 my @result;
324 1 return JSON->new->utf8->decode($geonamesresponse);
325 }
326  
327 sub _parse_text_result {
328 0 ▶ my( $self, $geonamesresponse ) = @_;
Subroutine _parse_text_result: not called CC=1 SCAR=6.9
329 0 my @result;
330 0 $result[0]->{Result} = $geonamesresponse;
331 0 return \@result;
332 }
333  
334 sub _request {
335 1 ▶ my( $self, $request_url ) = @_;
Subroutine _request: called CC=2 SCAR=7.1
336
337 1 my $res = $self->{ua}->get( $request_url );
338 1 ▶ return $res->can('res') ? $res->res : $res;
Branch: 1 branch
BranchTrueFalseTotal
$res->can('res') ? : 1 0 1
339 }
340  
341 sub _do_search {
342 1 ▶ my( $self, $searchtype, @args ) = @_;
Subroutine _do_search: called CC=7 SCAR=28.7
343  
344 1 my $request_url = $self->_build_request_url( $searchtype, @args );
345 1 my $response = $self->_request( $request_url );
346  
347 # check mime-type to determine which parse method to use.
348 # we accept text/xml, text/plain (how do see if it is JSON or not?)
349 1 ▶ my $mime_type = $response->headers->content_type || '';
Condition50%1/2
$response->headers->content_type || ''
l!l
10
MC/DC0%0/1
$response->headers->content_type || ''
$response->headers->content_type
Truth table50%1/2
A
A= $response->headers->content_type
Aresult
11
00
350  
351 1 my $body = '';
352 1 ▶ if ($response->can('body')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->can('body')) { } 1 0 1
353 1 $body = $response->body;
354 }
355 else {
356 0 $body = $response->content;
357 }
358  
359 1 ▶ if($mime_type =~ m(\Atext/xml;?) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mime_type =~ m[\Atext/xml;?]) 1 0 1
360 1 return $self->_parse_xml_result( $body, $searchtype eq 'get' );
361 }
362 0 ▶ if($mime_type =~ m(\Aapplication/json;?) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mime_type =~ m[\Aapplication/json;?]) 0 0 0
363 # a JSON object always start with a left-brace {
364 # according to http://json.org/
365 0 ▶ if( $body =~ m/\A\{/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body =~ /\A\{/) { } 0 0 0
366 0 ▶ if ($response->can('json')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->can('json')) { } 0 0 0
367 0 return $response->json;
368 }
369 else {
370 0 return $self->_parse_json_result( $body );
371 }
372 }
373 else {
374 0 return $self->_parse_text_result( $body );
375 }
376 }
377  
378 0 carp "Invalid mime type [$mime_type]. Maybe you aren't connected.";
379  
380 0 return [];
381 }
382  
383 sub geocode {
384 0 ▶ my( $self, $q ) = @_;
Subroutine geocode: not called CC=1 SCAR=6.9
Pod documented
385 0 $self->search( 'q' => $q );
386 }
387  
388 sub AUTOLOAD {
389 1 ▶ my $self = shift;
Subroutine AUTOLOAD: called CC=3 SCAR=12.0
390 1 ▶ my $type = ref($self) || croak "$self is not an object";
Condition33%1/3
ref $self || croak("$self is not an object")
l!l&&r!l&&!r
100
MC/DC0%0/2
ref $self || croak("$self is not an object")
ref $self croak("$self is not an object")
Truth table33%1/3
A || B
A= ref $selfB= croak("$self is not an object")
ABresult
1X1
011
000
391 1 my $name = our $AUTOLOAD;
392 1 $name =~ s/.*://;
393  
394 1 ▶ unless (exists $self->{_functions}->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"_functions"}{$name}) 0 1 1
395 0 croak "No such method '$AUTOLOAD'";
396 }
397  
398 1 return($self->_do_search($name, @_));
399 }
400  
401 10 ▶sub DESTROY { 1 }
Subroutine DESTROY: called CC=1 SCAR=0.0
402  
403 6 1;
404  
405 __END__
↑ Summary