| 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/'
MC/DC0%0/1
$params{'api_endpoint'} || 'https://api.what3words.com/v3/'
$params{'api_endpoint'}
Truth table50%1/2
A
A= $params{'api_endpoint'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 10 | ▶ | $self->{key} = $params{key} || die "API key not set"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$params{'key'} || die('API key not set')
MC/DC100%1/1
$params{'key'} || die('API key not set')
$params{'key'}
Truth table100%2/2
A
A= $params{'key'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 9 | ▶ | my $version = $Geo::What3Words::VERSION || ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$Geo::What3Words::VERSION || ''
MC/DC0%0/1
$Geo::What3Words::VERSION || ''
$Geo::What3Words::VERSION
Truth table50%1/2
A
A= $Geo::What3Words::VERSION
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%4/6
$res and is_hashref($res) and exists $res->{"coordinates"}
$res and is_hashref($res)
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%4/6
$res and is_hashref($res) and exists $res->{"words"}
$res and is_hashref($res)
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 9 | ▶ | return 1 if ($words =~ m/^(\p{Lower}+)\.(\p{Lower}+)\.(\p{Lower}+)$/); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
defined $words and $words =~ /^[^.\s]+\.[^.\s]+\.[^.\s]+$/
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]+$/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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'}
MC/DC50%1/2
shift() || $self->{'language'}
shift()
$self->{'language'}
Truth table66%2/3
A || B
A= shift()B= $self->{'language'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 21 | ▶ | if (!_valid_position($position)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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() || {}
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | 15 | my ($lat, $lng) = ($1, $2); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | 15 | ▶ | return 0 if ($lat < -90 || $lat > 90 || $lng < -180 || $lng > 180); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition88%8/9
$lat < -90 or $lat > 90 or $lng < -180 or $lng > 180
$lat < -90 or $lat > 90 or $lng < -180
$lat < -90 or $lat > 90
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
is_hashref($body) and is_hashref($body->{"error"})
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"})
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | 4 | ▶ | if ($url =~ m{^[a-z][a-z0-9+.-]*://(?:[^@/?#]*@)?(?:\[([^\]]+)\]|([^:/?#]+))}i) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | 3 | ▶ | return defined($1) ? $1 : $2; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | 55 | ▶ | if (is_coderef($self->{logging})) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||