GeoIP2/Database/Reader.pm

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