Google/V3.pm

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