GooglePlaces/V3.pm

filter: Statementstmt 41.1% 49 / 119 Branchbran 11.3% 5 / 44 Conditioncond 28.5% 8 / 28 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 58.8% 10 / 17 Podpod 100% 5 / 5 totaltotal 33.4% 77 / 230 CC 35 SCAR 49.8
CC
35
Cov
32%
geocode
380.0
new
13.0
reverse_geocode
12.0
CRAP 145.8
↑ Summary GooglePlaces.pm »
1 package Geo::Coder::GooglePlaces::V3;
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 Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 5 my @ALLOWED_FILTERS = qw/route locality administrative_area postal_code country/;
14  
15 =head1 NAME
16  
17 Geo::Coder::GooglePlaces::V3 - Google Places Geocoding API V3
18  
19 =head1 VERSION
20  
21 Version 0.04
22  
23 =cut
24  
25 5 our $VERSION = '0.04';
26  
27 =head1 SYNOPSIS
28  
29 use Geo::Coder::GooglePlaces;
30  
31 my $geocoder = Geo::Coder::GooglePlaces->new();
32 my $location = $geocoder->geocode(location => 'Hollywood and Highland, Los Angeles, CA');
33  
34 =head1 DESCRIPTION
35  
36 Geo::Coder::GooglePlaces::V3 provides a geocoding functionality using Google Places API V3.
37  
38 =head1 SUBROUTINES/METHODS
39  
40 =head2 new
41  
42 $geocoder = Geo::Coder::GooglePlaces->new();
43 $geocoder = Geo::Coder::GooglePlaces->new(language => 'ru');
44 $geocoder = Geo::Coder::GooglePlaces->new(gl => 'ca');
45 $geocoder = Geo::Coder::GooglePlaces->new(oe => 'latin1');
46  
47 To specify the language of Google's response add C<language> parameter
48 with a two-letter value. Note that adding that parameter does not
49 guarantee that every request returns translated data.
50  
51 You can also set C<gl> parameter to set country code (e.g. I<ca> for Canada).
52  
53 You can ask for a character encoding other than utf-8 by setting the I<oe>
54 parameter, but this is not recommended.
55  
56 You can optionally use your Places Premier Client ID, by passing your client
57 code as the C<client> parameter and your private key as the C<key> parameter.
58 The URL signing for Premier Client IDs requires the I<Digest::HMAC_SHA1>
59 and I<MIME::Base64> modules. To test your client, set the environment
60 variables GMAP_CLIENT and GMAP_KEY before running v3_live.t
61  
62 GMAP_CLIENT=your_id GMAP_KEY='your_key' make test
63  
64 You can get a key from https://console.developers.google.com/apis/credentials.
65  
66 =cut
67  
68 sub new {
69 9 ▶ my($class, %param) = @_;
Subroutine new: called CC=9 SCAR=25.6
Pod documented
70  
71 9 ▶ my $ua = delete $param{ua} || LWP::UserAgent->new(agent => __PACKAGE__ . "/$VERSION");
Condition33%1/3
delete $param{'ua'} || 'LWP::UserAgent'->new('agent', 'Geo::Coder::GooglePlaces::V3' . "/$Geo::Coder::GooglePlaces::V3::VERSION")
l!l&&r!l&&!r
090
MC/DC0%0/2
delete $param{'ua'} || 'LWP::UserAgent'->new('agent', 'Geo::Coder::GooglePlaces::V3' . "/$Geo::Coder::GooglePlaces::V3::VERSION")
delete $param{'ua'} 'LWP::UserAgent'->new('agent', 'Geo::Coder::GooglePlaces::V3' . "/$Geo::Coder::GooglePlaces::V3::VERSION")
Truth table33%1/3
A || B
A= delete $param{'ua'}B= 'LWP::UserAgent'->new('agent', 'Geo::Coder::GooglePlaces::V3' . "/$Geo::Coder::GooglePlaces::V3::VERSION")
ABresult
1X1
011
000
72 9 ▶ my $host = delete $param{host} || 'maps.googleapis.com';
Condition50%1/2
delete $param{'host'} || 'maps.googleapis.com'
l!l
09
MC/DC0%0/1
delete $param{'host'} || 'maps.googleapis.com'
delete $param{'host'}
Truth table50%1/2
A
A= delete $param{'host'}
Aresult
11
00
73  
74 9 ▶ my $language = delete $param{language} || delete $param{hl};
Condition33%1/3
delete $param{'language'} || delete $param{'hl'}
l!l&&r!l&&!r
009
MC/DC0%0/2
delete $param{'language'} || delete $param{'hl'}
delete $param{'language'} delete $param{'hl'}
Truth table33%1/3
A || B
A= delete $param{'language'}B= delete $param{'hl'}
ABresult
1X1
011
000
75 9 ▶ my $region = delete $param{region} || delete $param{gl};
Condition33%1/3
delete $param{'region'} || delete $param{'gl'}
l!l&&r!l&&!r
009
MC/DC0%0/2
delete $param{'region'} || delete $param{'gl'}
delete $param{'region'} delete $param{'gl'}
Truth table33%1/3
A || B
A= delete $param{'region'}B= delete $param{'gl'}
ABresult
1X1
011
000
76 9 ▶ my $oe = delete $param{oe} || 'utf8';
Condition50%1/2
delete $param{'oe'} || 'utf8'
l!l
09
MC/DC0%0/1
delete $param{'oe'} || 'utf8'
delete $param{'oe'}
Truth table50%1/2
A
A= delete $param{'oe'}
Aresult
11
00
77 9 ▶ my $sensor = delete $param{sensor} || 0;
Condition50%1/2
delete $param{'sensor'} || 0
l!l
09
MC/DC0%0/1
delete $param{'sensor'} || 0
delete $param{'sensor'}
Truth table50%1/2
A
A= delete $param{'sensor'}
Aresult
11
00
78 9 ▶ my $client = delete $param{client} || '';
Condition50%1/2
delete $param{'client'} || ''
l!l
09
MC/DC0%0/1
delete $param{'client'} || ''
delete $param{'client'}
Truth table50%1/2
A
A= delete $param{'client'}
Aresult
11
00
79 9 ▶ my $key = delete $param{key} || '';
Condition50%1/2
delete $param{'key'} || ''
l!l
09
MC/DC0%0/1
delete $param{'key'} || ''
delete $param{'key'}
Truth table50%1/2
A
A= delete $param{'key'}
Aresult
11
00
80 9 my $components = delete $param{components};
81  
82 9 bless {
83 ua => $ua, host => $host, language => $language,
84 region => $region, oe => $oe, sensor => $sensor,
85 client => $client, key => $key,
86 components => $components,
87 }, $class;
88 }
89  
90 =head2 geocode
91  
92 $location = $geocoder->geocode(location => $location);
93 @location = $geocoder->geocode(location => $location);
94  
95 Queries I<$location> to Google Places geocoding API and returns hash
96 reference returned back from API server. When you cann the method in
97 an array context, it returns all the candidates got back, while it
98 returns the 1st one in a scalar context.
99  
100 When you'd like to pass non-ASCII string as a location, you should
101 pass it as either UTF-8 bytes or Unicode flagged string.
102  
103 =cut
104  
105 sub geocode {
106 0 ▶ my $self = shift;
Subroutine geocode: not called CC=19 SCAR=59.4
Pod documented
107  
108 0 my %param;
109 0 ▶ if (@_ % 2 == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ % 2 == 0) { } 0 0 0
110 0 %param = @_;
111 } else {
112 0 $param{location} = shift;
113 }
114  
115 my $location = $param{location}
116 0 ▶ or Carp::croak("Usage: geocode(location => \$location)");
Branch: 1 branch
BranchTrueFalseTotal
my $location = $param{"location"} or Carp::croak("Usage: geocode(location => \$location)") 0 0 0
117  
118 0 ▶ if (Encode::is_utf8($location)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Encode::is_utf8($location)) 0 0 0
119 0 $location = Encode::encode_utf8($location);
120 }
121  
122 0 ▶ my $loc_param = $param{reverse} ? 'latlng' : 'query';
Branch: 1 branch
BranchTrueFalseTotal
$param{'reverse'} ? : 0 0 0
123  
124 0 my $uri = URI->new("https://$self->{host}/maps/api/place/textsearch/json");
125 0 my %query_parameters = ($loc_param => $location);
126 0 ▶ $query_parameters{language} = $self->{language} if defined $self->{language};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"language"} 0 0 0
127 0 ▶ $query_parameters{region} = $self->{region} if defined $self->{region};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"region"} 0 0 0
128 0 $query_parameters{oe} = $self->{oe};
129 0 ▶ $query_parameters{sensor} = $self->{sensor} ? 'true' : 'false';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'sensor'} ? : 0 0 0
130 0 my $components_params = $self->_get_components_query_params;
131 0 ▶ $query_parameters{components} = $components_params if defined $components_params;
Branch: 1 branch
BranchTrueFalseTotal
if defined $components_params 0 0 0
132 0 ▶ $query_parameters{key} = $self->{key} if(defined($self->{key}) && (length $self->{key}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"key"} and length $self->{"key"} 0 0 0
Condition0%0/3
defined $self->{"key"} and length $self->{"key"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"key"} and length $self->{"key"}
defined $self->{"key"} length $self->{"key"}
Truth table0%0/3
A and B
A= defined $self->{"key"}B= length $self->{"key"}
ABresult
0X0
100
111
133 0 $uri->query_form(%query_parameters);
134 0 my $url = $uri->as_string;
135  
136 # Process Places Premier account info
137 0 ▶ if ($self->{client} and $self->{key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"client"} and $self->{"key"}) 0 0 0
Condition0%0/3
$self->{"client"} and $self->{"key"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"client"} and $self->{"key"}
$self->{"client"} $self->{"key"}
Truth table0%0/3
A and B
A= $self->{"client"}B= $self->{"key"}
ABresult
0X0
100
111
138 0 delete $query_parameters{key};
139 0 $query_parameters{client} = $self->{client};
140 0 $uri->query_form(%query_parameters);
141  
142 0 my $signature = $self->_make_signature($uri);
143 # signature must be last parameter in query string or you get 403's
144 0 $url = $uri->as_string;
145 0 ▶ $url .= '&signature='.$signature if $signature;
Branch: 1 branch
BranchTrueFalseTotal
if $signature 0 0 0
146 }
147  
148 0 my $res = $self->{ua}->get($url);
149  
150 0 ▶ if ($res->is_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_error) 0 0 0
151 0 Carp::croak("Google Places API returned error: " . $res->status_line);
152 }
153  
154 0 my $json = JSON->new->utf8;
155 0 my $data = $json->decode($res->content);
156  
157 0 ▶ unless ($data->{status} eq 'OK' || $data->{status} eq 'ZERO_RESULTS') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data->{"status"} eq "OK" or $data->{"status"} eq "ZERO_RESULTS") 0 0 0
Condition0%0/3
$data->{"status"} eq "OK" or $data->{"status"} eq "ZERO_RESULTS"
l!l&&r!l&&!r
000
MC/DC0%0/2
$data->{"status"} eq "OK" or $data->{"status"} eq "ZERO_RESULTS"
$data->{"status"} eq "OK" $data->{"status"} eq "ZERO_RESULTS"
Truth table0%0/3
A or B
A= $data->{"status"} eq "OK"B= $data->{"status"} eq "ZERO_RESULTS"
ABresult
1X1
011
000
158 0 Carp::croak(sprintf "Google Places API returned status '%s'", $data->{status});
159 }
160  
161 0 ▶ my @results = @{ $data->{results} || [] };
Branch: 1 branch
BranchTrueFalseTotal
$data->{"results"} or [] 0 0 0
162 0 ▶ wantarray ? @results : $results[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
163 }
164  
165 =head2 reverse_geocode
166  
167 $location = $geocoder->reverse_geocode(latlng => '37.778907,-122.39732');
168 @location = $geocoder->reverse_geocode(latlng => '37.778907,-122.39732');
169  
170 Similar to geocode except it expects a latitude/longitude parameter.
171  
172 =cut
173  
174 sub reverse_geocode {
175 0 ▶ my $self = shift;
Subroutine reverse_geocode: not called CC=3 SCAR=24.8
Pod documented
176  
177 0 my %param;
178 0 ▶ if (@_ % 2 == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ % 2 == 0) { } 0 0 0
179 0 %param = @_;
180 } else {
181 0 $param{latlng} = shift;
182 }
183  
184 my $latlng = $param{latlng}
185 0 ▶ or Carp::croak("Usage: reverse_geocode(latlng => \$latlng)");
Branch: 1 branch
BranchTrueFalseTotal
my $latlng = $param{"latlng"} or Carp::croak("Usage: reverse_geocode(latlng => \$latlng)") 0 0 0
186  
187 0 return $self->geocode(location => $latlng, reverse => 1);
188 }
189  
190 # methods below adapted from
191 # http://gmaps-samples.googlecode.com/svn/trunk/urlsigning/urlsigner.pl
192 sub _decode_urlsafe_base64 {
193 0 ▶ my ($self, $content) = @_;
Subroutine _decode_urlsafe_base64: not called CC=1 SCAR=6.9
194  
195 0 $content =~ tr/-/\+/;
196 0 $content =~ tr/_/\//;
197  
198 0 return MIME::Base64::decode_base64($content);
199 }
200  
201 sub _encode_urlsafe{
202 0 ▶ my ($self, $content) = @_;
Subroutine _encode_urlsafe: not called CC=1 SCAR=6.9
203 0 $content =~ tr/\+/\-/;
204 0 $content =~ tr/\//\_/;
205  
206 0 return $content;
207 }
208  
209 sub _make_signature {
210 0 ▶ my ($self, $uri) = @_;
Subroutine _make_signature: not called CC=1 SCAR=6.9
211  
212 0 require Digest::HMAC_SHA1;
213 0 require MIME::Base64;
214  
215 0 my $key = $self->_decode_urlsafe_base64($self->{key});
216 0 my $to_sign = $uri->path_query;
217  
218 0 my $digest = Digest::HMAC_SHA1->new($key);
219 0 $digest->add($to_sign);
220 0 my $signature = $digest->b64digest;
221  
222 0 return $self->_encode_urlsafe($signature);
223 }
224  
225 # Google API wants the components formatted in the following way:
226 # <filter1>:<value1>|<filter2>:<value2>|....|<filterN>:<valueN>
227 sub _get_components_query_params {
228 7 ▶ my ($self, ) = @_;
Subroutine _get_components_query_params: called CC=5 SCAR=16.2
229 7 my $components = $self->{components};
230  
231 7 my @validated_components;
232 7 foreach my $filter (sort keys %$components ) {
233 8 ▶ next unless grep {$_ eq $filter} @ALLOWED_FILTERS;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $filter;} @ALLOWED_FILTERS 1 7 8
234 7 my $value = $components->{$filter};
235 7 ▶ if (!defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 7 7
236 0 Carp::croak("Value not specified for filter $filter");
237 }
238 # Google API expects the parameter to be passed as <filter_name>:<value>
239 7 push @validated_components, "$filter:$value";
240 }
241 7 ▶ return unless @validated_components;
Branch: 1 branch
BranchTrueFalseTotal
unless @validated_components 1 6 7
242 6 return join('|', @validated_components);
243 }
244  
245 =head2 ua
246  
247 Accessor method to get and set UserAgent object used internally. You
248 can call I<env_proxy> for example, to get the proxy information from
249 environment variables:
250  
251 $coder->ua->env_proxy(1);
252  
253 You can also set your own User-Agent object:
254  
255 $coder->ua( LWP::UserAgent::Throttled->new() );
256  
257 =cut
258  
259 sub ua {
260 0 ▶ my $self = shift;
Subroutine ua: not called CC=2 SCAR=17.9
Pod documented
261 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
262 0 $self->{ua} = shift;
263 }
264 0 $self->{ua};
265 }
266  
267 =head2 key
268  
269 Accessor method to get and set your Google API key.
270  
271 print $coder->key(), "\n";
272  
273 =cut
274  
275 sub key {
276 0 ▶ my $self = shift;
Subroutine key: not called CC=2 SCAR=17.9
Pod documented
277 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
278 0 $self->{key} = shift;
279 }
280 0 $self->{key};
281 }
282  
283 5 1;
284 __END__
↑ Summary GooglePlaces.pm »