blib/lib/Geo/Coder/GeocodeFarm.pm

filter: Statementstmt 96.1% 74 / 77 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.8% 130 / 157 CC 24 SCAR 20.2
CC
24
Cov
86%
_request
15.0
new
5.7
reverse_geocode
5.5
CRAP 7.5
↑ 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 7 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 7 our $VERSION = '0.0401';
38  
39 7 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
40 7 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 7 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 7 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 7 ▶use URI::QueryParam;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 7 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 7 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 7 ▶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 10 ▶ 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 10 ▶ raise_failure => $args{raise_failure} || 1,
Branch: 1 branch
BranchTrueFalseTotal
$args{'key'} ? : 6 4 10
Condition50%4/8
$args{'ua'} || 'HTTP::Tiny'->new('agent', 'Geo::Coder::GeocodeFarm' . "/$Geo::Coder::GeocodeFarm::VERSION")
l!l&&r!l&&!r
910
$args{'parser'} || 'JSON'->new->utf8
l!l&&r!l&&!r
0100
$args{'raise_failure'} || 1
l!l
010
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 10 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 4 ▶ my ($self, %args) = @_;
Subroutine reverse_geocode: called CC=5 SCAR=17.1
Pod documented
223  
224 4 my ($lat, $lon) = do {
225 4 ▶ if (defined $args{latlng}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $args{'latlng'}) { } 3 1 4
elsif (defined $args{'lat'} and defined $args{'lon'}) { } 1 0 1
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
001
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 1 @args{qw(lat lon)};
232 }
233 else {
234 0 croak "Attribute (latlng) or attributes (lat) and (lon) are required";
235 }
236 };
237  
238 4 return $self->_request('reverse', %args, lat => $lat, lon => $lon);
239 };
240  
241  
242 sub _request {
243 10 ▶ my ($self, $type, %args) = @_;
Subroutine _request: called CC=14 SCAR=27.1
244  
245 10 my $url = URI->new_abs(sprintf('json/%s/', $type), $self->{url});
246  
247 10 ▶ if ($type eq 'forward') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'forward') { } 6 4 10
elsif ($type eq 'reverse') { } 4 0 4
248 6 $url->query_param_append(addr => $args{addr});
249 } elsif ($type eq 'reverse') {
250 4 $url->query_param_append(lat => $args{lat}, lon => $args{lon});
251 } else {
252 0 croak "Unknown type for request";
253 }
254  
255 10 ▶ $url->query_param_append(key => $self->{key}) if $self->{key};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"key"} 4 6 10
256 10 warn $url if DEBUG;
257  
258 10 my $res = $self->{ua}->get($url);
259  
260 10 my $content = do {
261 10 ▶ if (blessed $res and $res->isa('HTTP::Response')) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($res) and $res->isa('HTTP::Response')) { } 4 6 10
elsif (ref $res eq 'HASH') { } 6 0 6
Condition66%2/3
builtin::blessed($res) and $res->isa("HTTP::Response")
!ll&&!rl&&r
604
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
262 4 ▶ croak $res->status_line unless $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 0 4 4
263 4 $res->decoded_content;
264 } elsif (ref $res eq 'HASH') {
265 6 ▶ croak "@{[$res->{status}, $res->{reason}]}" unless $res->{success};
Branch: 1 branch
BranchTrueFalseTotal
unless $res->{"success"} 1 5 6
266 5 $res->{content};
267 } else {
268 0 croak "Wrong response $res ";
269 }
270 };
271  
272 9 warn $content if DEBUG;
273 9 ▶ return unless $content;
Branch: 1 branch
BranchTrueFalseTotal
unless $content 0 9 9
274  
275 9 my $data = eval { $self->{parser}->decode($content) };
276 9 ▶ croak $content if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 9 9
277  
278 croak "GeocodeFarm API returned status: ", $data->{geocoding_results}{STATUS}{status}
279 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
280  
281 7 return $data->{geocoding_results};
282 };
283  
284  
285 7 1;
286  
287  
288 =for readme continue
289  
290 =head1 SEE ALSO
291  
292 L<https://www.geocode.farm/>
293  
294 =head1 BUGS
295  
296 If you find the bug or want to implement new features, please report it at
297 L<https://github.com/dex4er/perl-Geo-Coder-GeocodeFarm/issues>
298  
299 The code repository is available at
300 L<http://github.com/dex4er/perl-Geo-Coder-GeocodeFarm>
301  
302 =head1 AUTHOR
303  
304 Piotr Roszatycki <dexter@cpan.org>
305  
306 =head1 LICENSE
307  
308 Copyright (c) 2013, 2015 Piotr Roszatycki <dexter@cpan.org>.
309  
310 This is free software; you can redistribute it and/or modify it under
311 the same terms as perl itself.
312  
313 See L<http://dev.perl.org/licenses/artistic.html>
↑ Summary Build »