| 1 | package Geo::Coder::Google::V3; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 1 | our $VERSION = '0.19_01'; | |||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use JSON; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use HTTP::Request; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use URI; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||
| 13 | 1 | my @ALLOWED_FILTERS = qw/route locality administrative_area postal_code country/; | |||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||
| 15 | sub new { | ||||||||||||||||||||||||||||||||
| 16 | 7 | ▶ | my($class, %param) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=10 SCAR=27.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||
| 18 | 7 | ▶ | my $ua = delete $param{ua} || LWP::UserAgent->new(agent => __PACKAGE__ . "/$VERSION"); | ||||||||||||||||||||||||||||||
|
Condition33%1/3
delete $param{'ua'} || 'LWP::UserAgent'->new('agent', 'Geo::Coder::Google::V3' . "/$Geo::Coder::Google::V3::VERSION")
MC/DC0%0/2
delete $param{'ua'} || 'LWP::UserAgent'->new('agent', 'Geo::Coder::Google::V3' . "/$Geo::Coder::Google::V3::VERSION")
delete $param{'ua'}
'LWP::UserAgent'->new('agent', 'Geo::Coder::Google::V3' . "/$Geo::Coder::Google::V3::VERSION")
Truth table33%1/3
A || B
A= delete $param{'ua'}B= 'LWP::UserAgent'->new('agent', 'Geo::Coder::Google::V3' . "/$Geo::Coder::Google::V3::VERSION")
| |||||||||||||||||||||||||||||||||
| 19 | 7 | ▶ | my $host = delete $param{host} || 'maps.googleapis.com'; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
delete $param{'host'} || 'maps.googleapis.com'
MC/DC0%0/1
delete $param{'host'} || 'maps.googleapis.com'
delete $param{'host'}
Truth table50%1/2
A
A= delete $param{'host'}
| |||||||||||||||||||||||||||||||||
| 20 | 7 | ▶ | my $language = delete $param{language} || delete $param{hl}; | ||||||||||||||||||||||||||||||
|
Condition33%1/3
delete $param{'language'} || delete $param{'hl'}
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'}
| |||||||||||||||||||||||||||||||||
| 21 | 7 | ▶ | my $region = delete $param{region} || delete $param{gl}; | ||||||||||||||||||||||||||||||
|
Condition33%1/3
delete $param{'region'} || delete $param{'gl'}
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'}
| |||||||||||||||||||||||||||||||||
| 22 | 7 | ▶ | my $oe = delete $param{oe} || 'utf8'; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
delete $param{'oe'} || 'utf8'
MC/DC0%0/1
delete $param{'oe'} || 'utf8'
delete $param{'oe'}
Truth table50%1/2
A
A= delete $param{'oe'}
| |||||||||||||||||||||||||||||||||
| 23 | 7 | ▶ | my $channel = delete $param{channel} || undef; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
delete $param{'channel'} || undef
MC/DC0%0/1
delete $param{'channel'} || undef
delete $param{'channel'}
Truth table50%1/2
A
A= delete $param{'channel'}
| |||||||||||||||||||||||||||||||||
| 24 | 7 | ▶ | my $client = delete $param{client} || ''; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
delete $param{'client'} || ''
MC/DC0%0/1
delete $param{'client'} || ''
delete $param{'client'}
Truth table50%1/2
A
A= delete $param{'client'}
| |||||||||||||||||||||||||||||||||
| 25 | 7 | ▶ | my $key = delete $param{key} || ''; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
delete $param{'key'} || ''
MC/DC0%0/1
delete $param{'key'} || ''
delete $param{'key'}
Truth table50%1/2
A
A= delete $param{'key'}
| |||||||||||||||||||||||||||||||||
| 26 | 7 | ▶ | my $apikey = delete $param{apikey} || ''; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
delete $param{'apikey'} || ''
MC/DC0%0/1
delete $param{'apikey'} || ''
delete $param{'apikey'}
Truth table50%1/2
A
A= delete $param{'apikey'}
| |||||||||||||||||||||||||||||||||
| 27 | 7 | my $components = delete $param{components}; | |||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||
| 29 | 7 | bless { | |||||||||||||||||||||||||||||||
| 30 | ua => $ua, host => $host, language => $language, | ||||||||||||||||||||||||||||||||
| 31 | region => $region, oe => $oe, channel => $channel, | ||||||||||||||||||||||||||||||||
| 32 | client => $client, key => $key, apikey => $apikey, | ||||||||||||||||||||||||||||||||
| 33 | components => $components, | ||||||||||||||||||||||||||||||||
| 34 | }, $class; | ||||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||
| 37 | sub ua { | ||||||||||||||||||||||||||||||||
| 38 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
ua: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 39 | 0 | ▶ | if (@_) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 40 | 0 | $self->{ua} = shift; | |||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||
| 42 | 0 | $self->{ua}; | |||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||
| 45 | sub reverse_geocode { | ||||||||||||||||||||||||||||||||
| 46 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
reverse_geocode: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||
| 48 | 0 | my %param; | |||||||||||||||||||||||||||||||
| 49 | 0 | ▶ | if (@_ % 2 == 0) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 50 | 0 | %param = @_; | |||||||||||||||||||||||||||||||
| 51 | } else { | ||||||||||||||||||||||||||||||||
| 52 | 0 | $param{latlng} = shift; | |||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||
| 55 | my $latlng = $param{latlng} | ||||||||||||||||||||||||||||||||
| 56 | 0 | ▶ | or Carp::croak("Usage: reverse_geocode(latlng => \$latlng)"); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||
| 58 | 0 | return $self->geocode(location => $latlng, reverse => 1); | |||||||||||||||||||||||||||||||
| 59 | }; | ||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||
| 61 | sub geocode { | ||||||||||||||||||||||||||||||||
| 62 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
geocode: not called CC=18 SCAR=58.3
Pod
documented
| |||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||
| 64 | 0 | my %param; | |||||||||||||||||||||||||||||||
| 65 | 0 | ▶ | if (@_ % 2 == 0) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 66 | 0 | %param = @_; | |||||||||||||||||||||||||||||||
| 67 | } else { | ||||||||||||||||||||||||||||||||
| 68 | 0 | $param{location} = shift; | |||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||
| 71 | my $location = $param{location} | ||||||||||||||||||||||||||||||||
| 72 | 0 | ▶ | or Carp::croak("Usage: geocode(location => \$location)"); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||
| 74 | 0 | ▶ | my $loc_param = $param{reverse} ? 'latlng' : 'address'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||
| 76 | 0 | my $uri = URI->new("https://$self->{host}/maps/api/geocode/json"); | |||||||||||||||||||||||||||||||
| 77 | 0 | my %query_parameters = ($loc_param => $location); | |||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | $query_parameters{language} = $self->{language} if defined $self->{language}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | $query_parameters{region} = $self->{region} if defined $self->{region}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 80 | 0 | $query_parameters{oe} = $self->{oe}; | |||||||||||||||||||||||||||||||
| 81 | 0 | ▶ | $query_parameters{channel} = $self->{channel} if defined $self->{channel}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 82 | 0 | my $components_params = $self->_get_components_query_params; | |||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | $query_parameters{components} = $components_params if defined $components_params; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 84 | 0 | ▶ | $query_parameters{key} = $self->{key} if defined $self->{key}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 85 | 0 | $uri->query_form(%query_parameters); | |||||||||||||||||||||||||||||||
| 86 | 0 | my $url = $uri->as_string; | |||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||
| 88 | # Process Maps Premier account info | ||||||||||||||||||||||||||||||||
| 89 | 0 | ▶ | if ($self->{client} and $self->{key}) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
$self->{"client"} and $self->{"key"}
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"}
| |||||||||||||||||||||||||||||||||
| 90 | 0 | delete $query_parameters{key}; | |||||||||||||||||||||||||||||||
| 91 | 0 | $query_parameters{client} = $self->{client}; | |||||||||||||||||||||||||||||||
| 92 | 0 | $uri->query_form(%query_parameters); | |||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||
| 94 | 0 | my $signature = $self->make_signature($uri); | |||||||||||||||||||||||||||||||
| 95 | # signature must be last parameter in query string or you get 403's | ||||||||||||||||||||||||||||||||
| 96 | 0 | $url = $uri->as_string; | |||||||||||||||||||||||||||||||
| 97 | 0 | ▶ | $url .= '&signature='.$signature if $signature; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 98 | } elsif ($self->{apikey}) { | ||||||||||||||||||||||||||||||||
| 99 | 0 | $query_parameters{key} = $self->{apikey}; | |||||||||||||||||||||||||||||||
| 100 | 0 | $uri->query_form(%query_parameters); | |||||||||||||||||||||||||||||||
| 101 | 0 | $url = $uri->as_string; | |||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||
| 104 | 0 | my $res = $self->{ua}->get($url); | |||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||
| 106 | 0 | ▶ | if ($res->is_error) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 107 | 0 | Carp::croak("Google Maps API returned error: " . $res->status_line); | |||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||
| 110 | 0 | my $json = JSON->new->utf8; | |||||||||||||||||||||||||||||||
| 111 | 0 | my $data = $json->decode($res->content); | |||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||
| 113 | 0 | ▶ | unless ($data->{status} eq 'OK' || $data->{status} eq 'ZERO_RESULTS') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$data->{"status"} eq "OK" or $data->{"status"} eq "ZERO_RESULTS"
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"
| |||||||||||||||||||||||||||||||||
| 114 | 0 | Carp::croak(sprintf "Google Maps API returned status '%s'", $data->{status}); | |||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||
| 117 | 0 | ▶ | my @results = @{ $data->{results} || [] }; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 118 | 0 | ▶ | wantarray ? @results : $results[0]; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||
| 121 | # methods below adapted from | ||||||||||||||||||||||||||||||||
| 122 | # http://gmaps-samples.googlecode.com/svn/trunk/urlsigning/urlsigner.pl | ||||||||||||||||||||||||||||||||
| 123 | sub decode_urlsafe_base64 { | ||||||||||||||||||||||||||||||||
| 124 | 0 | ▶ | my ($self, $content) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
decode_urlsafe_base64: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||
| 126 | 0 | $content =~ tr/-/\+/; | |||||||||||||||||||||||||||||||
| 127 | 0 | $content =~ tr/_/\//; | |||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||
| 129 | 0 | return MIME::Base64::decode_base64($content); | |||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||
| 132 | sub encode_urlsafe{ | ||||||||||||||||||||||||||||||||
| 133 | 0 | ▶ | my ($self, $content) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
encode_urlsafe: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 134 | 0 | $content =~ tr/\+/\-/; | |||||||||||||||||||||||||||||||
| 135 | 0 | $content =~ tr/\//\_/; | |||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||
| 137 | 0 | return $content; | |||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||
| 140 | sub make_signature { | ||||||||||||||||||||||||||||||||
| 141 | 0 | ▶ | my ($self, $uri) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
make_signature: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||
| 143 | 0 | require Digest::HMAC_SHA1; | |||||||||||||||||||||||||||||||
| 144 | 0 | require MIME::Base64; | |||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||
| 146 | 0 | my $key = $self->decode_urlsafe_base64($self->{key}); | |||||||||||||||||||||||||||||||
| 147 | 0 | my $to_sign = $uri->path_query; | |||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||
| 149 | 0 | my $digest = Digest::HMAC_SHA1->new($key); | |||||||||||||||||||||||||||||||
| 150 | 0 | $digest->add($to_sign); | |||||||||||||||||||||||||||||||
| 151 | 0 | my $signature = $digest->b64digest; | |||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||
| 153 | 0 | return $self->encode_urlsafe($signature); | |||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||
| 156 | # Google API wants the components formatted in the following way: | ||||||||||||||||||||||||||||||||
| 157 | # <filter1>:<value1>|<filter2>:<value2>|....|<filterN>:<valueN> | ||||||||||||||||||||||||||||||||
| 158 | sub _get_components_query_params { | ||||||||||||||||||||||||||||||||
| 159 | 7 | ▶ | my ($self, ) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_get_components_query_params: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||||||
| 160 | 7 | my $components = $self->{components}; | |||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||
| 162 | 7 | my @validated_components; | |||||||||||||||||||||||||||||||
| 163 | 7 | foreach my $filter (sort keys %$components ) { | |||||||||||||||||||||||||||||||
| 164 | 8 | ▶ | next unless grep {$_ eq $filter} @ALLOWED_FILTERS; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 165 | 7 | my $value = $components->{$filter}; | |||||||||||||||||||||||||||||||
| 166 | 7 | ▶ | if (!defined $value) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 167 | 0 | Carp::croak("Value not specified for filter $filter"); | |||||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||||||
| 169 | # Google API expects the parameter to be passed as <filter_name>:<value> | ||||||||||||||||||||||||||||||||
| 170 | 7 | push @validated_components, "$filter:$value"; | |||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||
| 172 | 7 | ▶ | return unless @validated_components; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 173 | 6 | return join('|', @validated_components); | |||||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||
| 176 | 1 | 1; | |||||||||||||||||||||||||||||||
| 177 | __END__ | ||||||||||||||||||||||||||||||||