GeoIP2/WebService/Client.pm

filter: Statementstmt 98.8% 166 / 168 Branchbran 100% 16 / 16 Conditioncond 68.7% 11 / 16 MC/DCmcdc 44.4% 4 / 9 Subroutinesub 97.7% 44 / 45 Podpod 75.0% 3 / 4 totaltotal 94.5% 244 / 258 CC 15 SCAR 7.5
CC
15
Cov
96%
_handle_4xx_status
7.0
BUILD
3.0
__ANON__
3.0
CRAP 2.1
« GeoIP2/Database/Reader.pm ↑ Summary GeoIP2.pm »
1 package GeoIP2::WebService::Client;
2  
3 1 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 our $VERSION = '2.006002';
9  
10 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use Data::Validate::IP 0.25 qw( is_public_ip );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use GeoIP2::Error::Generic;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use GeoIP2::Error::HTTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use GeoIP2::Error::IPAddressNotFound;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use GeoIP2::Error::WebService;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use GeoIP2::Model::City;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use GeoIP2::Model::Country;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use GeoIP2::Model::Insights;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 use GeoIP2::Types
21 1 ▶ qw( JSONObject MaxMindID MaxMindLicenseKey Str URIObject UserAgentObject );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use HTTP::Headers;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use MIME::Base64 qw( encode_base64 );
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use LWP::Protocol::https;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use Params::Validate qw( validate );
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use Sub::Quote qw( quote_sub );
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 1 ▶use namespace::clean -except => 'meta';
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 1 with 'GeoIP2::Role::HasLocales';
37  
38 1 has account_id => (
39 is => 'ro',
40 isa => MaxMindID,
41 required => 1,
42 );
43 1 *user_id = \&account_id; # for backwards-compatibility
44  
45 1 has license_key => (
46 is => 'ro',
47 isa => MaxMindLicenseKey,
48 required => 1,
49 );
50  
51 1 has host => (
52 is => 'ro',
53 isa => Str,
54 default => quote_sub(q{ 'geoip.maxmind.com' }),
55 );
56  
57 1 has ua => (
58 is => 'ro',
59 isa => UserAgentObject,
60 builder => '_build_ua',
61 );
62  
63 1 has _base_uri => (
64 is => 'ro',
65 isa => URIObject,
66 lazy => 1,
67 builder => '_build_base_uri',
68 );
69  
70 1 has _json => (
71 is => 'ro',
72 isa => JSONObject,
73 init_arg => undef,
74 default => quote_sub(q{ JSON::MaybeXS->new(utf8 => 1) }),
75 );
76  
77 16 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
78 16 my ( $orig, @args ) = @_;
79 16 my %params = %{ $orig->(@args) };
80 $params{account_id} = delete $params{user_id}
81 16 ▶ if exists $params{user_id};
Branch: 1 branch
BranchTrueFalseTotal
if exists $params{"user_id"} 1 15 16
82 16 return \%params;
83 1 };
84  
85 16 ▶sub BUILD {
Subroutine BUILD: called CC=3 SCAR=11.1
Pod undocumented
86 16 my $self = shift;
87  
88 ## no critic (TryTiny::RequireBlockTermination)
89 16 ▶ my $self_version = try { 'v' . $self->VERSION() } || 'v?';
Subroutine __ANON__: called CC=1 SCAR=0.2
Condition50%1/2
&try(sub { 'v' . $self->VERSION; } ) || 'v?'
l!l
160
MC/DC0%0/1
&try(sub { 'v' . $self->VERSION; } ) || 'v?'
&try(sub { 'v' . $self->VERSION; } )
Truth table50%1/2
A
A= &try(sub { 'v' . $self->VERSION; } )
Aresult
11
00
90  
91 16 my $ua = $self->ua();
92 16 ▶ my $ua_version = try { 'v' . $ua->VERSION() } || 'v?';
Subroutine __ANON__: called CC=1 SCAR=0.2
Condition50%1/2
&try(sub { 'v' . $ua->VERSION; } ) || 'v?'
l!l
160
MC/DC0%0/1
&try(sub { 'v' . $ua->VERSION; } ) || 'v?'
&try(sub { 'v' . $ua->VERSION; } )
Truth table50%1/2
A
A= &try(sub { 'v' . $ua->VERSION; } )
Aresult
11
00
93  
94 16 my $agent
95 = blessed($self)
96 . " $self_version" . ' ('
97 . blessed($ua) . q{ }
98 . $ua_version . q{ / }
99 . "Perl $^V)";
100  
101 16 $ua->agent($agent);
102 }
103  
104 21 ▶sub country {
Subroutine country: called CC=1 SCAR=0.0
Pod documented
105 21 my $self = shift;
106  
107 21 return $self->_response_for(
108 'country',
109 'GeoIP2::Model::Country',
110 @_,
111 );
112 }
113  
114 0 ▶sub city {
Subroutine city: not called CC=1 SCAR=6.9
Pod documented
115 0 my $self = shift;
116  
117 0 return $self->_response_for(
118 'city',
119 'GeoIP2::Model::City',
120 @_,
121 );
122 }
123  
124 1 ▶sub insights {
Subroutine insights: called CC=1 SCAR=0.0
Pod documented
125 1 my $self = shift;
126  
127 1 return $self->_response_for(
128 'insights',
129 'GeoIP2::Model::Insights',
130 @_,
131 );
132 }
133  
134 my %spec = (
135 ip => {
136 callbacks => {
137 22 ▶ 'is a public IP address or me' => sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
138 22 ▶ return defined $_[0]
Condition83%5/6
defined $_[0] && ($_[0] eq 'me' || is_public_ip($_[0]))
!ll&&!rl&&r
0616
$_[0] eq "me" or is_public_ip($_[0])
l!l&&r!l&&!r
1156
MC/DC66%2/3
defined $_[0] && ($_[0] eq 'me' || is_public_ip($_[0]))
defined $_[0] $_[0] eq "me" is_public_ip($_[0])
Truth table75%3/4
A && B or C
A= defined $_[0]B= $_[0] eq "me"C= is_public_ip($_[0])
ABCresult
0XX0
1000
11X1
1011
139 && ( $_[0] eq 'me'
140 || is_public_ip( $_[0] ) );
141 }
142 },
143 },
144 1 );
145  
146 22 ▶sub _response_for {
Subroutine _response_for: called CC=2 SCAR=6.9
147 22 my $self = shift;
148 22 my $path = shift;
149 22 my $model_class = shift;
150  
151 22 my %p = validate( @_, \%spec );
152  
153 16 my $uri = $self->_base_uri()->clone();
154 16 $uri->path_segments( $uri->path_segments(), $path, $p{ip} );
155  
156 16 my $request = HTTP::Request->new(
157 'GET', $uri,
158 HTTP::Headers->new( Accept => 'application/json' ),
159 );
160  
161 16 $request->authorization_basic(
162 $self->account_id(),
163 $self->license_key(),
164 );
165  
166 16 my $response = $self->ua()->request($request);
167  
168 16 ▶ if ( $response->code() == 200 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->code == 200) { } 7 9 16
169 7 my $body = $self->_handle_success( $response, $uri );
170 return $model_class->new(
171 6 %{$body},
172 locales => $self->locales(),
173 );
174 }
175 else {
176 # all other error codes throw an exception
177 9 $self->_handle_error_status( $response, $uri, $p{ip} );
178 }
179 }
180  
181 7 ▶sub _handle_success {
Subroutine _handle_success: called CC=1 SCAR=0.0
182 7 my $self = shift;
183 7 my $response = shift;
184 7 my $uri = shift;
185  
186 7 my $body;
187 7 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
188 7 $body = $self->_json()->decode( $response->decoded_content() );
189 }
190 1 ▶ catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
191 1 GeoIP2::Error::Generic->throw(
192 message =>
193 "Received a 200 response for $uri but could not decode the response as JSON: $_",
194 );
195 7 };
196  
197 6 return $body;
198 }
199  
200 9 ▶sub _handle_error_status {
Subroutine _handle_error_status: called CC=3 SCAR=11.0
201 9 my $self = shift;
202 9 my $response = shift;
203 9 my $uri = shift;
204 9 my $ip = shift;
205  
206 9 my $status = $response->code();
207  
208 9 ▶ if ( $status =~ /^4/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($status =~ /^4/) { } 7 2 9
elsif ($status =~ /^5/) { } 1 1 2
209 7 $self->_handle_4xx_status( $response, $status, $uri, $ip );
210 }
211 elsif ( $status =~ /^5/ ) {
212 1 $self->_handle_5xx_status( $status, $uri );
213 }
214 else {
215 1 $self->_handle_non_200_status( $status, $uri );
216 }
217 }
218  
219 7 ▶sub _handle_4xx_status {
Subroutine _handle_4xx_status: called CC=7 SCAR=19.5
220 7 my $self = shift;
221 7 my $response = shift;
222 7 my $status = shift;
223 7 my $uri = shift;
224 7 my $ip = shift;
225  
226 7 ▶ if ( $status == 404 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status == 404) 2 5 7
227 2 GeoIP2::Error::IPAddressNotFound->throw(
228 message => "No record found for IP address $ip",
229 ip_address => $ip,
230 );
231 }
232  
233 5 my $content = $response->decoded_content();
234  
235 5 my $body = {};
236  
237 5 ▶ if ( defined $content && length $content ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $content and length $content) { } 4 1 5
Condition66%2/3
defined $content and length $content
!ll&&!rl&&r
014
MC/DC50%1/2
defined $content and length $content
defined $content length $content
Truth table66%2/3
A and B
A= defined $contentB= length $content
ABresult
0X0
100
111
238 4 ▶ if ( $response->content_type() =~ /json/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->content_type =~ /json/) { } 3 1 4
239 3 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
240 3 $body = $self->_json()->decode($content);
241 }
242 1 ▶ catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
243 1 GeoIP2::Error::HTTP->throw(
244 message =>
245 "Received a $status error for $uri but it did not include the expected JSON body: $_",
246 http_status => $status,
247 uri => $uri,
248 );
249 3 };
250 GeoIP2::Error::Generic->throw( message =>
251 'Response contains JSON but it does not specify code or error keys'
252 2 ▶ ) unless $body->{code} && $body->{error};
Branch: 1 branch
BranchTrueFalseTotal
unless $body->{"code"} and $body->{"error"} 1 1 2
Condition66%2/3
$body->{"code"} and $body->{"error"}
!ll&&!rl&&r
101
MC/DC50%1/2
$body->{"code"} and $body->{"error"}
$body->{"code"} $body->{"error"}
Truth table66%2/3
A and B
A= $body->{"code"}B= $body->{"error"}
ABresult
0X0
100
111
253 }
254 else {
255 1 GeoIP2::Error::HTTP->throw(
256 message =>
257 "Received a $status error for $uri with the following body: $content",
258 http_status => $status,
259 uri => $uri,
260 );
261 }
262 }
263 else {
264 1 GeoIP2::Error::HTTP->throw(
265 message => "Received a $status error for $uri with no body",
266 http_status => $status,
267 uri => $uri,
268 );
269 }
270  
271 GeoIP2::Error::WebService->throw(
272 message => delete $body->{error},
273 1 %{$body},
274 http_status => $status,
275 uri => $uri,
276 );
277 }
278  
279 1 ▶sub _handle_5xx_status {
Subroutine _handle_5xx_status: called CC=1 SCAR=0.0
280 1 my $self = shift;
281 1 my $status = shift;
282 1 my $uri = shift;
283  
284 1 GeoIP2::Error::HTTP->throw(
285 message => "Received a server error ($status) for $uri",
286 http_status => $status,
287 uri => $uri,
288 );
289 }
290  
291 1 ▶sub _handle_non_200_status {
Subroutine _handle_non_200_status: called CC=1 SCAR=0.0
292 1 my $self = shift;
293 1 my $status = shift;
294 1 my $uri = shift;
295  
296 1 GeoIP2::Error::HTTP->throw(
297 message =>
298 "Received a very surprising HTTP status ($status) for $uri",
299 http_status => $status,
300 uri => $uri,
301 );
302 }
303  
304 14 ▶sub _build_base_uri {
Subroutine _build_base_uri: called CC=1 SCAR=0.0
305 14 my $self = shift;
306  
307 14 return URI->new( 'https://' . $self->host() . '/geoip/v2.1' );
308 }
309  
310 2 ▶sub _build_ua {
Subroutine _build_ua: called CC=1 SCAR=0.0
311 2 my $self = shift;
312  
313 2 return LWP::UserAgent->new();
314 }
315  
316 1 1;
317  
318 # ABSTRACT: Perl API for the GeoIP2 Precision web services
319  
320 __END__
« GeoIP2/Database/Reader.pm ↑ Summary GeoIP2.pm »