blib/lib/Geo/Coder/OpenCage.pm untested

filter: Statementstmt 0.0% 0 / 89 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 19 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 147 CC 12 SCAR 33.4
CC
12
Cov
0%
geocode
72.0
new
12.0
ua
6.0
CRAP 28.3
1 package Geo::Coder::OpenCage;
2 # ABSTRACT: Geocode coordinates and addresses with the OpenCage Geocoding API
3 $Geo::Coder::OpenCage::VERSION = '0.42';
4 use strict;
5 use warnings;
6  
7 use Carp;
8 use HTTP::Tiny;
9 use JSON::MaybeXS;
10 use Scalar::Util 'blessed';
11 use URI;
12  
13 my $version = our $VERSION || 'dev';
14 my $ua_string = "Geo::Coder::OpenCage/$version";
15  
16 sub new {
17 my ($class, %params) = @_;
18  
19 if (!$params{api_key}) {
20 croak "api_key is a required parameter for new()";
21 }
22  
23 my $ua = $params{ua} || HTTP::Tiny->new(timeout => 10);
24 $ua->agent($ua_string);
25 my $api_url = 'https://api.opencagedata.com/geocode/v1/json';
26
27 if (defined($params{http}) && $params{http} eq '1') {
28 $api_url =~ s|^https://|http://|;
29 }
30 my $self = {
31 version => $version,
32 api_key => $params{api_key},
33 ua => $ua,
34 json => JSON::MaybeXS->new(utf8 => 1),
35 url => URI->new($api_url),
36 };
37  
38 return bless $self, $class;
39 }
40  
41 sub ua {
42 my $self = shift;
43 my $ua = shift;
44 if (defined($ua)) {
45 $ua->agent($ua_string);
46 $self->{ua} = $ua;
47 }
48 return $self->{ua};
49 }
50  
51 # see list: https://opencagedata.com/api#forward-opt
52 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 my ($self, %params) = @_;
75  
76 if (defined($params{location})) {
77 $params{q} = delete $params{location};
78 } else {
79 carp "location is a required parameter for geocode()";
80 return;
81 }
82  
83 foreach my $k (keys %params) {
84 if (!exists($valid_params{$k})) {
85 carp "Unknown geocode parameter: $k";
86 delete $params{$k};
87 } elsif ($valid_params{$k} == 0) { # is a real parameter but we dont support it
88 carp "Unsupported geocode parameter: $k";
89 delete $params{$k};
90 }
91 }
92  
93 $params{key} = $self->{api_key};
94
95 # sort the params for better cachability
96 my @final_params;
97 foreach my $k (sort keys %params){
98 push(@final_params, $k => $params{$k})
99
100 }
101 my $URL = $self->{url}->clone();
102 $URL->query_form(\@final_params);
103 my $response = $self->{ua}->get($URL);
104  
105 # Support HTTP::Tiny and LWP:: CPAN packages (and subclasses of either).
106 my $is_lwp = blessed $response && $response->isa('HTTP::Response');
107 my $content = $is_lwp ? $response->decoded_content() : $response->{content};
108 my $is_success = $is_lwp ? $response->is_success() : $response->{success};
109  
110 my $rh_content;
111 my $ok = eval { $rh_content = $self->{json}->decode($content); 1 };
112 if (!$ok) {
113 my $err = $@ || 'unknown error';
114 carp "failed to decode response from '" . $self->_sanitized_url($URL) . "': $err";
115 return;
116 }
117  
118 if (!$is_success) {
119 my $status = $rh_content->{status} || {};
120 # Cleanup server-supplied strings, e.g. new-lines
121 my $code = ($status->{code} // '?') =~ s/[[:cntrl:]]//gr;
122 my $msg = ($status->{message} // 'unknown error') =~ s/[[:cntrl:]]//gr;
123 carp "response when requesting '" . $self->_sanitized_url($URL) . "': $code, $msg";
124 return;
125 }
126 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 my ($self, $url) = @_;
132 my $masked = substr($self->{api_key}, 0, 6) . '...';
133 (my $str = "$url") =~ s/\Q$self->{api_key}\E/$masked/g;
134 return $str;
135 }
136  
137 sub reverse_geocode {
138 my ($self, %params) = @_;
139  
140 foreach my $k (qw(lat lng)) {
141 if (!defined($params{$k})) {
142 carp "$k is a required parameter";
143 return;
144 }
145 }
146  
147 $params{location} = join(',', delete @params{'lat', 'lng'});
148 return $self->geocode(%params);
149 }
150  
151 1;
152  
153 __END__