blib/lib/Geo/Coder/Googlev3.pm

filter: Statementstmt 93.4% 100 / 107 Branchbran 80.0% 40 / 50 Conditioncond 75.0% 9 / 12 MC/DCmcdc 57.1% 4 / 7 Subroutinesub 100% 17 / 17 Podpod 75.0% 6 / 8 totaltotal 87.5% 176 / 201 SCAR 34.5
CC
30
Cov
88%
geocode
8.2
geocode_url
8.0
bounds
8.0
CRAP 31.5
↑ Summary
1 # -*- mode:perl; coding:iso-8859-1 -*-
2  
3 #
4 # Author: Slaven Rezic
5 #
6 # Copyright (C) 2010,2011,2013,2014,2017 Slaven Rezic. All rights reserved.
7 # This package is free software; you can redistribute it and/or
8 # modify it under the same terms as Perl itself.
9 #
10  
11 package Geo::Coder::Googlev3;
12  
13 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 our $VERSION = '0.16';
16  
17 1 ▶use Carp ('croak');
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use JSON::XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use LWP::UserAgent ();
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use URI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use URI::QueryParam ();
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 sub new {
25 11 ▶ my($class, %args) = @_;
Subroutine new: called CC=7 SCAR=19.6
Pod documented
26 11 my $self = bless {}, $class;
27 $self->{ua} = delete $args{ua} ||
28 11 ▶ LWP::UserAgent->new(
Condition33%1/3
delete $args{'ua'} || 'LWP::UserAgent'->new('agent', 'Geo::Coder::Googlev3' . "/$Geo::Coder::Googlev3::VERSION libwww-perl/$LWP::VERSION", 'env_proxy', 1, 'timeout', 15)
l!l&&r!l&&!r
0110
MC/DC0%0/2
delete $args{'ua'} || 'LWP::UserAgent'->new('agent', 'Geo::Coder::Googlev3' . "/$Geo::Coder::Googlev3::VERSION libwww-perl/$LWP::VERSION", 'env_proxy', 1, 'timeout', 15)
delete $args{'ua'} 'LWP::UserAgent'->new('agent', 'Geo::Coder::Googlev3' . "/$Geo::Coder::Googlev3::VERSION libwww-perl/$LWP::VERSION", 'env_proxy', 1, 'timeout', 15)
Truth table33%1/3
A || B
A= delete $args{'ua'}B= 'LWP::UserAgent'->new('agent', 'Geo::Coder::Googlev3' . "/$Geo::Coder::Googlev3::VERSION libwww-perl/$LWP::VERSION", 'env_proxy', 1, 'timeout', 15)
ABresult
1X1
011
000
29 agent => __PACKAGE__ . "/$VERSION libwww-perl/$LWP::VERSION",
30 env_proxy => 1,
31 timeout => 15,
32 );
33 11 ▶ $self->{region} = delete $args{region} || delete $args{gl};
Condition66%2/3
delete $args{'region'} || delete $args{'gl'}
l!l&&r!l&&!r
0110
MC/DC50%1/2
delete $args{'region'} || delete $args{'gl'}
delete $args{'region'} delete $args{'gl'}
Truth table66%2/3
A || B
A= delete $args{'region'}B= delete $args{'gl'}
ABresult
1X1
011
000
34 11 $self->{language} = delete $args{language};
35 {
36 11 my $sensor;
37 11 ▶ if ($args{sensor}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"sensor"}) 3 8 11
38 3 $sensor = delete $args{sensor};
39 3 ▶ if ($sensor !~ m{^(false|true)$}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sensor =~ /^(false|true)$/) 1 2 3
40 1 croak "sensor argument has to be either 'false' or 'true'";
41 }
42 }
43 10 $self->{sensor} = $sensor;
44 }
45 10 ▶ if ($args{bounds}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"bounds"}) 1 9 10
46 1 $self->bounds(delete $args{bounds});
47 }
48 10 $self->{key} = delete $args{key};
49 10 $self->{use_https} = delete $args{use_https};
50 10 ▶ croak "Unsupported arguments: " . join(" ", %args) if %args;
Branch: 1 branch
BranchTrueFalseTotal
if %args 0 10 10
51 10 $self;
52 }
53  
54 sub ua {
55 17 ▶ my $self = shift;
Subroutine ua: called CC=2 SCAR=7.6
Pod undocumented
56 17 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 17 17
57 0 $self->{ua} = shift;
58 }
59 17 $self->{ua};
60 }
61  
62 sub geocode {
63 16 ▶ my($self, %args) = @_;
Subroutine geocode: called CC=6 SCAR=21.0
Pod documented
64 16 my $raw = delete $args{raw};
65 16 my $url = $self->geocode_url(%args);
66 16 my $ua = $self->ua;
67 16 my $resp = $ua->get($url);
68 16 ▶ if ($resp->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resp->is_success) { } 16 0 16
69 16 my $content = $resp->decoded_content(charset => "none");
70 16 my $res = JSON::XS->new->utf8->decode($content);
71 16 ▶ if ($raw) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raw) 1 15 16
72 1 return $res;
73 }
74 15 ▶ if ($res->{status} eq 'OK') {
Branch: 2 branches
BranchTrueFalseTotal
if ($res->{'status'} eq 'OK') { } 0 15 15
elsif ($res->{'status'} eq 'ZERO_RESULTS') { } 0 15 15
75 0 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
76 0 return @{ $res->{results} };
77 } else {
78 0 return $res->{results}->[0];
79 }
80 } elsif ($res->{status} eq 'ZERO_RESULTS') {
81 0 return;
82 } else {
83 15 croak "Fetching $url did not return OK status, but '" . $res->{status} . "'";
84 }
85 } else {
86 0 croak "Fetching $url failed: " . $resp->status_line;
87 }
88 }
89  
90 # private!
91 sub geocode_url {
92 19 ▶ my($self, %args) = @_;
Subroutine geocode_url: called CC=8 SCAR=20.8
Pod undocumented
93 19 my $loc = $args{location};
94 19 ▶ my $url = URI->new(($self->{use_https} ? 'https' : 'http') . '://maps.google.com/maps/api/geocode/json');
Branch: 1 branch
BranchTrueFalseTotal
$self->{'use_https'} ? : 1 18 19
95 19 my %url_params;
96 19 $url_params{address} = $loc;
97 19 ▶ $url_params{sensor} = $self->{sensor} if defined $self->{sensor};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"sensor"} 2 17 19
98 19 ▶ $url_params{region} = $self->{region} if defined $self->{region};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"region"} 1 18 19
99 19 ▶ $url_params{language} = $self->{language} if defined $self->{language};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"language"} 2 17 19
100 19 ▶ if (defined $self->{bounds}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"bounds"}) 1 18 19
101 1 $url_params{bounds} = join '|', map { $_->{lat}.','.$_->{lng} } @{ $self->{bounds} };
102 }
103 19 ▶ $url_params{key} = $self->{key} if defined $self->{key};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"key"} 1 18 19
104 19 ▶ while(my($k,$v) = each %url_params) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %url_params) 26 19 45
105 26 $url->query_param($k => Encode::encode_utf8($v));
106 }
107 19 $url = $url->as_string;
108 19 $url;
109 }
110  
111 sub region {
112 1 ▶ my $self = shift;
Subroutine region: called CC=2 SCAR=7.1
Pod documented
113 1 ▶ $self->{region} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 1 1
114 1 return $self->{region};
115 }
116  
117  
118 sub language {
119 1 ▶ my $self = shift;
Subroutine language: called CC=2 SCAR=7.1
Pod documented
120 1 ▶ $self->{language} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 1 1
121 1 return $self->{language};
122 }
123  
124 sub sensor {
125 3 ▶ my $self = shift;
Subroutine sensor: called CC=2 SCAR=7.1
Pod documented
126 3 ▶ $self->{sensor} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 3 3
127 3 return $self->{sensor};
128 }
129  
130 1 ▶use constant _BOUNDS_ERROR_MSG => "bounds must be in the form [{lat=>...,lng=>...}, {lat=>...,lng=>...}]";
Subroutine BEGIN: called CC=1 SCAR=0.0
131  
132 sub bounds {
133 7 ▶ my $self = shift;
Subroutine bounds: called CC=8 SCAR=20.8
Pod documented
134 7 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 5 2 7
135 5 my $bounds = shift;
136 5 ▶ if (ref $bounds ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $bounds ne "ARRAY") 1 4 5
137 1 croak _BOUNDS_ERROR_MSG . ', but the supplied parameter is not even an array reference.';
138 }
139 4 ▶ if (@$bounds != 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$bounds != 2) 1 3 4
140 1 croak _BOUNDS_ERROR_MSG . ', but the supplied parameter has not exactly two array elements.';
141 }
142 3 ▶ if ((grep { ref $_ eq 'HASH' && exists $_->{lng} && exists $_->{lat} ? 1 : 0 } @$bounds) != 2) {
Branch: 2 branches
BranchTrueFalseTotal
if (grep({ref $_ eq "HASH" && exists $_->{"lng"} && exists $_->{"lat"} ? 1 : 0;} @$bounds) != 2) 2 1 3
ref $_ eq 'HASH' && exists $_->{'lng'} && exists $_->{'lat'} ? : 2 4 6
Condition100%6/6
ref $_ eq "HASH" and exists $_->{"lng"} and exists $_->{"lat"}
!ll&&!rl&&r
312
ref $_ eq "HASH" and exists $_->{"lng"}
!ll&&!rl&&r
213
MC/DC100%3/3
ref $_ eq "HASH" and exists $_->{"lng"} and exists $_->{"lat"}
ref $_ eq "HASH" exists $_->{"lng"} exists $_->{"lat"}
Truth table100%4/4
A and B and C
A= ref $_ eq "HASH"B= exists $_->{"lng"}C= exists $_->{"lat"}
ABCresult
0XX0
10X0
1100
1111
143 2 croak _BOUNDS_ERROR_MSG . ', but the supplied elements are not lat/lng hashes.';
144 }
145 1 $self->{bounds} = $bounds;
146 }
147 3 return $self->{bounds};
148 }
149  
150 1 1;
151  
152 __END__
↑ Summary