GeoIP2/Database/Reader.pm

filter: Statementstmt 100% 104 / 104 Branchbran 100% 12 / 12 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 29 / 29 Podpod 100% 8 / 8 totaltotal 100% 156 / 156 CC 8 SCAR 9.4
CC
8
Cov
100%
_model_for_address
8.0
BEGIN
1.0
BEGIN
1.0
CRAP 2.6
↑ Summary GeoIP2/WebService/Client.pm »
1 package GeoIP2::Database::Reader;
2  
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 7 our $VERSION = '2.006000';
7  
8 7 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 7 ▶use Data::Validate::IP 0.25 qw( is_private_ip );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use GeoIP2::Error::Generic;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use GeoIP2::Error::IPAddressNotFound;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 ▶use GeoIP2::Model::ASN;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 7 ▶use GeoIP2::Model::AnonymousIP;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 7 ▶use GeoIP2::Model::City;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 7 ▶use GeoIP2::Model::ConnectionType;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 7 ▶use GeoIP2::Model::Country;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 7 ▶use GeoIP2::Model::Domain;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 7 ▶use GeoIP2::Model::Enterprise;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 7 ▶use GeoIP2::Model::Insights;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 7 ▶use GeoIP2::Model::ISP;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 7 ▶use GeoIP2::Types qw( Str );
Subroutine BEGIN: called CC=1 SCAR=0.0
23 7 ▶use MaxMind::DB::Reader 1.000000;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 7 ▶use namespace::clean -except => 'meta';
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 7 with 'GeoIP2::Role::HasLocales';
28  
29 has file => (
30 is => 'ro',
31 isa => Str,
32 required => 1,
33 10 ▶ coerce => sub { "$_[0]" },
Subroutine __ANON__: called CC=1 SCAR=0.0
34 7 );
35  
36 7 has _reader => (
37 is => 'ro',
38 does => 'MaxMind::DB::Reader::Role::Reader',
39 lazy => 1,
40 builder => '_build_reader',
41 handles => ['metadata'],
42 );
43  
44 sub _build_reader {
45 10 ▶ my $self = shift;
Subroutine _build_reader: called CC=1 SCAR=0.0
46 10 return MaxMind::DB::Reader->new( file => $self->file );
47 }
48  
49 sub _model_for_address {
50 158 ▶ my $self = shift;
Subroutine _model_for_address: called CC=8 SCAR=20.8
51 158 my $class = shift;
52 158 my %args = @_;
53 158 my $ip = $args{ip};
54  
55 158 ▶ unless ( defined $ip ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ip) 3 155 158
56 3 my ($method) = ( caller(1) )[3];
57 3 GeoIP2::Error::Generic->throw( message =>
58 "Required param (ip) was missing when calling $method on "
59 . __PACKAGE__ );
60 }
61  
62 155 ▶ unless ( $self->metadata->database_type =~ $args{type_check} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->metadata->database_type =~ /$args{"type_check"}/) 108 47 155
63 108 ( my $method = ( caller(1) )[3] ) =~ s/.+:://;
64 108 GeoIP2::Error::Generic->throw( message => 'The '
65 . ( ref $self )
66 . "->$method()"
67 . ' method cannot be called with a '
68 . $self->metadata->database_type
69 . ' database' );
70 }
71  
72 47 ▶ if ( $ip eq 'me' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ip eq "me") 3 44 47
73 3 my ($method) = ( caller(1) )[3];
74 3 GeoIP2::Error::Generic->throw(
75 message => "me is not a valid IP when calling $method on "
76 . __PACKAGE__ );
77 }
78  
79 44 ▶ if ( is_private_ip($ip) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_private_ip($ip)) 3 41 44
80 3 my ($method) = ( caller(1) )[3];
81 3 GeoIP2::Error::Generic->throw(
82 message => "The IP address you provided ($ip) is not a "
83 . "public IP address when calling $method on "
84 . __PACKAGE__ );
85 }
86  
87 41 my $geoip_record = $self->_reader->record_for_address($ip);
88 38 ▶ unless ($geoip_record) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($geoip_record) 24 14 38
89 24 GeoIP2::Error::IPAddressNotFound->throw(
90 message => "No record found for IP address $ip",
91 ip_address => $ip,
92 );
93 }
94  
95 14 my $model_class = 'GeoIP2::Model::' . $class;
96  
97 14 ▶ if ( $args{is_flat} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'is_flat'}) { } 10 4 14
98 10 $geoip_record->{ip_address} = $ip;
99 }
100 else {
101 4 ▶ $geoip_record->{traits} ||= {};
Condition100%2/2
$geoip_record->{"traits"} ||= {}
l!l
22
MC/DC100%1/1
$geoip_record->{"traits"} ||= {}
$geoip_record->{"traits"}
Truth table100%2/2
A
A= $geoip_record->{"traits"}
Aresult
11
00
102 4 $geoip_record->{traits}{ip_address} = $ip;
103 }
104  
105 return $model_class->new(
106 14 %{$geoip_record},
107 locales => $self->locales,
108 );
109 }
110  
111 sub asn {
112 1 ▶ my $self = shift;
Subroutine asn: called CC=1 SCAR=0.0
Pod documented
113 1 return $self->_model_for_address(
114 'ASN',
115 type_check => qr/^GeoLite2-ASN$/,
116 is_flat => 1,
117 @_
118 );
119 }
120  
121 sub city {
122 25 ▶ my $self = shift;
Subroutine city: called CC=1 SCAR=0.0
Pod documented
123 25 return $self->_model_for_address(
124 'City',
125 type_check =>
126 qr/^(?:GeoLite2|GeoIP2)-(?:Precision-|)?City(-[a-zA-Z\-]+)?$/,
127 @_
128 );
129 }
130  
131 sub country {
132 25 ▶ my $self = shift;
Subroutine country: called CC=1 SCAR=0.0
Pod documented
133 25 return $self->_model_for_address(
134 'Country',
135 type_check => qr/^(?:GeoLite2|GeoIP2)-(?:Precision-)?Country$/,
136 @_
137 );
138 }
139  
140 sub connection_type {
141 19 ▶ my $self = shift;
Subroutine connection_type: called CC=1 SCAR=0.0
Pod documented
142 19 return $self->_model_for_address(
143 'ConnectionType',
144 type_check => qr/^GeoIP2-(?:Precision-)?Connection-Type$/,
145 is_flat => 1,
146 @_
147 );
148 }
149  
150 sub domain {
151 19 ▶ my $self = shift;
Subroutine domain: called CC=1 SCAR=0.0
Pod documented
152 19 return $self->_model_for_address(
153 'Domain',
154 type_check => qr/^GeoIP2-(?:Precision-)?Domain$/,
155 is_flat => 1,
156 @_
157 );
158 }
159  
160 sub enterprise {
161 26 ▶ my $self = shift;
Subroutine enterprise: called CC=1 SCAR=0.0
Pod documented
162 26 return $self->_model_for_address(
163 'Enterprise',
164 type_check => qr/^GeoIP2-(?:Precision-)?Enterprise$/,
165 @_
166 );
167 }
168  
169 sub isp {
170 19 ▶ my $self = shift;
Subroutine isp: called CC=1 SCAR=0.0
Pod documented
171 19 return $self->_model_for_address(
172 'ISP',
173 type_check => qr/^GeoIP2-(?:Precision-)?ISP$/,
174 is_flat => 1,
175 @_
176 );
177 }
178  
179 sub anonymous_ip {
180 24 ▶ my $self = shift;
Subroutine anonymous_ip: called CC=1 SCAR=0.0
Pod documented
181 24 return $self->_model_for_address(
182 'AnonymousIP',
183 type_check => qr/^GeoIP2-(?:Precision-)?Anonymous-IP$/,
184 is_flat => 1,
185 @_,
186 );
187 }
188  
189 7 1;
190  
191 # ABSTRACT: Perl API for GeoIP2 databases
192  
193 __END__
194  
195 =pod
196  
197 =encoding UTF-8
198  
199 =head1 NAME
200  
201 GeoIP2::Database::Reader - Perl API for GeoIP2 databases
202  
203 =head1 VERSION
204  
205 version 2.006000
206  
207 =head1 SYNOPSIS
208  
209 use 5.008;
210  
211 use GeoIP2::Database::Reader;
212  
213 my $reader = GeoIP2::Database::Reader->new(
214 file => '/path/to/database',
215 locales => [ 'en', 'de', ]
216 );
217  
218 my $city = $reader->city( ip => '24.24.24.24' );
219 my $country = $city->country();
220 print $country->iso_code(), "\n";
221  
222 =head1 DESCRIPTION
223  
224 This class provides a reader API for all GeoIP2 databases. Each method returns
225 a different model class.
226  
227 If the database does not return a particular piece of data for an IP address,
228 the associated attribute is not populated.
229  
230 =head1 USAGE
231  
232 The basic API for this class is the same for all database types. First you
233 create a database reader object with your C<file> and C<locale> params.
234 Then you call the method corresponding to your database type, passing it the
235 IP address you want to look up.
236  
237 If the request succeeds, the method call will return a model class for the
238 method point you called.
239  
240 If the database cannot be read, the reader class throws an exception.
241  
242 =head1 IP GEOLOCATION USAGE
243  
244 IP geolocation is inherently imprecise. Locations are often near the center of
245 the population. Any location provided by a GeoIP2 database should not be used
246 to identify a particular address or household.
247  
248 =head1 CONSTRUCTOR
249  
250 This class has a single constructor method:
251  
252 =head2 GeoIP2::Database::Reader->new()
253  
254 This method creates a new object. It accepts the following arguments:
255  
256 =over 4
257  
258 =item * file
259  
260 This is the path to the GeoIP2 database file which you'd like to query.
261  
262 =item * locales
263  
264 This is an array reference where each value is a string indicating a locale.
265 This argument will be passed on to record classes to use when their C<name()>
266 methods are called.
267  
268 The order of the locales is significant. When a record class has multiple
269 names (country, city, etc.), its C<name()> method will look at each element of
270 this array ref and return the first locale for which it has a name.
271  
272 Note that the only locale which is always present in the GeoIP2 data in "en".
273 If you do not include this locale, the C<name()> method may end up returning
274 C<undef> even when the record in question has an English name.
275  
276 Currently, the valid list of locale codes is:
277  
278 =over 8
279  
280 =item * de - German
281  
282 =item * en - English
283  
284 English names may still include accented characters if that is the accepted
285 spelling in English. In other words, English does not mean ASCII.
286  
287 =item * es - Spanish
288  
289 =item * fr - French
290  
291 =item * ja - Japanese
292  
293 =item * pt-BR - Brazilian Portuguese
294  
295 =item * ru - Russian
296  
297 =item * zh-CN - simplified Chinese
298  
299 =back
300  
301 Passing any other locale code will result in an error.
302  
303 The default value for this argument is C<['en']>.
304  
305 =back
306  
307 =head1 REQUEST METHODS
308  
309 All of the request methods accept a single argument:
310  
311 =over 4
312  
313 =item * ip
314  
315 This must be a valid IPv4 or IPv6 address. This is the address that you want to
316 look up using the GeoIP2 web service.
317  
318 Unlike the web service client class, you cannot pass the string "me" as your ip
319 address.
320  
321 =back
322  
323 =head2 $reader->asn()
324  
325 This method returns a L<GeoIP2::Model::ASN> object.
326  
327 =head2 $reader->connection_type()
328  
329 This method returns a L<GeoIP2::Model::ConnectionType> object.
330  
331 =head2 $reader->country()
332  
333 This method returns a L<GeoIP2::Model::Country> object.
334  
335 =head2 $reader->city()
336  
337 This method returns a L<GeoIP2::Model::City> object.
338  
339 =head2 $reader->domain()
340  
341 This method returns a L<GeoIP2::Model::Domain> object.
342  
343 =head2 $reader->isp()
344  
345 This method returns a L<GeoIP2::Model::ISP> object.
346  
347 =head2 $reader->enterprise()
348  
349 This method returns a L<GeoIP2::Model::Enterprise> object.
350  
351 =head2 $reader->anonymous_ip()
352  
353 This method returns a L<GeoIP2::Model::AnonymousIP> object.
354  
355 =head1 OTHER METHODS
356  
357 =head2 $reader->metadata()
358  
359 This method returns a L<MaxMind::DB::Metadata> object containing information
360 about the database.
361  
362 =head1 EXCEPTIONS
363  
364 In the case of a fatal error, the reader will throw a
365 L<GeoIP2::Error::Generic> or L<GeoIP2::Error::IPAddressNotFound> exception
366 object.
367  
368 This error class has an C<< $error->message() >> method and overload
369 stringification to show that message. This means that if you don't explicitly
370 catch errors they will ultimately be sent to C<STDERR> with some sort of
371 (hopefully) useful error message.
372  
373 =head1 WHAT DATA IS RETURNED?
374  
375 While many of the databases return the same basic records, the attributes which
376 can be populated vary between model classes. In addition, while a database may
377 offer a particular piece of data, MaxMind does not always have every piece of
378 data for any given IP address.
379  
380 Because of these factors, it is possible for any model class to return a record
381 where some or all of the attributes are unpopulated.
382  
383 See L<http://dev.maxmind.com/geoip/geoip2/web-services> for details on what
384 data each end point I<may> return.
385  
386 Every record class attribute has a corresponding predicate method so you can
387 check to see if the attribute is set.
388  
389 =head1 SUPPORT
390  
391 =head1 AUTHORS
392  
393 =over 4
394  
395 =item *
396  
397 Dave Rolsky <drolsky@maxmind.com>
398  
399 =item *
400  
401 Greg Oschwald <goschwald@maxmind.com>
402  
403 =item *
404  
405 Mark Fowler <mfowler@maxmind.com>
406  
407 =item *
408  
409 Olaf Alders <oalders@maxmind.com>
410  
411 =back
412  
413 =head1 COPYRIGHT AND LICENSE
414  
415 This software is copyright (c) 2013 - 2018 by MaxMind, Inc.
416  
417 This is free software; you can redistribute it and/or modify it under
418 the same terms as the Perl 5 programming language system itself.
419  
420 =cut
↑ Summary GeoIP2/WebService/Client.pm »