File Coverage

blib/lib/Geo/Location/IP/Database/SimpleReader.pm
Criterion Covered Total %
statement 128 129 99.2
branch 17 18 94.4
condition n/a
subroutine 27 27 100.0
pod 10 10 100.0
total 182 184 98.9


line stmt bran cond sub pod time code
1             package Geo::Location::IP::Database::SimpleReader;
2              
3             # SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later
4              
5 10     10   145 use 5.026;
  10         30  
6 10     10   41 use warnings;
  10         17  
  10         409  
7 10     10   42 use utf8;
  10         15  
  10         80  
8              
9 10     10   256 use Object::Pad;
  10         23  
  10         74  
10              
11             class Geo::Location::IP::Database::SimpleReader;
12              
13             our $VERSION = 0.005;
14              
15 10     10   8305 use Geo::Location::IP::Address;
  10         29  
  10         573  
16 10     10   4985 use Geo::Location::IP::Model::AnonymousIP;
  10         24  
  10         464  
17 10     10   4412 use Geo::Location::IP::Model::ASN;
  10         25  
  10         378  
18 10     10   4984 use Geo::Location::IP::Model::City;
  10         36  
  10         672  
19 10     10   6220 use Geo::Location::IP::Model::ConnectionType;
  10         29  
  10         415  
20 10     10   4942 use Geo::Location::IP::Model::Country;
  10         26  
  10         395  
21 10     10   4538 use Geo::Location::IP::Model::Domain;
  10         28  
  10         381  
22 10     10   4554 use Geo::Location::IP::Model::Enterprise;
  10         27  
  10         368  
23 10     10   4604 use Geo::Location::IP::Model::ISP;
  10         32  
  10         371  
24 10     10   57 use Geo::Location::IP::Network;
  10         16  
  10         333  
25 10     10   4731 use IP::Geolocation::MMDB 1.011;
  10         1285957  
  10         843  
26              
27 10     10   6202 apply Geo::Location::IP::Role::HasLocales;
  10         29  
  10         27392  
28              
29             field $db;
30              
31             #<<<
32             ADJUST :params (:$file) {
33             $db = IP::Geolocation::MMDB->new(file => $file);
34             }
35             #>>>
36              
37 2     2 1 5 method anonymous_ip ($, $ip) {
  2         5  
  2         6  
  2         4  
38 2         9 my ($hash_ref, $ip_address) = $self->_get($ip);
39 2 100       8 if (defined $hash_ref) {
40 1         14 return Geo::Location::IP::Model::AnonymousIP->_from_hash($hash_ref,
41             $ip_address);
42             }
43 1         3 return;
44             }
45              
46 3     3 1 7 method asn ($, $ip) {
  3         12  
  3         8  
  3         6  
47 3         18 my ($hash_ref, $ip_address) = $self->_get($ip);
48 3 100       11 if (defined $hash_ref) {
49 2         27 return Geo::Location::IP::Model::ASN->_from_hash($hash_ref,
50             $ip_address);
51             }
52 1         4 return;
53             }
54              
55 10     10 1 19 method city ($, $ip) {
  10         27  
  10         19  
  10         44  
56 10         114 my ($hash_ref, $ip_address) = $self->_get($ip);
57 9 100       34 if (defined $hash_ref) {
58 3         32 return Geo::Location::IP::Model::City->_from_hash($hash_ref,
59             $ip_address, $self->locales);
60             }
61 6         16 return;
62             }
63              
64 2     2 1 4 method connection_type ($, $ip) {
  2         6  
  2         5  
  2         4  
65 2         9 my ($hash_ref, $ip_address) = $self->_get($ip);
66 2 100       7 if (defined $hash_ref) {
67 1         15 return Geo::Location::IP::Model::ConnectionType->_from_hash($hash_ref,
68             $ip_address);
69             }
70 1         5 return;
71             }
72              
73 1     1 1 2 method country ($, $ip) {
  1         3  
  1         2  
  1         2  
74 1         6 my ($hash_ref, $ip_address) = $self->_get($ip);
75 1 50       4 if (defined $hash_ref) {
76 1         5 return Geo::Location::IP::Model::Country->_from_hash($hash_ref,
77             $ip_address, $self->locales);
78             }
79 0         0 return;
80             }
81              
82 2     2 1 6 method domain ($, $ip) {
  2         7  
  2         5  
  2         5  
83 2         12 my ($hash_ref, $ip_address) = $self->_get($ip);
84 2 100       7 if (defined $hash_ref) {
85 1         17 return Geo::Location::IP::Model::Domain->_from_hash($hash_ref,
86             $ip_address);
87             }
88 1         5 return;
89             }
90              
91 2     2 1 3 method enterprise ($, $ip) {
  2         5  
  2         3  
  2         2  
92 2         8 my ($hash_ref, $ip_address) = $self->_get($ip);
93 2 100       5 if (defined $hash_ref) {
94 1         5 return Geo::Location::IP::Model::Enterprise->_from_hash($hash_ref,
95             $ip_address, $self->locales);
96             }
97 1         4 return;
98             }
99              
100 2     2 1 4 method isp ($, $ip) {
  2         2  
  2         4  
  2         3  
101 2         6 my ($hash_ref, $ip_address) = $self->_get($ip);
102 2 100       5 if (defined $hash_ref) {
103 1         10 return Geo::Location::IP::Model::ISP->_from_hash($hash_ref,
104             $ip_address);
105             }
106 1         2 return;
107             }
108              
109 8     8 1 9164 method file () {
  8         46  
  8         16  
110 8         87 return $db->file;
111             }
112              
113 54     54 1 112 method metadata () {
  54         148  
  54         106  
114 54         253 return $db->metadata;
115             }
116              
117 24     24   53 method _get ($ip) {
  24         95  
  24         47  
  24         42  
118 24         857 my ($hash_ref, $prefixlen) = $db->get($ip);
119 23         3991 my $ip_address;
120 23 100       89 if (defined $hash_ref) {
121 11         180 my $network = Geo::Location::IP::Network->new(
122             address => $ip,
123             prefixlen => $prefixlen,
124             );
125 11         132 $ip_address = Geo::Location::IP::Address->new(
126             address => $ip,
127             network => $network,
128             );
129             }
130 23         114 return $hash_ref, $ip_address;
131             }
132              
133             1;
134             __END__