blib/lib/Geo/What3Words.pm

filter: Statementstmt 91.6% 120 / 131 Branchbran 85.7% 36 / 42 Conditioncond 72.7% 32 / 44 MC/DCmcdc 50.0% 12 / 24 Subroutinesub 91.3% 21 / 23 Podpod 100% 10 / 10 totaltotal 84.3% 231 / 274 CC 39 SCAR 14.0
CC
39
Cov
87%
_valid_position
7.0
_query_remote_api
6.0
ping
6.0
CRAP 4.1
↑ Summary
1 # ABSTRACT: turn WGS84 coordinates into three word addresses and vice-versa using what3words.com HTTPS API
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  
6 package Geo::What3Words;
7 1 $Geo::What3Words::VERSION = '3.0.6';
8 1 ▶use Cpanel::JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Encode qw( decode_utf8 );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Ref::Util qw( is_hashref is_coderef );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 my $JSONXS = Cpanel::JSON::XS->new->utf8->allow_nonref(1);
15  
16  
17  
18  
19 10 ▶sub new {
Subroutine new: called CC=5 SCAR=16.6
Pod documented
20 10 my ($class, %params) = @_;
21  
22 10 my $self = {};
23 10 ▶ $self->{api_endpoint} = $params{api_endpoint} || 'https://api.what3words.com/v3/';
Condition50%1/2
$params{'api_endpoint'} || 'https://api.what3words.com/v3/'
l!l
010
MC/DC0%0/1
$params{'api_endpoint'} || 'https://api.what3words.com/v3/'
$params{'api_endpoint'}
Truth table50%1/2
A
A= $params{'api_endpoint'}
Aresult
11
00
24 10 ▶ $self->{key} = $params{key} || die "API key not set";
Condition100%2/2
$params{'key'} || die('API key not set')
l!l
91
MC/DC100%1/1
$params{'key'} || die('API key not set')
$params{'key'}
Truth table100%2/2
A
A= $params{'key'}
Aresult
11
00
25 9 $self->{language} = $params{language};
26 9 $self->{logging} = $params{logging};
27  
28 ## _ua is used for testing. But could also be used to
29 ## set proxies or such
30 9 ▶ $self->{ua} = $params{ua} || HTTP::Tiny->new;
Condition33%1/3
$params{'ua'} || 'HTTP::Tiny'->new
l!l&&r!l&&!r
900
MC/DC0%0/2
$params{'ua'} || 'HTTP::Tiny'->new
$params{'ua'} 'HTTP::Tiny'->new
Truth table33%1/3
A || B
A= $params{'ua'}B= 'HTTP::Tiny'->new
ABresult
1X1
011
000
31  
32 9 ▶ my $version = $Geo::What3Words::VERSION || '';
Condition50%1/2
$Geo::What3Words::VERSION || ''
l!l
90
MC/DC0%0/1
$Geo::What3Words::VERSION || ''
$Geo::What3Words::VERSION
Truth table50%1/2
A
A= $Geo::What3Words::VERSION
Aresult
11
00
33 9 $self->{ua}->agent("Perl Geo::What3Words $version");
34  
35 9 return bless($self, $class);
36 }
37  
38  
39 0 ▶sub ping {
Subroutine ping: not called CC=2 SCAR=17.9
Pod documented
40 0 my $self = shift;
41  
42 0 my $host = _host_from_url($self->{api_endpoint});
43  
44 0 $self->_log("pinging $host...");
45  
46 ## loaded here so the rest of the module works without them,
47 ## Net::Ping loads Net::Ping::External itself
48 0 require Net::Ping;
49 0 my $netping = Net::Ping->new('external');
50 0 my $res = $netping->ping($host);
51  
52 0 ▶ $self->_log($res ? 'available' : 'unavailable');
Branch: 1 branch
BranchTrueFalseTotal
$res ? : 0 0 0
53  
54 0 return $res;
55 }
56  
57  
58 2 ▶sub words2pos {
Subroutine words2pos: called CC=4 SCAR=14.0
Pod documented
59 2 my ($self, @params) = @_;
60  
61 2 my $res = $self->words_to_position(@params);
62 2 ▶ if ($res && is_hashref($res) && exists($res->{coordinates})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res and is_hashref($res) and exists $res->{"coordinates"}) 1 1 2
Condition66%4/6
$res and is_hashref($res) and exists $res->{"coordinates"}
!ll&&!rl&&r
101
$res and is_hashref($res)
!ll&&!rl&&r
101
MC/DC33%1/3
$res and is_hashref($res) and exists $res->{"coordinates"}
$res is_hashref($res) exists $res->{"coordinates"}
Truth table50%2/4
A and B and C
A= $resB= is_hashref($res)C= exists $res->{"coordinates"}
ABCresult
0XX0
10X0
1100
1111
63 1 return $res->{coordinates}->{lat} . ',' . $res->{coordinates}->{lng};
64 }
65 1 return;
66 }
67  
68  
69  
70 14 ▶sub pos2words {
Subroutine pos2words: called CC=4 SCAR=14.0
Pod documented
71 14 my ($self, @params) = @_;
72 14 my $res = $self->position_to_words(@params);
73 14 ▶ if ($res && is_hashref($res) && exists($res->{words})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res and is_hashref($res) and exists $res->{"words"}) 2 12 14
Condition66%4/6
$res and is_hashref($res) and exists $res->{"words"}
!ll&&!rl&&r
1202
$res and is_hashref($res)
!ll&&!rl&&r
1202
MC/DC33%1/3
$res and is_hashref($res) and exists $res->{"words"}
$res is_hashref($res) exists $res->{"words"}
Truth table50%2/4
A and B and C
A= $resB= is_hashref($res)C= exists $res->{"words"}
ABCresult
0XX0
10X0
1100
1111
74 2 return $res->{words};
75 }
76 12 return;
77 }
78  
79  
80 11 ▶sub valid_words_format {
Subroutine valid_words_format: called CC=3 SCAR=11.0
Pod documented
81 11 my $self = shift;
82 11 my $words = shift;
83  
84 ## Translating the PHP regular expression w3w uses in their
85 ## documentation
86 ## http://perldoc.perl.org/perlunicode.html#Unicode-Character-Properties
87 ## http://php.net/manual/en/reference.pcre.pattern.differences.php
88 11 ▶ return 0 unless $words;
Branch: 1 branch
BranchTrueFalseTotal
unless $words 2 9 11
89 9 ▶ return 1 if ($words =~ m/^(\p{Lower}+)\.(\p{Lower}+)\.(\p{Lower}+)$/);
Branch: 1 branch
BranchTrueFalseTotal
if $words =~ /^(\p{Lower}+)\.(\p{Lower}+)\.(\p{Lower}+)$/ 5 4 9
90 4 return 0;
91 }
92  
93  
94 11 ▶sub words_to_position {
Subroutine words_to_position: called CC=3 SCAR=11.0
Pod documented
95 11 my $self = shift;
96 11 my $words = shift;
97  
98 ## save a round trip for input the API would reject anyway. More
99 ## lenient than valid_words_format so we never reject something
100 ## the API might accept
101 11 ▶ if (!defined($words) || $words !~ m/^[^.\s]+\.[^.\s]+\.[^.\s]+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $words and $words =~ /^[^.\s]+\.[^.\s]+\.[^.\s]+$/) 6 5 11
Condition100%3/3
defined $words and $words =~ /^[^.\s]+\.[^.\s]+\.[^.\s]+$/
!ll&&!rl&&r
155
MC/DC100%2/2
defined $words and $words =~ /^[^.\s]+\.[^.\s]+\.[^.\s]+$/
defined $words $words =~ /^[^.\s]+\.[^.\s]+\.[^.\s]+$/
Truth table100%3/3
A and B
A= defined $wordsB= $words =~ /^[^.\s]+\.[^.\s]+\.[^.\s]+$/
ABresult
0X0
100
111
102 6 return $self->_invalid_input('BadWords', '3 word address', $words);
103 }
104  
105 5 return $self->_query_remote_api('convert-to-coordinates', {words => $words});
106  
107 }
108  
109  
110 21 ▶sub position_to_words {
Subroutine position_to_words: called CC=3 SCAR=11.0
Pod documented
111 21 my $self = shift;
112 21 my $position = shift;
113 21 ▶ my $language = shift || $self->{language};
Condition66%2/3
shift() || $self->{'language'}
l!l&&r!l&&!r
2019
MC/DC50%1/2
shift() || $self->{'language'}
shift() $self->{'language'}
Truth table66%2/3
A || B
A= shift()B= $self->{'language'}
ABresult
1X1
011
000
114  
115 21 ▶ if (!_valid_position($position)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_valid_position($position)) 9 12 21
116 9 return $self->_invalid_input('BadCoordinates', 'coordinates', $position);
117 }
118  
119 # https://developer.what3words.com/public-api/docs#convert-to-3wa
120 12 return $self->_query_remote_api(
121 'convert-to-3wa',
122 { coordinates => $position,
123 language => $language
124 }
125 );
126 }
127  
128  
129 3 ▶sub get_languages {
Subroutine get_languages: called CC=1 SCAR=0.0
Pod documented
130 3 my $self = shift;
131 3 return $self->_query_remote_api('available-languages');
132 }
133  
134  
135 20 ▶sub error {
Subroutine error: called CC=1 SCAR=0.0
Pod documented
136 20 my $self = shift;
137 20 return $self->{error};
138 }
139  
140  
141 0 ▶sub oneword_available {
Subroutine oneword_available: not called CC=1 SCAR=6.9
Pod documented
142 0 warn 'deprecated method: oneword_available';
143 0 return;
144 }
145  
146 20 ▶sub _query_remote_api {
Subroutine _query_remote_api: called CC=6 SCAR=17.9
147 20 my $self = shift;
148 20 my $method_name = shift;
149 20 ▶ my $rh_params = shift || {};
Condition100%2/2
shift() || {}
l!l
173
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
150  
151 20 $self->{error} = undef;
152  
153 my $rh_fields = {
154 #a => 1,
155 key => $self->{key},
156 20 format => 'json',
157 %$rh_params
158 };
159  
160 20 foreach my $key (keys %$rh_fields) {
161 69 ▶ delete $rh_fields->{$key} if (!defined($rh_fields->{$key}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rh_fields->{$key} 10 59 69
162 }
163  
164 ## class method, $self->{ua} might not be an HTTP::Tiny
165 20 my $url = $self->{api_endpoint} . $method_name . '?' . HTTP::Tiny->www_form_urlencode($rh_fields);
166  
167 ## never show the API key in warnings or logs
168 20 (my $safe_url = $url) =~ s/([?&]key=)[^&]*/${1}XXXX/;
169  
170 20 $self->_log("GET $safe_url");
171 20 my $response = $self->{ua}->get($url);
172  
173 20 ▶ if (!$response->{success}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->{"success"}) 6 14 20
174 6 warn "got failed response from $safe_url: " . $response->{status};
175 6 $self->_log("got failed response from $safe_url: " . $response->{status});
176 6 $self->{error} = _error_from_response($response);
177 6 return;
178 }
179  
180 ## the JSON decoder expects UTF-8 bytes, only decode for logging
181 14 my $json = $response->{content};
182 14 ▶ $self->_log(decode_utf8($json)) if $self->{logging};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"logging"} 14 0 14
183  
184 14 return $JSONXS->decode($json);
185 }
186  
187 ## 'lat,lng' with lat in -90..90 and lng in -180..180
188 21 ▶sub _valid_position {
Subroutine _valid_position: called CC=7 SCAR=19.5
189 21 my $position = shift;
190 21 ▶ return 0 unless defined $position;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $position 1 20 21
191  
192 20 my $num = qr/[-+]?(?:\d+(?:\.\d*)?|\.\d+)/;
193 20 ▶ return 0 unless ($position =~ m/^\s*($num)\s*,\s*($num)\s*$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $position =~ /^\s*($num)\s*,\s*($num)\s*$/ 5 15 20
194 15 my ($lat, $lng) = ($1, $2);
195 15 ▶ return 0 if ($lat < -90 || $lat > 90 || $lng < -180 || $lng > 180);
Branch: 1 branch
BranchTrueFalseTotal
if $lat < -90 or $lat > 90 or $lng < -180 or $lng > 180 3 12 15
Condition88%8/9
$lat < -90 or $lat > 90 or $lng < -180 or $lng > 180
l!l&&r!l&&!r
2112
$lat < -90 or $lat > 90 or $lng < -180
l!l&&r!l&&!r
2013
$lat < -90 or $lat > 90
l!l&&r!l&&!r
1113
MC/DC75%3/4
$lat < -90 or $lat > 90 or $lng < -180 or $lng > 180
$lat < -90 $lat > 90 $lng < -180 $lng > 180
Truth table80%4/5
A or B or C or D
A= $lat < -90B= $lat > 90C= $lng < -180D= $lng > 180
ABCDresult
1XXX1
01XX1
001X1
00011
00000
196 12 return 1;
197 }
198  
199 ## the API sends errors as {"error":{"code":...,"message":...}}, but
200 ## e.g. connection failures (status 599) come from HTTP::Tiny as text
201 6 ▶sub _error_from_response {
Subroutine _error_from_response: called CC=5 SCAR=16.2
202 6 my $response = shift;
203  
204 6 my $error = {status => $response->{status}, code => undef, message => $response->{reason}};
205  
206 6 my $body = eval { $JSONXS->decode($response->{content}) };
207 6 ▶ if (is_hashref($body) && is_hashref($body->{error})) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_hashref($body) and is_hashref($body->{'error'})) { } 4 2 6
elsif ($response->{'status'} == 599 and defined $response->{'content'}) { } 1 2 3
Condition66%2/3
is_hashref($body) and is_hashref($body->{"error"})
!ll&&!rl&&r
204
MC/DC50%1/2
is_hashref($body) and is_hashref($body->{"error"})
is_hashref($body) is_hashref($body->{"error"})
Truth table66%2/3
A and B
A= is_hashref($body)B= is_hashref($body->{"error"})
ABresult
0X0
100
111
208 4 $error->{code} = $body->{error}->{code};
209 4 $error->{message} = $body->{error}->{message};
210 ▶ } elsif ($response->{status} == 599 && defined($response->{content})) {
Condition66%2/3
$response->{"status"} == 599 and defined $response->{"content"}
!ll&&!rl&&r
101
MC/DC50%1/2
$response->{"status"} == 599 and defined $response->{"content"}
$response->{"status"} == 599 defined $response->{"content"}
Truth table66%2/3
A and B
A= $response->{"status"} == 599B= defined $response->{"content"}
ABresult
0X0
100
111
211 1 $error->{message} = $response->{content};
212 }
213 6 return $error;
214 }
215  
216 15 ▶sub _invalid_input {
Subroutine _invalid_input: called CC=2 SCAR=6.9
217 15 my $self = shift;
218 15 my $code = shift;
219 15 my $what = shift;
220 15 my $input = shift;
221  
222 15 ▶ my $msg = "invalid $what: " . (defined($input) ? "'$input'" : 'undef') . ', not calling API';
Branch: 1 branch
BranchTrueFalseTotal
defined $input ? : 13 2 15
223 15 $self->{error} = {status => undef, code => $code, message => $msg};
224 15 warn $msg;
225 15 $self->_log($msg);
226 15 return;
227 }
228  
229 ## http://example.com/some/path => example.com
230 ## also works with IP addresses, [::1] => ::1
231 4 ▶sub _host_from_url {
Subroutine _host_from_url: called CC=4 SCAR=13.9
232 4 my $url = shift;
233 4 ▶ return unless defined $url;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $url 0 4 4
234 4 ▶ if ($url =~ m{^[a-z][a-z0-9+.-]*://(?:[^@/?#]*@)?(?:\[([^\]]+)\]|([^:/?#]+))}i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url =~ m(^[a-z][a-z0-9+.-]*://(?:[^\@/?#]*@)?(?:\[([^\]]+)\]|([^:/?#]+)))i) 3 1 4
235 3 ▶ return defined($1) ? $1 : $2;
Branch: 1 branch
BranchTrueFalseTotal
defined $1 ? : 1 2 3
236 }
237 1 return;
238 }
239  
240 55 ▶sub _log {
Subroutine _log: called CC=3 SCAR=11.4
241 55 my $self = shift;
242 55 my $message = shift;
243 55 ▶ return unless $self->{logging};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"logging"} 0 55 55
244  
245 55 ▶ if (is_coderef($self->{logging})) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_coderef($self->{'logging'})) { } 55 0 55
246 55 my $lc = $self->{logging};
247 55 &$lc("Geo::What3Words -- " . $message);
248 } else {
249 0 print "Geo::What3Words -- " . $message . "\n";
250 }
251 55 return;
252 }
253  
254  
255 1 1;
256  
257 __END__
↑ Summary