File Coverage

blib/lib/Geo/Location/IP/Address.pm
Criterion Covered Total %
statement 41 43 95.3
branch 6 8 75.0
condition 2 3 66.6
subroutine 12 13 92.3
pod 3 5 60.0
total 64 72 88.8


line stmt bran cond sub pod time code
1             package Geo::Location::IP::Address;
2              
3             # SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later
4              
5 20     20   1046490 use 5.026;
  20         73  
6 20     20   95 use warnings;
  20         51  
  20         1142  
7 20     20   116 use utf8;
  20         43  
  20         187  
8              
9 20     20   7162 use Object::Pad;
  20         119830  
  20         137  
10              
11             class Geo::Location::IP::Address;
12              
13             our $VERSION = 0.005;
14              
15 20     20   18137 use Geo::Location::IP::Network;
  20         151  
  20         1169  
16 20     20   149 use Scalar::Util qw();
  20         34  
  20         21460  
17              
18 27     27 1 610 field $address :param :reader;
  27         151  
19 4     4 1 679 field $network :param :reader;
  4         23  
20 7     7 1 2170 field $version :reader;
  7         47  
21              
22             ADJUST {
23             if (defined $address) {
24             if (defined $network && defined $network->version) {
25             $version = $network->version;
26             }
27             else {
28             if (index($address, '.') >= 0) {
29             $version = 4;
30             if (!defined $network) {
31             $network = Geo::Location::IP::Network->new(
32             address => $address,
33             prefixlen => 32
34             );
35             }
36             }
37             elsif (index($address, ':') >= 0) {
38             $version = 6;
39             if (!defined $network) {
40             $network = Geo::Location::IP::Network->new(
41             address => $address,
42             prefixlen => 128
43             );
44             }
45             }
46             }
47             }
48             }
49              
50 11     11   31 sub _from_hash ($class, $hash_ref, $ip_address) {
  11         29  
  11         25  
  11         25  
  11         26  
51             # If a web service accepts "me" as an IP address, the returned IP address
52             # may differ from the local host's IP address if the host is behind a NAT
53             # gateway.
54 11 100       51 if (exists $hash_ref->{ip_address}) {
55 3         9 my $ip = $hash_ref->{ip_address};
56 3 50 66     20 if (!defined $ip_address || $ip_address->address ne $ip) {
57 3         33 $ip_address = $class->new(address => $ip, network => undef);
58             }
59             }
60 11         991 return $ip_address;
61             }
62              
63             use overload
64 20         150 q{eq} => \&eq,
65 20     20   173 q{""} => \&stringify;
  20         33  
66              
67             sub eq {
68 24     24 0 14829 my ($self, $other) = @_;
69              
70 24 100       118 if (Scalar::Util::blessed($other)) {
71 1 50       7 if ($other->isa(__PACKAGE__)) {
72 1         6 return $self->address eq $other->address;
73             }
74             }
75 23         139 return $self->address eq $other;
76             }
77              
78             sub stringify {
79 0     0 0   my $self = shift;
80              
81 0           return $self->address;
82             }
83              
84             1;
85             __END__