File Coverage

blib/lib/Geo/Location/IP/Database/Reader.pm
Criterion Covered Total %
statement 154 154 100.0
branch 12 12 100.0
condition 4 6 66.6
subroutine 20 20 100.0
pod 8 8 100.0
total 198 200 99.0


line stmt bran cond sub pod time code
1             package Geo::Location::IP::Database::Reader;
2              
3             # SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later
4              
5 10     10   1002110 use 5.026;
  10         33  
6 10     10   81 use warnings;
  10         26  
  10         2031  
7 10     10   60 use utf8;
  10         24  
  10         79  
8              
9 10     10   6128 use Object::Pad;
  10         112576  
  10         68  
10              
11             class Geo::Location::IP::Database::Reader
12 10     10   7000 :isa(Geo::Location::IP::Database::SimpleReader);
  10         39  
  10         1540  
13              
14             our $VERSION = 0.005;
15              
16 10     10   1533 use Geo::Location::IP::Address;
  10         19  
  10         305  
17 10     10   5644 use Geo::Location::IP::Error::Generic;
  10         36  
  10         513  
18 10     10   5113 use Geo::Location::IP::Error::AddressNotFound;
  10         51  
  10         37964  
19              
20 2     2 1 976 method anonymous_ip ($key, $ip) {
  2         10  
  2         5  
  2         5  
  2         4  
21 2         16 $self->_assert_database_type(qr{Anonymous-IP});
22 2         11 $self->_assert_ip_address($key, $ip);
23 2         16 my $model = $self->SUPER::anonymous_ip($key => $ip);
24 2         11 $self->_assert_record($model, $ip);
25 1         5 return $model;
26             }
27              
28 4     4 1 2316 method asn ($key, $ip) {
  4         20  
  4         12  
  4         11  
  4         6  
29 4         34 $self->_assert_database_type(qr{ASN});
30 3         22 $self->_assert_ip_address($key, $ip);
31 3         30 my $model = $self->SUPER::asn($key => $ip);
32 3         18 $self->_assert_record($model, $ip);
33 2         8 return $model;
34             }
35              
36 31     31 1 10843 method city ($key, $ip) {
  31         117  
  31         57  
  31         55  
  31         53  
37 31         179 $self->_assert_database_type(qr{City});
38 31         128 $self->_assert_ip_address($key, $ip);
39 10         63 my $model = $self->SUPER::city($key => $ip);
40 9         79 $self->_assert_record($model, $ip);
41 3         15 return $model;
42             }
43              
44 2     2 1 909 method connection_type ($key, $ip) {
  2         10  
  2         4  
  2         6  
  2         4  
45 2         16 $self->_assert_database_type(qr{Connection-Type});
46 2         13 $self->_assert_ip_address($key, $ip);
47 2         16 my $model = $self->SUPER::connection_type($key => $ip);
48 2         11 $self->_assert_record($model, $ip);
49 1         4 return $model;
50             }
51              
52 1     1 1 633 method country ($key, $ip) {
  1         5  
  1         3  
  1         2  
  1         18  
53 1         10 $self->_assert_database_type(qr{Country});
54 1         8 $self->_assert_ip_address($key, $ip);
55 1         8 my $model = $self->SUPER::country($key => $ip);
56 1         6 $self->_assert_record($model, $ip);
57 1         4 return $model;
58             }
59              
60 2     2 1 889 method domain ($key, $ip) {
  2         8  
  2         7  
  2         5  
  2         4  
61 2         20 $self->_assert_database_type(qr{Domain});
62 2         14 $self->_assert_ip_address($key, $ip);
63 2         17 my $model = $self->SUPER::domain($key => $ip);
64 2         11 $self->_assert_record($model, $ip);
65 1         3 return $model;
66             }
67              
68 2     2 1 606 method enterprise ($key, $ip) {
  2         6  
  2         6  
  2         3  
  2         3  
69 2         14 $self->_assert_database_type(qr{Enterprise});
70 2         11 $self->_assert_ip_address($key, $ip);
71 2         12 my $model = $self->SUPER::enterprise($key => $ip);
72 2         16 $self->_assert_record($model, $ip);
73 1         6 return $model;
74             }
75              
76 2     2 1 549 method isp ($key, $ip) {
  2         5  
  2         5  
  2         2  
  2         3  
77 2         13 $self->_assert_database_type(qr{ISP});
78 2         9 $self->_assert_ip_address($key, $ip);
79 2         11 my $model = $self->SUPER::isp($key => $ip);
80 2         7 $self->_assert_record($model, $ip);
81 1         3 return $model;
82             }
83              
84             our $private_ip_address_regex = qr{
85             \A
86             (?:
87             10\.
88             |
89             172\.(?:1[6-9]|2[0-9]|3[01])\.
90             |
91             192\.168\.
92             |
93             f[cd]
94             )
95             }xms;
96              
97 46     46   86 method _assert_database_type ($type_regex) {
  46         131  
  46         80  
  46         66  
98 46         224 my $type = $self->metadata->database_type;
99 46 100       2699 if ($type !~ $type_regex) {
100 1         3 my $class = ref $self;
101 1         10 my $method = (caller(1))[3] =~ s{.+::}{}r;
102 1         5 my $message = "The $class->$method() method cannot be called with a "
103             . "$type database";
104 1         11 Geo::Location::IP::Error::Generic->throw(message => $message);
105             }
106 45         115 return;
107             }
108              
109 45     45   83 method _assert_ip_address ($key, $ip) {
  45         114  
  45         81  
  45         79  
  45         76  
110 45 100 66     366 if (!defined $ip || !defined $key || $key ne 'ip') {
      66        
111 1         2 my $class = ref $self;
112 1         9 my $method = (caller(1))[3] =~ s{.+::}{}r;
113 1         5 my $message = "Required param (ip) was missing when calling $method "
114             . "on $class";
115 1         5 Geo::Location::IP::Error::Generic->throw(message => $message);
116             }
117 44 100       133 if ($ip eq 'me') {
118 1         3 my $class = ref $self;
119 1         10 my $method = (caller(1))[3] =~ s{.+::}{}r;
120 1         3 my $message = "me is not a valid IP when calling $method on $class";
121 1         5 Geo::Location::IP::Error::Generic->throw(message => $message);
122             }
123 43 100       396 if ($ip =~ $private_ip_address_regex) {
124 19         41 my $class = ref $self;
125 19         178 my $method = (caller(1))[3] =~ s{.+::}{}r;
126 19         62 my $message = "The IP address you provided ($ip) is not a public "
127             . "IP address when calling $method on $class";
128 19         87 Geo::Location::IP::Error::Generic->throw(message => $message);
129             }
130 24         81 return;
131             }
132              
133 23     23   52 method _assert_record ($obj, $ip) {
  23         69  
  23         51  
  23         45  
  23         60  
134 23 100       105 if (!defined $obj) {
135 12         230 my $ip_address = Geo::Location::IP::Address->new(
136             address => $ip,
137             network => undef,
138             );
139 12         193 Geo::Location::IP::Error::AddressNotFound->throw(
140             message => "No record found for IP address $ip",
141             ip_address => $ip_address,
142             );
143             }
144 11         33 return;
145             }
146              
147 24     24   52 method _get ($ip) {
  24         56  
  24         50  
  24         63  
148 24         44 local $@;
149 24         58 my @result = eval { $self->SUPER::_get($ip) };
  24         124  
150 24 100       119 if (my $e = $@) {
151 1         5 Geo::Location::IP::Error::Generic->throw(message => $e);
152             }
153 23         97 return @result;
154             }
155              
156             1;
157             __END__