GeoIP2/Database/Reader.pm

filter: Statementstmt 100% 104 / 104 Branchbran 100% 12 / 12 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 29 / 29 Podpod 100% 8 / 8 totaltotal 100% 156 / 156 CC 8 SCAR 9.4
CC
8
Cov
100%
_model_for_address
8.0
BEGIN
1.0
BEGIN
1.0
CRAP 2.6
↑ Summary GeoIP2/WebService/Client.pm »
1 package GeoIP2::Database::Reader;
2  
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 7 our $VERSION = '2.006002';
7  
8 7 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 7 ▶use Data::Validate::IP 0.25 qw( is_private_ip );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use GeoIP2::Error::Generic;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use GeoIP2::Error::IPAddressNotFound;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 ▶use GeoIP2::Model::ASN;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 7 ▶use GeoIP2::Model::AnonymousIP;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 7 ▶use GeoIP2::Model::City;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 7 ▶use GeoIP2::Model::ConnectionType;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 7 ▶use GeoIP2::Model::Country;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 7 ▶use GeoIP2::Model::Domain;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 7 ▶use GeoIP2::Model::Enterprise;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 7 ▶use GeoIP2::Model::Insights;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 7 ▶use GeoIP2::Model::ISP;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 7 ▶use GeoIP2::Types qw( Str );
Subroutine BEGIN: called CC=1 SCAR=0.0
23 7 ▶use MaxMind::DB::Reader 1.000000;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 7 ▶use namespace::clean -except => 'meta';
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 7 with 'GeoIP2::Role::HasLocales';
28  
29 has file => (
30 is => 'ro',
31 isa => Str,
32 required => 1,
33 10 ▶ coerce => sub { "$_[0]" },
Subroutine __ANON__: called CC=1 SCAR=0.0
34 7 );
35  
36 7 has _reader => (
37 is => 'ro',
38 does => 'MaxMind::DB::Reader::Role::Reader',
39 lazy => 1,
40 builder => '_build_reader',
41 handles => ['metadata'],
42 );
43  
44 10 ▶sub _build_reader {
Subroutine _build_reader: called CC=1 SCAR=0.0
45 10 my $self = shift;
46 10 return MaxMind::DB::Reader->new( file => $self->file );
47 }
48  
49 158 ▶sub _model_for_address {
Subroutine _model_for_address: called CC=8 SCAR=20.8
50 158 my $self = shift;
51 158 my $class = shift;
52 158 my %args = @_;
53 158 my $ip = $args{ip};
54  
55 158 ▶ unless ( defined $ip ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ip) 3 155 158
56 3 my ($method) = ( caller(1) )[3];
57 3 GeoIP2::Error::Generic->throw( message =>
58 "Required param (ip) was missing when calling $method on "
59 . __PACKAGE__ );
60 }
61  
62 155 ▶ unless ( $self->metadata->database_type =~ $args{type_check} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->metadata->database_type =~ /$args{"type_check"}/) 108 47 155
63 108 ( my $method = ( caller(1) )[3] ) =~ s/.+:://;
64 108 GeoIP2::Error::Generic->throw( message => 'The '
65 . ( ref $self )
66 . "->$method()"
67 . ' method cannot be called with a '
68 . $self->metadata->database_type
69 . ' database' );
70 }
71  
72 47 ▶ if ( $ip eq 'me' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ip eq "me") 3 44 47
73 3 my ($method) = ( caller(1) )[3];
74 3 GeoIP2::Error::Generic->throw(
75 message => "me is not a valid IP when calling $method on "
76 . __PACKAGE__ );
77 }
78  
79 44 ▶ if ( is_private_ip($ip) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_private_ip($ip)) 3 41 44
80 3 my ($method) = ( caller(1) )[3];
81 3 GeoIP2::Error::Generic->throw(
82 message => "The IP address you provided ($ip) is not a "
83 . "public IP address when calling $method on "
84 . __PACKAGE__ );
85 }
86  
87 41 my $geoip_record = $self->_reader->record_for_address($ip);
88 38 ▶ unless ($geoip_record) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($geoip_record) 24 14 38
89 24 GeoIP2::Error::IPAddressNotFound->throw(
90 message => "No record found for IP address $ip",
91 ip_address => $ip,
92 );
93 }
94  
95 14 my $model_class = 'GeoIP2::Model::' . $class;
96  
97 14 ▶ if ( $args{is_flat} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'is_flat'}) { } 10 4 14
98 10 $geoip_record->{ip_address} = $ip;
99 }
100 else {
101 4 ▶ $geoip_record->{traits} ||= {};
Condition100%2/2
$geoip_record->{"traits"} ||= {}
l!l
22
MC/DC100%1/1
$geoip_record->{"traits"} ||= {}
$geoip_record->{"traits"}
Truth table100%2/2
A
A= $geoip_record->{"traits"}
Aresult
11
00
102 4 $geoip_record->{traits}{ip_address} = $ip;
103 }
104  
105 return $model_class->new(
106 14 %{$geoip_record},
107 locales => $self->locales,
108 );
109 }
110  
111 1 ▶sub asn {
Subroutine asn: called CC=1 SCAR=0.0
Pod documented
112 1 my $self = shift;
113 1 return $self->_model_for_address(
114 'ASN',
115 type_check => qr/^GeoLite2-ASN$/,
116 is_flat => 1,
117 @_
118 );
119 }
120  
121 25 ▶sub city {
Subroutine city: called CC=1 SCAR=0.0
Pod documented
122 25 my $self = shift;
123 25 return $self->_model_for_address(
124 'City',
125 type_check =>
126 qr/^(?:GeoLite2|GeoIP2)-(?:Precision-|)?City(-[a-zA-Z\-]+)?$/,
127 @_
128 );
129 }
130  
131 25 ▶sub country {
Subroutine country: called CC=1 SCAR=0.0
Pod documented
132 25 my $self = shift;
133 25 return $self->_model_for_address(
134 'Country',
135 type_check => qr/^(?:GeoLite2|GeoIP2)-(?:Precision-)?Country$/,
136 @_
137 );
138 }
139  
140 19 ▶sub connection_type {
Subroutine connection_type: called CC=1 SCAR=0.0
Pod documented
141 19 my $self = shift;
142 19 return $self->_model_for_address(
143 'ConnectionType',
144 type_check => qr/^GeoIP2-(?:Precision-)?Connection-Type$/,
145 is_flat => 1,
146 @_
147 );
148 }
149  
150 19 ▶sub domain {
Subroutine domain: called CC=1 SCAR=0.0
Pod documented
151 19 my $self = shift;
152 19 return $self->_model_for_address(
153 'Domain',
154 type_check => qr/^GeoIP2-(?:Precision-)?Domain$/,
155 is_flat => 1,
156 @_
157 );
158 }
159  
160 26 ▶sub enterprise {
Subroutine enterprise: called CC=1 SCAR=0.0
Pod documented
161 26 my $self = shift;
162 26 return $self->_model_for_address(
163 'Enterprise',
164 type_check => qr/^GeoIP2-(?:Precision-)?Enterprise$/,
165 @_
166 );
167 }
168  
169 19 ▶sub isp {
Subroutine isp: called CC=1 SCAR=0.0
Pod documented
170 19 my $self = shift;
171 19 return $self->_model_for_address(
172 'ISP',
173 type_check => qr/^GeoIP2-(?:Precision-)?ISP$/,
174 is_flat => 1,
175 @_
176 );
177 }
178  
179 24 ▶sub anonymous_ip {
Subroutine anonymous_ip: called CC=1 SCAR=0.0
Pod documented
180 24 my $self = shift;
181 24 return $self->_model_for_address(
182 'AnonymousIP',
183 type_check => qr/^GeoIP2-(?:Precision-)?Anonymous-IP$/,
184 is_flat => 1,
185 @_,
186 );
187 }
188  
189 7 1;
190  
191 # ABSTRACT: Perl API for GeoIP2 databases
192  
193 __END__
↑ Summary GeoIP2/WebService/Client.pm »