blib/lib/Geo/Coder/GeocodeFarm.pm

filter: Statementstmt 96.1% 75 / 78 Branchbran 78.1% 25 / 32 Conditioncond 57.8% 11 / 19 MC/DCmcdc 18.1% 2 / 11 Subroutinesub 100% 15 / 15 Podpod 100% 3 / 3 totaltotal 82.9% 131 / 158 SCAR 32.4
CC
24
Cov
86%
_request
15.0
new
5.7
reverse_geocode
5.5
CRAP 25.6
↑ Summary Build »
1 package Geo::Coder::GeocodeFarm;
2  
3 =head1 NAME
4  
5 Geo::Coder::GeocodeFarm - Geocode addresses with the GeocodeFarm API
6  
7 =head1 SYNOPSIS
8  
9 use Geo::Coder::GeocodeFarm;
10  
11 my $geocoder = Geo::Coder::GeocodeFarm->new(
12 key => '3d517dd448a5ce1c2874637145fed69903bc252a',
13 );
14 my $result = $geocoder->geocode(
15 location => '530 W Main St Anoka MN 55303 US',
16 lang => 'en',
17 count => 1,
18 );
19 printf "%f,%f",
20 $result->{RESULTS}{COORDINATES}{latitude},
21 $result->{RESULTS}{COORDINATES}{longitude};
22  
23 =head1 DESCRIPTION
24  
25 The C<Geo::Coder::GeocodeFarm> module provides an interface to the geocoding
26 functionality of the GeocodeFarm API v3.
27  
28 =for readme stop
29  
30 =cut
31  
32  
33 8 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 8 our $VERSION = '0.0402';
38  
39 8 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
40 8 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 8 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 8 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 8 ▶use URI::QueryParam;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 8 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 8 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 8 ▶use constant DEBUG => !! $ENV{PERL_GEO_CODER_GEOCODEFARM_DEBUG};
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49  
50 =head1 METHODS
51  
52 =head2 new
53  
54 $geocoder = Geo::Coder::GeocodeFarm->new(
55 key => '3d517dd448a5ce1c2874637145fed69903bc252a',
56 url => 'https://www.geocode.farm/v3/',
57 ua => HTTP::Tiny->new,
58 parser => JSON->new->utf8,
59 raise_failure => 1,
60 );
61  
62 Creates a new geocoding object with optional arguments.
63  
64 An API key is optional and can be obtained at
65 L<https://www.geocode.farm/dashboard/login/>
66  
67 C<url> argument is optional and then the default address is http-based if
68 C<key> argument is missing and https-based if C<key> is provided.
69  
70 C<ua> argument is a L<HTTP::Tiny> object by default and can be also set to
71 L<LWP::UserAgent> object.
72  
73 New account can be registered at L<https://www.geocode.farm/register/>
74  
75 =cut
76  
77 sub new {
78 11 ▶ my ($class, %args) = @_;
Subroutine new: called CC=5 SCAR=17.5
Pod documented
79  
80 my $self = bless +{
81 ua => $args{ua} || HTTP::Tiny->new(
82 agent => __PACKAGE__ . "/$VERSION",
83 ),
84 url => sprintf('%s://www.geocode.farm/v3/', $args{key} ? 'https' : 'http'),
85 parser => $args{parser} || JSON->new->utf8,
86 11 ▶ raise_failure => $args{raise_failure} || 1,
Branch: 1 branch
BranchTrueFalseTotal
$args{'key'} ? : 6 5 11
Condition50%4/8
$args{'ua'} || 'HTTP::Tiny'->new('agent', 'Geo::Coder::GeocodeFarm' . "/$Geo::Coder::GeocodeFarm::VERSION")
l!l&&r!l&&!r
920
$args{'parser'} || 'JSON'->new->utf8
l!l&&r!l&&!r
0110
$args{'raise_failure'} || 1
l!l
011
MC/DC0%0/2
$args{'ua'} || 'HTTP::Tiny'->new('agent', 'Geo::Coder::GeocodeFarm' . "/$Geo::Coder::GeocodeFarm::VERSION")
$args{'ua'} 'HTTP::Tiny'->new('agent', 'Geo::Coder::GeocodeFarm' . "/$Geo::Coder::GeocodeFarm::VERSION")
MC/DC0%0/2
$args{'parser'} || 'JSON'->new->utf8
$args{'parser'} 'JSON'->new->utf8
MC/DC0%0/1
$args{'raise_failure'} || 1
$args{'raise_failure'}
Truth table66%2/3
A || B
A= $args{'ua'}B= 'HTTP::Tiny'->new('agent', 'Geo::Coder::GeocodeFarm' . "/$Geo::Coder::GeocodeFarm::VERSION")
ABresult
1X1
011
000
Truth table33%1/3
A || B
A= $args{'parser'}B= 'JSON'->new->utf8
ABresult
1X1
011
000
Truth table50%1/2
A
A= $args{'raise_failure'}
Aresult
11
00
87 %args,
88 } => $class;
89  
90 11 return $self;
91 }
92  
93  
94 =head2 geocode
95  
96 $result = $geocoder->geocode(
97 location => $location,
98 lang => 'en', # optional: 'en' or 'de'
99 country => 'US', # optional
100 count => 1, # optional
101 )
102  
103 Forward geocoding takes a provided address or location and returns the
104 coordinate set for the requested location as a nested list:
105  
106 {
107 "geocoding_results": {
108 "LEGAL_COPYRIGHT": {
109 "copyright_notice": "Copyright (c) 2015 Geocode.Farm - All Rights Reserved.",
110 "copyright_logo": "https:\/\/www.geocode.farm\/images\/logo.png",
111 "terms_of_service": "https:\/\/www.geocode.farm\/policies\/terms-of-service\/",
112 "privacy_policy": "https:\/\/www.geocode.farm\/policies\/privacy-policy\/"
113 },
114 "STATUS": {
115 "access": "FREE_USER, ACCESS_GRANTED",
116 "status": "SUCCESS",
117 "address_provided": "530 W Main St Anoka MN 55303 US",
118 "result_count": 1
119 },
120 "ACCOUNT": {
121 "ip_address": "1.2.3.4",
122 "distribution_license": "NONE, UNLICENSED",
123 "usage_limit": "250",
124 "used_today": "26",
125 "used_total": "26",
126 "first_used": "26 Mar 2015"
127 },
128 "RESULTS": [
129 {
130 "result_number": 1,
131 "formatted_address": "530 West Main Street, Anoka, MN 55303, USA",
132 "accuracy": "EXACT_MATCH",
133 "ADDRESS": {
134 "street_number": "530",
135 "street_name": "West Main Street",
136 "locality": "Anoka",
137 "admin_2": "Anoka County",
138 "admin_1": "Minnesota",
139 "postal_code": "55303",
140 "country": "United States"
141 },
142 "LOCATION_DETAILS": {
143 "elevation": "UNAVAILABLE",
144 "timezone_long": "UNAVAILABLE",
145 "timezone_short": "America\/Menominee"
146 },
147 "COORDINATES": {
148 "latitude": "45.2041251174690",
149 "longitude": "-93.4003513528652"
150 },
151 "BOUNDARIES": {
152 "northeast_latitude": "45.2041251778513",
153 "northeast_longitude": "-93.4003513845523",
154 "southwest_latitude": "45.2027761197097",
155 "southwest_longitude": "-93.4017002802923"
156 }
157 }
158 ],
159 "STATISTICS": {
160 "https_ssl": "DISABLED, INSECURE"
161 }
162 }
163 }
164  
165 Method throws an error (or returns failure as nested list if raise_failure
166 argument is false) if the service failed to find coordinates or wrong key was
167 used.
168  
169 Methods throws an error if there was an other problem.
170  
171 =cut
172  
173 sub geocode {
174 8 ▶ my ($self, %args) = @_;
Subroutine geocode: called CC=3 SCAR=11.0
Pod documented
175  
176 8 my ($addr) = do {
177 8 ▶ if (defined $args{location}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $args{'location'}) { } 5 3 8
elsif (defined $args{'addr'}) { } 1 2 3
178 5 $args{location};
179 }
180 elsif (defined $args{addr}) {
181 1 $args{addr};
182 }
183 else {
184 2 croak "Attribute (location) or attribute (addr) is required";
185 }
186 };
187  
188 6 return $self->_request('forward', %args, addr => $addr);
189 };
190  
191  
192 =head2 reverse_geocode
193  
194 $result = $geocoder->reverse_geocode(
195 lat => $latitude,
196 lon => $longtitude,
197 lang => 'en', # optional: 'en' or 'de'
198 country => 'US', # optional
199 count => 1, # optional
200 )
201  
202 or
203  
204 $result = $geocoder->reverse_geocode(
205 latlng => "$latitude,$longtitude",
206 # ... optional args
207 )
208  
209 Reverse geocoding takes a provided coordinate set and returns the address for
210 the requested coordinates as a nested list. Its format is the same as for
211 L</geocode> method.
212  
213 Method throws an error (or returns failure as nested list if raise_failure
214 argument is false) if the service failed to find coordinates or wrong key was
215 used.
216  
217 Method throws an error if there was an other problem.
218  
219 =cut
220  
221 sub reverse_geocode {
222 5 ▶ my ($self, %args) = @_;
Subroutine reverse_geocode: called CC=5 SCAR=17.1
Pod documented
223  
224 5 my ($lat, $lon) = do {
225 5 ▶ if (defined $args{latlng}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $args{'latlng'}) { } 3 2 5
elsif (defined $args{'lat'} and defined $args{'lon'}) { } 2 0 2
226 3 my @latlng = split ',', $args{latlng};
227 3 ▶ croak "Attribute (latlng) is invalid" unless @latlng == 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @latlng == 2 0 3 3
228 3 @latlng;
229 }
230 ▶ elsif (defined $args{lat} and defined $args{lon}) {
Condition33%1/3
defined $args{"lat"} and defined $args{"lon"}
!ll&&!rl&&r
002
MC/DC0%0/2
defined $args{"lat"} and defined $args{"lon"}
defined $args{"lat"} defined $args{"lon"}
Truth table33%1/3
A and B
A= defined $args{"lat"}B= defined $args{"lon"}
ABresult
0X0
100
111
231 2 @args{qw(lat lon)};
232 }
233 else {
234 0 croak "Attribute (latlng) or attributes (lat) and (lon) are required";
235 }
236 };
237  
238 5 return $self->_request('reverse', %args, lat => $lat, lon => $lon);
239 };
240  
241  
242 sub _request {
243 11 ▶ my ($self, $type, %args) = @_;
Subroutine _request: called CC=14 SCAR=27.1
244  
245 11 my $url = URI->new_abs(sprintf('json/%s/', $type), $self->{url});
246  
247 11 ▶ if ($type eq 'forward') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'forward') { } 6 5 11
elsif ($type eq 'reverse') { } 5 0 5
248 6 $url->query_param_append(addr => $args{addr});
249 } elsif ($type eq 'reverse') {
250 5 $url->query_param_append(lat => $args{lat});
251 5 $url->query_param_append(lon => $args{lon});
252 } else {
253 0 croak "Unknown type for request";
254 }
255  
256 11 ▶ $url->query_param_append(key => $self->{key}) if $self->{key};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"key"} 4 7 11
257 11 warn $url if DEBUG;
258  
259 11 my $res = $self->{ua}->get($url);
260  
261 11 my $content = do {
262 11 ▶ if (blessed $res and $res->isa('HTTP::Response')) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($res) and $res->isa('HTTP::Response')) { } 4 7 11
elsif (ref $res eq 'HASH') { } 7 0 7
Condition66%2/3
builtin::blessed($res) and $res->isa("HTTP::Response")
!ll&&!rl&&r
704
MC/DC50%1/2
builtin::blessed($res) and $res->isa("HTTP::Response")
builtin::blessed($res) $res->isa("HTTP::Response")
Truth table66%2/3
A and B
A= builtin::blessed($res)B= $res->isa("HTTP::Response")
ABresult
0X0
100
111
263 4 ▶ croak $res->status_line unless $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 0 4 4
264 4 $res->decoded_content;
265 } elsif (ref $res eq 'HASH') {
266 7 ▶ croak "@{[$res->{status}, $res->{reason}]}" unless $res->{success};
Branch: 1 branch
BranchTrueFalseTotal
unless $res->{"success"} 2 5 7
267 5 $res->{content};
268 } else {
269 0 croak "Wrong response $res ";
270 }
271 };
272  
273 9 warn $content if DEBUG;
274 9 ▶ return unless $content;
Branch: 1 branch
BranchTrueFalseTotal
unless $content 0 9 9
275  
276 9 my $data = eval { $self->{parser}->decode($content) };
277 9 ▶ croak $content if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 9 9
278  
279 croak "GeocodeFarm API returned status: ", $data->{geocoding_results}{STATUS}{status}
280 9 ▶ if ($self->{raise_failure} and ($data->{geocoding_results}{STATUS}{status}||'') ne 'SUCCESS');
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"raise_failure"} and ($data->{"geocoding_results"}{"STATUS"}{"status"} || "") ne "SUCCESS" 2 7 9
Condition80%4/5
$self->{"raise_failure"} and ($data->{"geocoding_results"}{"STATUS"}{"status"} || "") ne "SUCCESS"
!ll&&!rl&&r
252
$data->{'geocoding_results'}{'STATUS'}{'status'} || ''
l!l
70
MC/DC50%1/2
$self->{"raise_failure"} and ($data->{"geocoding_results"}{"STATUS"}{"status"} || "") ne "SUCCESS"
$self->{"raise_failure"} $data->{'geocoding_results'}{'STATUS'}{'status'}
Truth table66%2/3
A and B
A= $self->{"raise_failure"}B= $data->{'geocoding_results'}{'STATUS'}{'status'}
ABresult
0X0
100
111
281  
282 7 return $data->{geocoding_results};
283 };
284  
285  
286 8 1;
287  
288  
289 =for readme continue
290  
291 =head1 SEE ALSO
292  
293 L<https://www.geocode.farm/>
294  
295 =head1 BUGS
296  
297 If you find the bug or want to implement new features, please report it at
298 L<https://github.com/dex4er/perl-Geo-Coder-GeocodeFarm/issues>
299  
300 The code repository is available at
301 L<http://github.com/dex4er/perl-Geo-Coder-GeocodeFarm>
302  
303 =head1 AUTHOR
304  
305 Piotr Roszatycki <dexter@cpan.org>
306  
307 =head1 LICENSE
308  
309 Copyright (c) 2013, 2015 Piotr Roszatycki <dexter@cpan.org>.
310  
311 This is free software; you can redistribute it and/or modify it under
312 the same terms as perl itself.
313  
314 See L<http://dev.perl.org/licenses/artistic.html>
↑ Summary Build »