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