blib/lib/Net/IPData.pm

filter: Statementstmt 83.3% 100 / 120 Branchbran 88.8% 32 / 36 Conditioncond 64.0% 16 / 25 MC/DCmcdc 46.6% 7 / 15 Subroutinesub 76.6% 23 / 30 Podpod 100% 17 / 17 totaltotal 80.2% 195 / 243 CC 30 SCAR 15.3
CC
30
Cov
82%
_handle_response
11.9
lookup_field
6.0
bulk
6.0
CRAP 4.6
↑ Summary
1 package Net::IPData;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 our $VERSION = '1.0.0';
8  
9 3 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use JSON::PP qw(encode_json decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # API base URLs
15 3 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 BASE_URL => 'https://api.ipdata.co',
17 EU_BASE_URL => 'https://eu-api.ipdata.co',
18 MAX_BULK => 100,
19 3 };
20  
21 # Valid top-level fields for filtering
22 3 my %VALID_FIELDS = map { $_ => 1 } qw(
23 ip is_eu city region region_code region_type
24 country_name country_code continent_name continent_code
25 latitude longitude postal calling_code flag
26 emoji_flag emoji_unicode asn company carrier
27 languages currency time_zone threat count status message
28 );
29  
30 # Valid single-field path lookups (fields accessible via /{ip}/{field})
31 3 my %PATH_FIELDS = map { $_ => 1 } qw(
32 ip is_eu city region region_code region_type
33 country_name country_code continent_name continent_code
34 latitude longitude postal calling_code flag
35 emoji_flag emoji_unicode asn carrier languages
36 currency time_zone threat
37 );
38  
39 9 ▶sub new {
Subroutine new: called CC=6 SCAR=17.9
Pod documented
40 9 my ($class, %args) = @_;
41  
42 my $api_key = $args{api_key}
43 9 ▶ or croak 'Required parameter "api_key" is missing';
Branch: 1 branch
BranchTrueFalseTotal
my $api_key = $args{"api_key"} or croak("Required parameter \"api_key\" is missing") 7 2 9
44  
45 7 my $base_url = $args{base_url};
46 7 ▶ if ($args{eu} && !$base_url) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"eu"} and not $base_url) 1 6 7
Condition100%3/3
$args{"eu"} and not $base_url
!ll&&!rl&&r
511
MC/DC100%2/2
$args{"eu"} and not $base_url
$args{"eu"} not $base_url
Truth table100%3/3
A and not(B)
A= $args{"eu"}B= not $base_url
ABresult
0X0
100
111
47 1 $base_url = EU_BASE_URL;
48 }
49 7 ▶ $base_url //= BASE_URL;
Condition100%2/2
$base_url //= "https://api.ipdata.co"
l!l
34
MC/DC100%1/1
$base_url //= "https://api.ipdata.co"
$base_url
Truth table100%2/2
A
A= $base_url
Aresult
11
00
50 7 $base_url =~ s{/+$}{}; # strip trailing slashes
51  
52 7 ▶ my $timeout = $args{timeout} // 30;
Condition100%2/2
$args{"timeout"} // 30
l!l
16
MC/DC100%1/1
$args{"timeout"} // 30
$args{"timeout"}
Truth table100%2/2
A
A= $args{"timeout"}
Aresult
11
00
53  
54 7 my $ua = HTTP::Tiny->new(
55 agent => "Net-IPData-Perl/$VERSION",
56 timeout => $timeout,
57 default_headers => {
58 'Accept' => 'application/json',
59 'Content-Type' => 'application/json',
60 },
61 );
62  
63 7 return bless {
64 api_key => $api_key,
65 base_url => $base_url,
66 ua => $ua,
67 timeout => $timeout,
68 }, $class;
69 }
70  
71 # --- Public Methods ---
72  
73 10 ▶sub lookup {
Subroutine lookup: called CC=5 SCAR=16.1
Pod documented
74 10 my ($self, $ip, %opts) = @_;
75  
76 10 ▶ my $path = defined $ip && length $ip ? "/$ip" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $ip && length $ip ? : 9 1 10
Condition66%2/3
defined $ip and length $ip
!ll&&!rl&&r
109
MC/DC50%1/2
defined $ip and length $ip
defined $ip length $ip
Truth table66%2/3
A and B
A= defined $ipB= length $ip
ABresult
0X0
100
111
77 10 my @params;
78  
79 10 ▶ if (my $fields = $opts{fields}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fields = $opts{"fields"}) 4 6 10
80 4 ▶ my @field_list = ref $fields eq 'ARRAY' ? @$fields : split(/\s*,\s*/, $fields);
Branch: 1 branch
BranchTrueFalseTotal
ref $fields eq 'ARRAY' ? : 3 1 4
81 4 _validate_fields(\@field_list);
82 3 push @params, 'fields=' . join(',', @field_list);
83 }
84  
85 9 my $url = $self->_build_url($path, @params);
86 9 return $self->_get($url);
87 }
88  
89 1 ▶sub lookup_mine {
Subroutine lookup_mine: called CC=1 SCAR=0.0
Pod documented
90 1 my ($self, %opts) = @_;
91 1 return $self->lookup(undef, %opts);
92 }
93  
94 10 ▶sub lookup_field {
Subroutine lookup_field: called CC=6 SCAR=18.0
Pod documented
95 10 my ($self, $ip, $field) = @_;
96  
97 10 ▶ croak 'Required parameter "ip" is missing' unless defined $ip && length $ip;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ip and length $ip 1 9 10
Condition66%2/3
defined $ip and length $ip
!ll&&!rl&&r
019
MC/DC50%1/2
defined $ip and length $ip
defined $ip length $ip
Truth table66%2/3
A and B
A= defined $ipB= length $ip
ABresult
0X0
100
111
98 9 ▶ croak 'Required parameter "field" is missing' unless defined $field && length $field;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $field and length $field 1 8 9
Condition66%2/3
defined $field and length $field
!ll&&!rl&&r
018
MC/DC50%1/2
defined $field and length $field
defined $field length $field
Truth table66%2/3
A and B
A= defined $fieldB= length $field
ABresult
0X0
100
111
99 8 ▶ croak "Invalid field: $field" unless $PATH_FIELDS{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless $PATH_FIELDS{$field} 3 5 8
100  
101 5 my $url = $self->_build_url("/$ip/$field");
102 5 return $self->_get($url);
103 }
104  
105 5 ▶sub bulk {
Subroutine bulk: called CC=6 SCAR=18.0
Pod documented
106 5 my ($self, $ips, %opts) = @_;
107  
108 5 ▶ croak 'Required parameter "ips" must be an array reference'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ips eq "ARRAY" 1 4 5
109 unless ref $ips eq 'ARRAY';
110 4 ▶ croak 'Bulk lookup supports a maximum of ' . MAX_BULK . ' IP addresses'
Branch: 1 branch
BranchTrueFalseTotal
if @$ips > 100 1 3 4
111 if @$ips > MAX_BULK;
112 3 ▶ croak 'Bulk lookup requires at least one IP address'
Branch: 1 branch
BranchTrueFalseTotal
if @$ips == 0 1 2 3
113 if @$ips == 0;
114  
115 2 my @params;
116 2 ▶ if (my $fields = $opts{fields}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fields = $opts{"fields"}) 1 1 2
117 1 ▶ my @field_list = ref $fields eq 'ARRAY' ? @$fields : split(/\s*,\s*/, $fields);
Branch: 1 branch
BranchTrueFalseTotal
ref $fields eq 'ARRAY' ? : 1 0 1
118 1 _validate_fields(\@field_list);
119 0 push @params, 'fields=' . join(',', @field_list);
120 }
121  
122 1 my $url = $self->_build_url('/bulk', @params);
123 1 return $self->_post($url, $ips);
124 }
125  
126 0 ▶sub asn {
Subroutine asn: not called CC=1 SCAR=6.9
Pod documented
127 0 my ($self, $ip) = @_;
128 0 return $self->lookup_field($ip, 'asn');
129 }
130  
131 1 ▶sub threat {
Subroutine threat: called CC=1 SCAR=0.0
Pod documented
132 1 my ($self, $ip) = @_;
133 1 return $self->lookup_field($ip, 'threat');
134 }
135  
136 0 ▶sub carrier {
Subroutine carrier: not called CC=1 SCAR=6.9
Pod documented
137 0 my ($self, $ip) = @_;
138 0 return $self->lookup_field($ip, 'carrier');
139 }
140  
141 1 ▶sub currency {
Subroutine currency: called CC=1 SCAR=0.0
Pod documented
142 1 my ($self, $ip) = @_;
143 1 return $self->lookup_field($ip, 'currency');
144 }
145  
146 0 ▶sub time_zone {
Subroutine time_zone: not called CC=1 SCAR=6.9
Pod documented
147 0 my ($self, $ip) = @_;
148 0 return $self->lookup_field($ip, 'time_zone');
149 }
150  
151 0 ▶sub languages {
Subroutine languages: not called CC=1 SCAR=6.9
Pod documented
152 0 my ($self, $ip) = @_;
153 0 return $self->lookup_field($ip, 'languages');
154 }
155  
156 0 ▶sub country_name {
Subroutine country_name: not called CC=1 SCAR=6.9
Pod documented
157 0 my ($self, $ip) = @_;
158 0 return $self->lookup_field($ip, 'country_name');
159 }
160  
161 0 ▶sub country_code {
Subroutine country_code: not called CC=1 SCAR=6.9
Pod documented
162 0 my ($self, $ip) = @_;
163 0 return $self->lookup_field($ip, 'country_code');
164 }
165  
166 0 ▶sub is_eu {
Subroutine is_eu: not called CC=1 SCAR=6.9
Pod documented
167 0 my ($self, $ip) = @_;
168 0 return $self->lookup_field($ip, 'is_eu');
169 }
170  
171 # --- Accessors ---
172  
173 1 ▶sub api_key { return $_[0]->{api_key} }
Subroutine api_key: called CC=1 SCAR=0.0
Pod documented
174 4 ▶sub base_url { return $_[0]->{base_url} }
Subroutine base_url: called CC=1 SCAR=0.0
Pod documented
175 2 ▶sub timeout { return $_[0]->{timeout} }
Subroutine timeout: called CC=1 SCAR=0.0
Pod documented
176  
177 # --- Private Methods ---
178  
179 15 ▶sub _build_url {
Subroutine _build_url: called CC=2 SCAR=7.0
180 15 my ($self, $path, @params) = @_;
181  
182 15 unshift @params, 'api-key=' . $self->{api_key};
183 15 my $query = join('&', @params);
184  
185 15 ▶ return $self->{base_url} . ($path // '') . '?' . $query;
Condition50%1/2
$path // ""
l!l
150
MC/DC0%0/1
$path // ""
$path
Truth table50%1/2
A
A= $path
Aresult
11
00
186 }
187  
188 14 ▶sub _get {
Subroutine _get: called CC=1 SCAR=0.0
189 14 my ($self, $url) = @_;
190  
191 14 my $response = $self->{ua}->get($url);
192 14 return $self->_handle_response($response);
193 }
194  
195 1 ▶sub _post {
Subroutine _post: called CC=1 SCAR=0.0
196 1 my ($self, $url, $data) = @_;
197  
198 1 my $body = encode_json($data);
199 1 my $response = $self->{ua}->post($url, { content => $body });
200 1 return $self->_handle_response($response);
201 }
202  
203 15 ▶sub _handle_response {
Subroutine _handle_response: called CC=8 SCAR=24.8
204 15 my ($self, $response) = @_;
205  
206 15 my $status = $response->{status};
207 15 ▶ my $content = $response->{content} // '';
Condition50%1/2
$response->{"content"} // ""
l!l
150
MC/DC0%0/1
$response->{"content"} // ""
$response->{"content"}
Truth table50%1/2
A
A= $response->{"content"}
Aresult
11
00
208  
209 # Network-level failures from HTTP::Tiny (status 599)
210 15 ▶ if ($status == 599) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status == 599) 1 14 15
211 1 croak "Network error: $content";
212 }
213  
214 # Try to decode JSON
215 14 my $data;
216 14 eval { $data = decode_json($content); };
217 14 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 14 14
218 # Non-JSON response (e.g., single field value like country_name)
219 # The API returns bare strings for single-field path lookups
220 0 $content =~ s/^\s+|\s+$//g; # trim whitespace
221 0 $content =~ s/^"|"$//g; # strip surrounding quotes
222  
223 0 ▶ if ($status >= 200 && $status < 300) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status >= 200 and $status < 300) 0 0 0
Condition0%0/3
$status >= 200 and $status < 300
!ll&&!rl&&r
000
MC/DC0%0/2
$status >= 200 and $status < 300
$status >= 200 $status < 300
Truth table0%0/3
A and B
A= $status >= 200B= $status < 300
ABresult
0X0
100
111
224 0 return $content;
225 }
226 0 croak "HTTP $status: $content";
227 }
228  
229 # API error responses include a "message" field
230 14 ▶ if ($status >= 400) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status >= 400) 6 8 14
231 6 ▶ my $message = $data->{message} // "HTTP $status error";
Condition50%1/2
$data->{"message"} // "HTTP $status error"
l!l
60
MC/DC0%0/1
$data->{"message"} // "HTTP $status error"
$data->{"message"}
Truth table50%1/2
A
A= $data->{"message"}
Aresult
11
00
232 6 croak "API error ($status): $message";
233 }
234  
235 8 return $data;
236 }
237  
238 5 ▶sub _validate_fields {
Subroutine _validate_fields: called CC=3 SCAR=11.0
239 5 my ($fields) = @_;
240 5 for my $f (@$fields) {
241 10 ▶ croak "Invalid field: $f" unless $VALID_FIELDS{$f};
Branch: 1 branch
BranchTrueFalseTotal
unless $VALID_FIELDS{$f} 2 8 10
242 }
243 }
244  
245 3 1;
246  
247 __END__
↑ Summary