lib/Geo/Coder/OpenCage.pm

filter: Statementstmt 84.6% 88 / 104 Branchbran 73.5% 25 / 34 Conditioncond 42.8% 9 / 21 MC/DCmcdc 8.3% 1 / 12 Subroutinesub 100% 14 / 14 Podpod 100% 4 / 4 totaltotal 74.6% 141 / 189 CC 29 SCAR 26.0
CC
29
Cov
77%
geocode
27.7
_transport_error
7.1
new
5.4
CRAP 13.4
↑ Summary
1 package Geo::Coder::OpenCage 0.44;
2 # ABSTRACT: Geocode coordinates and addresses with the OpenCage Geocoding API
3  
4 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 12 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 12 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 12 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 12 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 12 ▶use URI::QueryParam;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 12 ▶my $version = our $VERSION || 'dev';
Condition50%1/2
our $VERSION || 'dev'
l!l
120
MC/DC0%0/1
our $VERSION || 'dev'
our $VERSION
Truth table50%1/2
A
A= our $VERSION
Aresult
11
00
15 12 my $ua_string = "Geo::Coder::OpenCage/$version";
16  
17 11 ▶sub new {
Subroutine new: called CC=5 SCAR=16.8
Pod documented
18 11 my ($class, %params) = @_;
19  
20 11 ▶ if (!$params{api_key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($params{"api_key"}) 1 10 11
21 1 croak "api_key is a required parameter for new()";
22 }
23  
24 # verify_SSL because HTTP::Tiny's default was off before 0.083
25 10 ▶ my $ua = $params{ua} || HTTP::Tiny->new(timeout => 10, verify_SSL => 1);
Condition66%2/3
$params{'ua'} || 'HTTP::Tiny'->new('timeout', 10, 'verify_SSL', 1)
l!l&&r!l&&!r
550
MC/DC0%0/2
$params{'ua'} || 'HTTP::Tiny'->new('timeout', 10, 'verify_SSL', 1)
$params{'ua'} 'HTTP::Tiny'->new('timeout', 10, 'verify_SSL', 1)
Truth table66%2/3
A || B
A= $params{'ua'}B= 'HTTP::Tiny'->new('timeout', 10, 'verify_SSL', 1)
ABresult
1X1
011
000
26 10 $ua->agent($ua_string);
27 10 my $api_url = 'https://api.opencagedata.com/geocode/v1/json';
28
29 10 ▶ if (defined($params{http}) && $params{http} eq '1') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"http"} and $params{"http"} eq 1) 0 10 10
Condition33%1/3
defined $params{"http"} and $params{"http"} eq 1
!ll&&!rl&&r
1000
MC/DC0%0/2
defined $params{"http"} and $params{"http"} eq 1
defined $params{"http"} $params{"http"} eq 1
Truth table33%1/3
A and B
A= defined $params{"http"}B= $params{"http"} eq 1
ABresult
0X0
100
111
30 # verify_SSL has no effect when the protocol isn't https
31 0 $api_url =~ s|^https://|http://|;
32 }
33 my $self = {
34 version => $version,
35 api_key => $params{api_key},
36 10 ua => $ua,
37 json => JSON::MaybeXS->new(utf8 => 1),
38 url => URI->new($api_url),
39 };
40  
41 10 return bless $self, $class;
42 }
43  
44 5 ▶sub ua {
Subroutine ua: called CC=2 SCAR=6.9
Pod documented
45 5 my $self = shift;
46 5 my $ua = shift;
47 5 ▶ if (defined($ua)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ua) 1 4 5
48 1 $ua->agent($ua_string);
49 1 $self->{ua} = $ua;
50 }
51 5 return $self->{ua};
52 }
53  
54 # see list: https://opencagedata.com/api#forward-opt
55 12 my %valid_params = (
56 abbrv => 1,
57 address_only => 1,
58 add_request => 1,
59 bounds => 1,
60 countrycode => 1,
61 format => 0,
62 jsonp => 0,
63 key => 0,
64 language => 1,
65 limit => 1,
66 min_confidence => 1,
67 no_annotations => 1,
68 no_dedupe => 1,
69 no_record => 1,
70 q => 1,
71 pretty => 1, # makes no actual difference
72 proximity => 1,
73 roadinfo => 1,
74 );
75  
76 7 ▶sub geocode {
Subroutine geocode: called CC=17 SCAR=33.2
Pod documented
77 7 my ($self, %params) = @_;
78  
79 7 ▶ if (defined($params{location})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{'location'}) { } 7 0 7
80 7 $params{q} = delete $params{location};
81 } else {
82 0 carp "location is a required parameter for geocode()";
83 0 return;
84 }
85  
86 7 foreach my $k (keys %params) {
87 11 ▶ if (!exists($valid_params{$k})) {
Branch: 2 branches
BranchTrueFalseTotal
if (not exists $valid_params{$k}) { } 1 10 11
elsif ($valid_params{$k} == 0) { } 3 7 10
88 1 carp "Unknown geocode parameter: " . ($k =~ s/[[:cntrl:]]//gr);
89 1 delete $params{$k};
90 } elsif ($valid_params{$k} == 0) { # is a real parameter but we dont support it
91 3 carp "Unsupported geocode parameter: $k";
92 3 delete $params{$k};
93 }
94 }
95  
96 7 $params{key} = $self->{api_key};
97
98 # sort the params for better cachability
99 7 my @final_params;
100 7 foreach my $k (sort keys %params){
101 14 push(@final_params, $k => $params{$k})
102
103 }
104 7 my $URL = $self->{url}->clone();
105 7 $URL->query_form(\@final_params);
106 7 my $response = $self->{ua}->get($URL);
107  
108 # Support HTTP::Tiny and LWP:: CPAN packages (and subclasses of either).
109 7 ▶ my $is_lwp = blessed $response && $response->isa('HTTP::Response');
Condition66%2/3
builtin::blessed($response) && $response->isa('HTTP::Response')
!ll&&!rl&&r
601
MC/DC50%1/2
builtin::blessed($response) && $response->isa('HTTP::Response')
builtin::blessed($response) $response->isa('HTTP::Response')
Truth table66%2/3
A && B
A= builtin::blessed($response)B= $response->isa('HTTP::Response')
ABresult
0X0
100
111
110 7 ▶ my $content = $is_lwp ? $response->decoded_content() : $response->{content};
Branch: 1 branch
BranchTrueFalseTotal
$is_lwp ? : 1 6 7
111 7 ▶ my $is_success = $is_lwp ? $response->is_success() : $response->{success};
Branch: 1 branch
BranchTrueFalseTotal
$is_lwp ? : 1 6 7
112  
113 7 ▶ if (my $error = _transport_error($response, $is_lwp)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = _transport_error($response, $is_lwp)) 0 7 7
114 0 $error =~ s/[[:cntrl:]]//g;
115 0 carp "request to '" . $self->_sanitized_url($URL) . "' failed: $error";
116 0 return;
117 }
118  
119 7 my $rh_content;
120 7 my $ok = eval { $rh_content = $self->{json}->decode($content); 1 };
121 7 ▶ if (!$ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 2 5 7
122 2 ▶ my $err = $@ || 'unknown error';
Condition50%1/2
$@ || 'unknown error'
l!l
20
MC/DC0%0/1
$@ || 'unknown error'
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
123 2 carp "failed to decode response from '" . $self->_sanitized_url($URL) . "': $err";
124 2 return;
125 }
126  
127 5 ▶ if (ref($rh_content) ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rh_content ne "HASH") 0 5 5
128 0 carp "unexpected response from '" . $self->_sanitized_url($URL) . "': not a JSON object";
129 0 return;
130 }
131  
132 5 ▶ if (!$is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_success) 0 5 5
133 0 ▶ my $status = ref($rh_content->{status}) eq 'HASH' ? $rh_content->{status} : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $rh_content->{'status'} eq 'HASH' ? : 0 0 0
134 # Cleanup server-supplied strings, e.g. new-lines
135 0 ▶ my $code = ($status->{code} // '?') =~ s/[[:cntrl:]]//gr;
Condition0%0/2
$status->{"code"} // "?"
l!l
00
MC/DC0%0/1
$status->{"code"} // "?"
$status->{"code"}
Truth table0%0/2
A
A= $status->{"code"}
Aresult
11
00
136 0 ▶ my $msg = ($status->{message} // 'unknown error') =~ s/[[:cntrl:]]//gr;
Condition0%0/2
$status->{"message"} // "unknown error"
l!l
00
MC/DC0%0/1
$status->{"message"} // "unknown error"
$status->{"message"}
Truth table0%0/2
A
A= $status->{"message"}
Aresult
11
00
137 0 carp "response when requesting '" . $self->_sanitized_url($URL) . "': $code, $msg";
138 0 return;
139 }
140 5 return $rh_content;
141 }
142  
143 # mask the API key in a URL before logging, so it doesn't leak into stderr
144 2 ▶sub _sanitized_url {
Subroutine _sanitized_url: called CC=1 SCAR=0.0
145 2 my ($self, $url) = @_;
146 2 my $masked = $url->clone;
147 2 $masked->query_param(key => substr($self->{api_key}, 0, 6) . '...');
148 2 return $masked->as_string;
149 }
150  
151 # the HTTP client's error message if no response arrived (connection, TLS, timeout)
152 7 ▶sub _transport_error {
Subroutine _transport_error: called CC=6 SCAR=19.6
153 7 my ($response, $is_lwp) = @_;
154 7 ▶ if ($is_lwp) {
Branch: 2 branches
BranchTrueFalseTotal
if ($is_lwp) { } 1 6 7
elsif (($response->{'status'} // 0) == 599) { } 0 6 6
155 1 ▶ my $warning = $response->header('Client-Warning') // '';
Condition50%1/2
$response->header("Client-Warning") // ""
l!l
01
MC/DC0%0/1
$response->header("Client-Warning") // ""
$response->header("Client-Warning")
Truth table50%1/2
A
A= $response->header("Client-Warning")
Aresult
11
00
156 1 ▶ return $response->status_line if $warning eq 'Internal response';
Branch: 1 branch
BranchTrueFalseTotal
if $warning eq "Internal response" 0 1 1
157 }
158 ▶ elsif (($response->{status} // 0) == 599) {
Condition50%1/2
$response->{"status"} // 0
l!l
06
MC/DC0%0/1
$response->{"status"} // 0
$response->{"status"}
Truth table50%1/2
A
A= $response->{"status"}
Aresult
11
00
159 0 return $response->{content};
160 }
161 7 return;
162 }
163  
164 3 ▶sub reverse_geocode {
Subroutine reverse_geocode: called CC=3 SCAR=11.3
Pod documented
165 3 my ($self, %params) = @_;
166  
167 3 foreach my $k (qw(lat lng)) {
168 4 ▶ if (!defined($params{$k})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $params{$k}) 3 1 4
169 3 carp "$k is a required parameter";
170 3 return;
171 }
172 }
173  
174 0 $params{location} = join(',', delete @params{'lat', 'lng'});
175 0 return $self->geocode(%params);
176 }
177  
178 12 1;
179  
180 __END__
↑ Summary