| 1 | package Geo::Coder::OpenCage; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Geocode coordinates and addresses with the OpenCage Geocoding API | ||||||||||||||||||||||||||||
| 3 | 11 | $Geo::Coder::OpenCage::VERSION = '0.42'; | |||||||||||||||||||||||||||
| 4 | 11 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 11 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 11 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 11 | ▶ | use HTTP::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 11 | ▶ | use JSON::MaybeXS; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 11 | ▶ | use Scalar::Util 'blessed'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 11 | ▶ | use URI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 11 | ▶ | my $version = our $VERSION || 'dev'; | ||||||||||||||||||||||||||
|
Condition50%1/2
our $VERSION || 'dev'
MC/DC0%0/1
our $VERSION || 'dev'
our $VERSION
Truth table50%1/2
A
A= our $VERSION
| |||||||||||||||||||||||||||||
| 14 | 11 | my $ua_string = "Geo::Coder::OpenCage/$version"; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | sub new { | ||||||||||||||||||||||||||||
| 17 | 8 | ▶ | my ($class, %params) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.8
Pod
documented
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 8 | ▶ | if (!$params{api_key}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 20 | 1 | croak "api_key is a required parameter for new()"; | |||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 7 | ▶ | my $ua = $params{ua} || HTTP::Tiny->new(timeout => 10); | ||||||||||||||||||||||||||
|
Condition66%2/3
$params{'ua'} || 'HTTP::Tiny'->new('timeout', 10)
MC/DC0%0/2
$params{'ua'} || 'HTTP::Tiny'->new('timeout', 10)
$params{'ua'}
'HTTP::Tiny'->new('timeout', 10)
Truth table66%2/3
A || B
A= $params{'ua'}B= 'HTTP::Tiny'->new('timeout', 10)
| |||||||||||||||||||||||||||||
| 24 | 7 | $ua->agent($ua_string); | |||||||||||||||||||||||||||
| 25 | 7 | my $api_url = 'https://api.opencagedata.com/geocode/v1/json'; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 7 | ▶ | if (defined($params{http}) && $params{http} eq '1') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $params{"http"} and $params{"http"} eq 1
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
| |||||||||||||||||||||||||||||
| 28 | 0 | $api_url =~ s|^https://|http://|; | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | my $self = { | ||||||||||||||||||||||||||||
| 31 | version => $version, | ||||||||||||||||||||||||||||
| 32 | api_key => $params{api_key}, | ||||||||||||||||||||||||||||
| 33 | 7 | ua => $ua, | |||||||||||||||||||||||||||
| 34 | json => JSON::MaybeXS->new(utf8 => 1), | ||||||||||||||||||||||||||||
| 35 | url => URI->new($api_url), | ||||||||||||||||||||||||||||
| 36 | }; | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 7 | return bless $self, $class; | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | sub ua { | ||||||||||||||||||||||||||||
| 42 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
ua: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 43 | 2 | my $ua = shift; | |||||||||||||||||||||||||||
| 44 | 2 | ▶ | if (defined($ua)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 1 | $ua->agent($ua_string); | |||||||||||||||||||||||||||
| 46 | 1 | $self->{ua} = $ua; | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | 2 | return $self->{ua}; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | # see list: https://opencagedata.com/api#forward-opt | ||||||||||||||||||||||||||||
| 52 | 11 | my %valid_params = ( | |||||||||||||||||||||||||||
| 53 | abbrv => 1, | ||||||||||||||||||||||||||||
| 54 | address_only => 1, | ||||||||||||||||||||||||||||
| 55 | add_request => 1, | ||||||||||||||||||||||||||||
| 56 | bounds => 1, | ||||||||||||||||||||||||||||
| 57 | countrycode => 1, | ||||||||||||||||||||||||||||
| 58 | format => 0, | ||||||||||||||||||||||||||||
| 59 | jsonp => 0, | ||||||||||||||||||||||||||||
| 60 | key => 0, | ||||||||||||||||||||||||||||
| 61 | language => 1, | ||||||||||||||||||||||||||||
| 62 | limit => 1, | ||||||||||||||||||||||||||||
| 63 | min_confidence => 1, | ||||||||||||||||||||||||||||
| 64 | no_annotations => 1, | ||||||||||||||||||||||||||||
| 65 | no_dedupe => 1, | ||||||||||||||||||||||||||||
| 66 | no_record => 1, | ||||||||||||||||||||||||||||
| 67 | q => 1, | ||||||||||||||||||||||||||||
| 68 | pretty => 1, # makes no actual difference | ||||||||||||||||||||||||||||
| 69 | proximity => 1, | ||||||||||||||||||||||||||||
| 70 | roadinfo => 1, | ||||||||||||||||||||||||||||
| 71 | ); | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | sub geocode { | ||||||||||||||||||||||||||||
| 74 | 7 | ▶ | my ($self, %params) = @_; | ||||||||||||||||||||||||||
|
Subroutine
geocode: called CC=15 SCAR=29.9
Pod
documented
| |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 7 | ▶ | if (defined($params{location})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 7 | $params{q} = delete $params{location}; | |||||||||||||||||||||||||||
| 78 | } else { | ||||||||||||||||||||||||||||
| 79 | 0 | carp "location is a required parameter for geocode()"; | |||||||||||||||||||||||||||
| 80 | 0 | return; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 7 | foreach my $k (keys %params) { | |||||||||||||||||||||||||||
| 84 | 11 | ▶ | if (!exists($valid_params{$k})) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 85 | 1 | carp "Unknown geocode parameter: $k"; | |||||||||||||||||||||||||||
| 86 | 1 | delete $params{$k}; | |||||||||||||||||||||||||||
| 87 | } elsif ($valid_params{$k} == 0) { # is a real parameter but we dont support it | ||||||||||||||||||||||||||||
| 88 | 3 | carp "Unsupported geocode parameter: $k"; | |||||||||||||||||||||||||||
| 89 | 3 | delete $params{$k}; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 7 | $params{key} = $self->{api_key}; | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | # sort the params for better cachability | ||||||||||||||||||||||||||||
| 96 | 7 | my @final_params; | |||||||||||||||||||||||||||
| 97 | 7 | foreach my $k (sort keys %params){ | |||||||||||||||||||||||||||
| 98 | 14 | push(@final_params, $k => $params{$k}) | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | 7 | my $URL = $self->{url}->clone(); | |||||||||||||||||||||||||||
| 102 | 7 | $URL->query_form(\@final_params); | |||||||||||||||||||||||||||
| 103 | 7 | my $response = $self->{ua}->get($URL); | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | # Support HTTP::Tiny and LWP:: CPAN packages (and subclasses of either). | ||||||||||||||||||||||||||||
| 106 | 7 | ▶ | my $is_lwp = blessed $response && $response->isa('HTTP::Response'); | ||||||||||||||||||||||||||
|
Condition66%2/3
builtin::blessed($response) && $response->isa('HTTP::Response')
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')
| |||||||||||||||||||||||||||||
| 107 | 7 | ▶ | my $content = $is_lwp ? $response->decoded_content() : $response->{content}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 7 | ▶ | my $is_success = $is_lwp ? $response->is_success() : $response->{success}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 7 | my $rh_content; | |||||||||||||||||||||||||||
| 111 | 7 | my $ok = eval { $rh_content = $self->{json}->decode($content); 1 }; | |||||||||||||||||||||||||||
| 112 | 7 | ▶ | if (!$ok) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 2 | ▶ | my $err = $@ || 'unknown error'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$@ || 'unknown error'
MC/DC0%0/1
$@ || 'unknown error'
$@
Truth table50%1/2
A
A= $@
| |||||||||||||||||||||||||||||
| 114 | 2 | carp "failed to decode response from '" . $self->_sanitized_url($URL) . "': $err"; | |||||||||||||||||||||||||||
| 115 | 2 | return; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 5 | ▶ | if (!$is_success) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 0 | ▶ | my $status = $rh_content->{status} || {}; | ||||||||||||||||||||||||||
|
Condition0%0/2
$rh_content->{'status'} || {}
MC/DC0%0/1
$rh_content->{'status'} || {}
$rh_content->{'status'}
Truth table0%0/2
A
A= $rh_content->{'status'}
| |||||||||||||||||||||||||||||
| 120 | # Cleanup server-supplied strings, e.g. new-lines | ||||||||||||||||||||||||||||
| 121 | 0 | ▶ | my $code = ($status->{code} // '?') =~ s/[[:cntrl:]]//gr; | ||||||||||||||||||||||||||
|
Condition0%0/2
$status->{"code"} // "?"
MC/DC0%0/1
$status->{"code"} // "?"
$status->{"code"}
Truth table0%0/2
A
A= $status->{"code"}
| |||||||||||||||||||||||||||||
| 122 | 0 | ▶ | my $msg = ($status->{message} // 'unknown error') =~ s/[[:cntrl:]]//gr; | ||||||||||||||||||||||||||
|
Condition0%0/2
$status->{"message"} // "unknown error"
MC/DC0%0/1
$status->{"message"} // "unknown error"
$status->{"message"}
Truth table0%0/2
A
A= $status->{"message"}
| |||||||||||||||||||||||||||||
| 123 | 0 | carp "response when requesting '" . $self->_sanitized_url($URL) . "': $code, $msg"; | |||||||||||||||||||||||||||
| 124 | 0 | return; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | 5 | return $rh_content; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | # mask the API key in a URL before logging, so it doesn't leak into stderr | ||||||||||||||||||||||||||||
| 130 | sub _sanitized_url { | ||||||||||||||||||||||||||||
| 131 | 2 | ▶ | my ($self, $url) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_sanitized_url: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 132 | 2 | my $masked = substr($self->{api_key}, 0, 6) . '...'; | |||||||||||||||||||||||||||
| 133 | 2 | (my $str = "$url") =~ s/\Q$self->{api_key}\E/$masked/g; | |||||||||||||||||||||||||||
| 134 | 2 | return $str; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | sub reverse_geocode { | ||||||||||||||||||||||||||||
| 138 | 3 | ▶ | my ($self, %params) = @_; | ||||||||||||||||||||||||||
|
Subroutine
reverse_geocode: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 3 | foreach my $k (qw(lat lng)) { | |||||||||||||||||||||||||||
| 141 | 4 | ▶ | if (!defined($params{$k})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | 3 | carp "$k is a required parameter"; | |||||||||||||||||||||||||||
| 143 | 3 | return; | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 0 | $params{location} = join(',', delete @params{'lat', 'lng'}); | |||||||||||||||||||||||||||
| 148 | 0 | return $self->geocode(%params); | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 11 | 1; | |||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | __END__ | ||||||||||||||||||||||||||||