| 1 | package Geo::Distance::Google; | ||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||
| 6 | 3 | our $VERSION = '0.01'; | |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use Carp; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use Data::Dumper qw( Dumper ); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use Encode; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use JSON; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use HTTP::Request; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 14 | 3 | ▶ | use Params::Validate; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 15 | 3 | ▶ | use URI; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||
| 17 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||||
|
Subroutine
new: called CC=12 SCAR=29.0
Pod
documented
| |||||||||||||||||||||||||||||||
| 18 | 1 | my($class, %param) = @_; | |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||
| 20 | 1 | ▶ | my $ua = delete $param{ua} || LWP::UserAgent->new(agent => __PACKAGE__ . "/$VERSION"); | ||||||||||||||||||||||||||||
|
Condition33%1/3
delete $param{'ua'} || 'LWP::UserAgent'->new('agent', 'Geo::Distance::Google' . "/$Geo::Distance::Google::VERSION")
MC/DC0%0/2
delete $param{'ua'} || 'LWP::UserAgent'->new('agent', 'Geo::Distance::Google' . "/$Geo::Distance::Google::VERSION")
delete $param{'ua'}
'LWP::UserAgent'->new('agent', 'Geo::Distance::Google' . "/$Geo::Distance::Google::VERSION")
Truth table33%1/3
A || B
A= delete $param{'ua'}B= 'LWP::UserAgent'->new('agent', 'Geo::Distance::Google' . "/$Geo::Distance::Google::VERSION")
| |||||||||||||||||||||||||||||||
| 21 | 1 | ▶ | 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'}
| |||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||
| 23 | 1 | ▶ | 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'}
| |||||||||||||||||||||||||||||||
| 24 | 1 | ▶ | 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'}
| |||||||||||||||||||||||||||||||
| 25 | 1 | ▶ | 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'}
| |||||||||||||||||||||||||||||||
| 26 | 1 | ▶ | my $sensor = delete $param{sensor} || 0; | ||||||||||||||||||||||||||||
|
Condition50%1/2
delete $param{'sensor'} || 0
MC/DC0%0/1
delete $param{'sensor'} || 0
delete $param{'sensor'}
Truth table50%1/2
A
A= delete $param{'sensor'}
| |||||||||||||||||||||||||||||||
| 27 | 1 | ▶ | 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'}
| |||||||||||||||||||||||||||||||
| 28 | 1 | ▶ | 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'}
| |||||||||||||||||||||||||||||||
| 29 | 1 | ▶ | my $units = delete $param{units} || ''; | ||||||||||||||||||||||||||||
|
Condition50%1/2
delete $param{'units'} || ''
MC/DC0%0/1
delete $param{'units'} || ''
delete $param{'units'}
Truth table50%1/2
A
A= delete $param{'units'}
| |||||||||||||||||||||||||||||||
| 30 | 1 | my $mode = delete $param{mode}; | |||||||||||||||||||||||||||||
| 31 | 1 | my $avoid = delete $param{avoid}; | |||||||||||||||||||||||||||||
| 32 | 1 | ▶ | my $https = delete $param{https} || 0; | ||||||||||||||||||||||||||||
|
Condition50%1/2
delete $param{'https'} || 0
MC/DC0%0/1
delete $param{'https'} || 0
delete $param{'https'}
Truth table50%1/2
A
A= delete $param{'https'}
| |||||||||||||||||||||||||||||||
| 33 | 1 | ▶ | my $debug = delete $param{debug} || 0; | ||||||||||||||||||||||||||||
|
Condition50%1/2
delete $param{'debug'} || 0
MC/DC0%0/1
delete $param{'debug'} || 0
delete $param{'debug'}
Truth table50%1/2
A
A= delete $param{'debug'}
| |||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||
| 35 | 1 | bless { | |||||||||||||||||||||||||||||
| 36 | ua => $ua, | ||||||||||||||||||||||||||||||
| 37 | host => $host, | ||||||||||||||||||||||||||||||
| 38 | language => $language, | ||||||||||||||||||||||||||||||
| 39 | region => $region, | ||||||||||||||||||||||||||||||
| 40 | oe => $oe, | ||||||||||||||||||||||||||||||
| 41 | sensor => $sensor, | ||||||||||||||||||||||||||||||
| 42 | client => $client, | ||||||||||||||||||||||||||||||
| 43 | key => $key, | ||||||||||||||||||||||||||||||
| 44 | units => $units, | ||||||||||||||||||||||||||||||
| 45 | mode => $mode, | ||||||||||||||||||||||||||||||
| 46 | avoid => $avoid, | ||||||||||||||||||||||||||||||
| 47 | https => $https, | ||||||||||||||||||||||||||||||
| 48 | __debug__ => $debug | ||||||||||||||||||||||||||||||
| 49 | }, $class; | ||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||
| 52 | 2 | ▶ | sub debug_level { | ||||||||||||||||||||||||||||
|
Subroutine
debug_level: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||||||||||||||||||||
| 53 | 2 | my $self = shift; | |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||
| 55 | 0 | ▶ | if ( @_ ) { $self->{__debug__} = shift; } | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||
| 57 | 2 | return $self->{__debug__}; | |||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||
| 60 | 0 | ▶ | sub ua { | ||||||||||||||||||||||||||||
|
Subroutine
ua: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||
| 61 | 0 | my $self = shift; | |||||||||||||||||||||||||||||
| 62 | 0 | ▶ | if (@_) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 63 | 0 | $self->{ua} = shift; | |||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||
| 65 | 0 | $self->{ua}; | |||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||
| 68 | 1 | ▶ | sub raw_distance { | ||||||||||||||||||||||||||||
|
Subroutine
raw_distance: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||
| 69 | 1 | my $self = shift; | |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||
| 71 | 1 | ▶ | $self->{__raw_response__} = shift if @_; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||
| 73 | 1 | return $self->{__raw_response__}; | |||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||
| 76 | 1 | ▶ | sub distance { | ||||||||||||||||||||||||||||
|
Subroutine
distance: called CC=29 SCAR=51.3
Pod
documented
| |||||||||||||||||||||||||||||||
| 77 | 1 | my $self = shift; | |||||||||||||||||||||||||||||
| 78 | 1 | my %p = validate @_, { | |||||||||||||||||||||||||||||
| 79 | origins => 1, | ||||||||||||||||||||||||||||||
| 80 | destinations => 1, | ||||||||||||||||||||||||||||||
| 81 | mode => 0, | ||||||||||||||||||||||||||||||
| 82 | avoid => 0, | ||||||||||||||||||||||||||||||
| 83 | units => 0 | ||||||||||||||||||||||||||||||
| 84 | }; | ||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||
| 86 | 1 | my $origins; | |||||||||||||||||||||||||||||
| 87 | my $destinations; | ||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||
| 89 | # both can be array refs or single items | ||||||||||||||||||||||||||||||
| 90 | 1 | foreach my $k ( qw( origins destinations ) ) { | |||||||||||||||||||||||||||||
| 91 | # convert to google format | ||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||
| 93 | # TODO: in future allow seperate lat & long in hash ref | ||||||||||||||||||||||||||||||
| 94 | 2 | ▶ | if ( ref $p{$k} ne 'ARRAY' ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 95 | 2 | $p{$k} = [ $p{$k} ]; | |||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||
| 99 | 1 | $origins = join '|', @{ $p{origins} }; | |||||||||||||||||||||||||||||
| 100 | 1 | $destinations = join '|', @{ $p{destinations} }; | |||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||
| 103 | 1 | ▶ | $origins = Encode::is_utf8( $origins ) ? Encode::encode_utf8( $origins ) : $origins; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 104 | 1 | ▶ | $destinations = Encode::is_utf8( $destinations ) ? Encode::encode_utf8( $destinations ) : $destinations; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||
| 106 | my $url = sprintf "%s://%s/maps/api/distancematrix/json", | ||||||||||||||||||||||||||||||
| 107 | 1 | ▶ | ( $self->{https} ? 'https' : 'http' ), $self->{host}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||
| 109 | 1 | my $uri = URI->new($url); | |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||
| 111 | # build query | ||||||||||||||||||||||||||||||
| 112 | my %query_parameters = ( | ||||||||||||||||||||||||||||||
| 113 | origins => $origins, | ||||||||||||||||||||||||||||||
| 114 | destinations => $destinations, | ||||||||||||||||||||||||||||||
| 115 | oe => $self->{oe}, | ||||||||||||||||||||||||||||||
| 116 | sensor => ( $self->{sensor} ? 'true' : 'false' ), | ||||||||||||||||||||||||||||||
| 117 | # optional parameters | ||||||||||||||||||||||||||||||
| 118 | ( $self->{units} ? ( units => $self->{units} ) : () ), | ||||||||||||||||||||||||||||||
| 119 | ( $p{mode} ? ( mode => $p{mode} ) | ||||||||||||||||||||||||||||||
| 120 | : defined $self->{mode} ? ( mode => $self->{mode} ) : ()), | ||||||||||||||||||||||||||||||
| 121 | # TODO: add support for avoid as list ref process too | ||||||||||||||||||||||||||||||
| 122 | ( $p{avoid} ? ( avoid => $p{avoid} ) | ||||||||||||||||||||||||||||||
| 123 | 1 | ▶ | : defined $self->{avoid} ? ( avoid => $self->{avoid} ) : () ) | ||||||||||||||||||||||||||||
|
Branch: 6 branches
| |||||||||||||||||||||||||||||||
| 124 | ); | ||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||
| 126 | # not sure about these | ||||||||||||||||||||||||||||||
| 127 | 1 | ▶ | $query_parameters{language} = $self->{language} if defined $self->{language}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 128 | 1 | ▶ | $query_parameters{region} = $self->{region} if defined $self->{region}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||
| 130 | 1 | $uri->query_form(%query_parameters); | |||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||
| 132 | # setup request | ||||||||||||||||||||||||||||||
| 133 | 1 | $url = $uri->as_string; | |||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||
| 135 | # Process Maps Premier account info | ||||||||||||||||||||||||||||||
| 136 | 1 | ▶ | if ($self->{client} and $self->{key}) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"client"} and $self->{"key"}
MC/DC0%0/2
$self->{"client"} and $self->{"key"}
$self->{"client"}
$self->{"key"}
Truth table33%1/3
A and B
A= $self->{"client"}B= $self->{"key"}
| |||||||||||||||||||||||||||||||
| 137 | 0 | $query_parameters{client} = $self->{client}; | |||||||||||||||||||||||||||||
| 138 | 0 | $uri->query_form(%query_parameters); | |||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||
| 140 | 0 | my $signature = $self->make_signature($uri); | |||||||||||||||||||||||||||||
| 141 | # signature must be last parameter in query string or you get 403's | ||||||||||||||||||||||||||||||
| 142 | 0 | $url = $uri->as_string; | |||||||||||||||||||||||||||||
| 143 | 0 | ▶ | $url .= '&signature='.$signature if $signature; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||
| 146 | 1 | $self->debug( "Sending request: $url" ); | |||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||
| 148 | 1 | my $res = $self->{ua}->get($url); | |||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||
| 150 | 1 | ▶ | if ($res->is_error) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 151 | 0 | Carp::croak("Google Maps API returned error: " . $res->status_line); | |||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||
| 154 | 1 | ▶ | if ( $res->headers->content_type !~ /json/ ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 155 | 0 | my $ct = $res->headers->content_type; | |||||||||||||||||||||||||||||
| 156 | 0 | croak "Invalid content-type '$ct' returned from webserver"; | |||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||
| 159 | 1 | my $json = JSON->new->utf8; | |||||||||||||||||||||||||||||
| 160 | 1 | my $data = $json->decode($res->content); | |||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||
| 162 | 1 | $self->raw_distance( $data ); | |||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||
| 164 | 1 | $self->debug( "data: " . Dumper( $data ) ); | |||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||
| 166 | 1 | ▶ | if ( ! defined $data->{status} || $data->{status} ne 'OK' ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $data->{"status"} or $data->{"status"} ne "OK"
MC/DC0%0/2
not defined $data->{"status"} or $data->{"status"} ne "OK"
not defined $data->{"status"}
$data->{"status"} ne "OK"
Truth table33%1/3
not(A) or B
A= not defined $data->{"status"}B= $data->{"status"} ne "OK"
| |||||||||||||||||||||||||||||||
| 167 | 1 | ▶ | croak "Google Maps API status error: " . ( $data->{status} || 'Invalid status' ); | ||||||||||||||||||||||||||||
|
Condition50%1/2
$data->{'status'} || 'Invalid status'
MC/DC0%0/1
$data->{'status'} || 'Invalid status'
$data->{'status'}
Truth table50%1/2
A
A= $data->{'status'}
| |||||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||
| 170 | # reprocess to make more friendly (IMO) | ||||||||||||||||||||||||||||||
| 171 | 0 | my $distance = []; | |||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||
| 173 | # origins[0] correspond to rows[0] | ||||||||||||||||||||||||||||||
| 174 | # destinations[0] correspond to rows->[x]->elements[0] | ||||||||||||||||||||||||||||||
| 175 | 0 | ▶ | for ( my $oid = 0; $oid < scalar( @{ $p{origins} } ); $oid++ ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||
| 177 | # verify origin information | ||||||||||||||||||||||||||||||
| 178 | next unless defined $data->{origin_addresses} && | ||||||||||||||||||||||||||||||
| 179 | 0 | ▶ | defined $data->{origin_addresses}->[$oid]; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $data->{"origin_addresses"} and defined $data->{"origin_addresses"}[$oid]
MC/DC0%0/2
defined $data->{"origin_addresses"} and defined $data->{"origin_addresses"}[$oid]
defined $data->{"origin_addresses"}
defined $data->{"origin_addresses"}[$oid]
Truth table0%0/3
A and B
A= defined $data->{"origin_addresses"}B= defined $data->{"origin_addresses"}[$oid]
| |||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||
| 181 | # missing return data | ||||||||||||||||||||||||||||||
| 182 | 0 | ▶ | next unless defined $data->{rows} && $data->{rows}->[$oid]; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $data->{"rows"} and $data->{"rows"}[$oid]
MC/DC0%0/2
defined $data->{"rows"} and $data->{"rows"}[$oid]
defined $data->{"rows"}
$data->{"rows"}[$oid]
Truth table0%0/3
A and B
A= defined $data->{"rows"}B= $data->{"rows"}[$oid]
| |||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||
| 184 | 0 | $distance->[$oid]->{origin_address} = $data->{origin_addresses}->[$oid]; | |||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||
| 186 | 0 | my $elements = $data->{rows}->[$oid]->{elements}; | |||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||
| 188 | # loop through each destination address | ||||||||||||||||||||||||||||||
| 189 | 0 | ▶ | foreach ( my $did = 0; $did < scalar( @{ $p{destinations} } ); $did++ ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 190 | 0 | ▶ | next unless defined $elements->[$did]; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||
| 192 | # reformat it to be a bit nicer for the consumer | ||||||||||||||||||||||||||||||
| 193 | $distance->[$oid]->{destinations}->[$did] = { | ||||||||||||||||||||||||||||||
| 194 | address => $data->{destination_addresses}->[$did], | ||||||||||||||||||||||||||||||
| 195 | distance => $elements->[$did]->{distance}, | ||||||||||||||||||||||||||||||
| 196 | duration => $elements->[$did]->{duration}, | ||||||||||||||||||||||||||||||
| 197 | status => $elements->[$did]->{status} | ||||||||||||||||||||||||||||||
| 198 | 0 | }; | |||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||
| 203 | 0 | $self->debug( "distance: " . Dumper($distance) ); | |||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||
| 205 | 0 | return $distance; | |||||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||
| 208 | # methods below adapted from | ||||||||||||||||||||||||||||||
| 209 | # http://gmaps-samples.googlecode.com/svn/trunk/urlsigning/urlsigner.pl | ||||||||||||||||||||||||||||||
| 210 | 0 | ▶ | sub decode_urlsafe_base64 { | ||||||||||||||||||||||||||||
|
Subroutine
decode_urlsafe_base64: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||
| 211 | 0 | my ($self, $content) = @_; | |||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||
| 213 | 0 | $content =~ tr/-/\+/; | |||||||||||||||||||||||||||||
| 214 | 0 | $content =~ tr/_/\//; | |||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||
| 216 | 0 | return MIME::Base64::decode_base64($content); | |||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||
| 219 | 0 | ▶ | sub encode_urlsafe{ | ||||||||||||||||||||||||||||
|
Subroutine
encode_urlsafe: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||
| 220 | 0 | my ($self, $content) = @_; | |||||||||||||||||||||||||||||
| 221 | 0 | $content =~ tr/\+/\-/; | |||||||||||||||||||||||||||||
| 222 | 0 | $content =~ tr/\//\_/; | |||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||
| 224 | 0 | return $content; | |||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||
| 227 | 0 | ▶ | sub make_signature { | ||||||||||||||||||||||||||||
|
Subroutine
make_signature: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||
| 228 | 0 | my ($self, $uri) = @_; | |||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||
| 230 | 0 | require Digest::HMAC_SHA1; | |||||||||||||||||||||||||||||
| 231 | 0 | require MIME::Base64; | |||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||
| 233 | 0 | my $key = $self->decode_urlsafe_base64($self->{key}); | |||||||||||||||||||||||||||||
| 234 | 0 | my $to_sign = $uri->path_query; | |||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||
| 236 | 0 | my $digest = Digest::HMAC_SHA1->new($key); | |||||||||||||||||||||||||||||
| 237 | 0 | $digest->add($to_sign); | |||||||||||||||||||||||||||||
| 238 | 0 | my $signature = $digest->b64digest; | |||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||
| 240 | 0 | return $self->encode_urlsafe($signature); | |||||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||
| 243 | # search input hash ref, then self for defined parameter or | ||||||||||||||||||||||||||||||
| 244 | # return empty list | ||||||||||||||||||||||||||||||
| 245 | 0 | ▶ | sub _get_multiple { | ||||||||||||||||||||||||||||
|
Subroutine
_get_multiple: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||||
| 246 | 0 | my $self = shift; | |||||||||||||||||||||||||||||
| 247 | 0 | ▶ | my $p = shift || return (); # params | ||||||||||||||||||||||||||||
|
Condition0%0/2
shift() || (return ())
MC/DC0%0/1
shift() || (return ())
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||||
| 248 | 0 | ▶ | my $key = shift || return (); # key | ||||||||||||||||||||||||||||
|
Condition0%0/2
shift() || (return ())
MC/DC0%0/1
shift() || (return ())
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||
| 250 | 0 | ▶ | return () unless ref $p eq 'HASH'; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||
| 252 | return defined $p->{$key} ? ( $key => $p->{$key} ) | ||||||||||||||||||||||||||||||
| 253 | 0 | ▶ | : defined $self->{$key} ? ( $key => $self->{$key} ) : (); | ||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||
| 256 | 2 | ▶ | sub debug { | ||||||||||||||||||||||||||||
|
Subroutine
debug: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||||||||||||||||||||
| 257 | 2 | my $self = shift; | |||||||||||||||||||||||||||||
| 258 | 2 | my $f = (caller(1))[3]; | |||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||
| 260 | 2 | ▶ | return unless $self->debug_level; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||
| 262 | 0 | printf STDERR "%s [%s] %s\n", scalar( localtime ), $f, shift; | |||||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||
| 266 | 3 | 1; | |||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||
| 268 | __END__ | ||||||||||||||||||||||||||||||