| 1 | package Net::IPData; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use 5.010001; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 4 | our $VERSION = '1.1.0'; | |||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use HTTP::Tiny; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use JSON::PP qw(encode_json); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 4 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 4 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | # API base URLs | ||||||||||||||||||||||||||||||||||||||||||
| 15 | 4 | ▶ | use constant { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 4 | BASE_URL => 'https://api.ipdata.co', | |||||||||||||||||||||||||||||||||||||||||
| 17 | EU_BASE_URL => 'https://eu-api.ipdata.co', | ||||||||||||||||||||||||||||||||||||||||||
| 18 | MAX_BULK => 100, | ||||||||||||||||||||||||||||||||||||||||||
| 19 | 4 | }; | |||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | # Single-field lookups (/{ip}/{field}) answer with a bare JSON value: a | ||||||||||||||||||||||||||||||||||||||||||
| 22 | # string, a number, true, false or null. JSON::PP before 4.0 rejects such a | ||||||||||||||||||||||||||||||||||||||||||
| 23 | # top-level scalar unless allow_nonref is set, hence one configured decoder | ||||||||||||||||||||||||||||||||||||||||||
| 24 | # instead of decode_json. | ||||||||||||||||||||||||||||||||||||||||||
| 25 | 4 | my $JSON = JSON::PP->new->utf8->allow_nonref; | |||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | # Valid top-level fields for filtering | ||||||||||||||||||||||||||||||||||||||||||
| 28 | 4 | my %VALID_FIELDS = map { $_ => 1 } qw( | |||||||||||||||||||||||||||||||||||||||||
| 29 | ip is_eu is_satellite is_anycast city region region_code region_type | ||||||||||||||||||||||||||||||||||||||||||
| 30 | country_name country_code continent_name continent_code | ||||||||||||||||||||||||||||||||||||||||||
| 31 | latitude longitude postal calling_code flag | ||||||||||||||||||||||||||||||||||||||||||
| 32 | emoji_flag emoji_unicode asn company carrier | ||||||||||||||||||||||||||||||||||||||||||
| 33 | languages currency time_zone threat count status message | ||||||||||||||||||||||||||||||||||||||||||
| 34 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | # Valid single-field path lookups (fields accessible via /{ip}/{field}) | ||||||||||||||||||||||||||||||||||||||||||
| 37 | 4 | my %PATH_FIELDS = map { $_ => 1 } qw( | |||||||||||||||||||||||||||||||||||||||||
| 38 | ip is_eu is_satellite is_anycast city region region_code region_type | ||||||||||||||||||||||||||||||||||||||||||
| 39 | country_name country_code continent_name continent_code | ||||||||||||||||||||||||||||||||||||||||||
| 40 | latitude longitude postal calling_code flag | ||||||||||||||||||||||||||||||||||||||||||
| 41 | emoji_flag emoji_unicode asn carrier languages | ||||||||||||||||||||||||||||||||||||||||||
| 42 | currency time_zone threat | ||||||||||||||||||||||||||||||||||||||||||
| 43 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | 17 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=16 SCAR=27.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 17 | my ($class, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | my $api_key = $args{api_key} | ||||||||||||||||||||||||||||||||||||||||||
| 49 | 17 | ▶ | or croak 'Required parameter "api_key" is missing'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | 15 | my $base_url = $args{base_url}; | |||||||||||||||||||||||||||||||||||||||||
| 52 | 15 | ▶ | if ($args{eu} && !$base_url) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$args{"eu"} and not $base_url
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
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | 1 | $base_url = EU_BASE_URL; | |||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||
| 55 | 15 | ▶ | $base_url //= BASE_URL; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$base_url //= "https://api.ipdata.co"
MC/DC100%1/1
$base_url //= "https://api.ipdata.co"
$base_url
Truth table100%2/2
A
A= $base_url
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 15 | $base_url =~ s{/+$}{}; # strip trailing slashes | |||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | 15 | ▶ | my $timeout = $args{timeout} // 30; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$args{"timeout"} // 30
MC/DC100%1/1
$args{"timeout"} // 30
$args{"timeout"}
Truth table100%2/2
A
A= $args{"timeout"}
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 15 | ▶ | my $keep_alive = $args{keep_alive} // 1; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$args{"keep_alive"} // 1
MC/DC100%1/1
$args{"keep_alive"} // 1
$args{"keep_alive"}
Truth table100%2/2
A
A= $args{"keep_alive"}
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 15 | my $keep_alive_timeout = $args{keep_alive_timeout}; | |||||||||||||||||||||||||||||||||||||||||
| 61 | 15 | ▶ | my $verify_ssl = ($args{verify_ssl} // 1) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$args{"verify_ssl"} // 1
MC/DC100%1/1
$args{"verify_ssl"} // 1
$args{"verify_ssl"}
Truth table100%2/2
A
A= $args{"verify_ssl"}
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | # One user agent per Net::IPData object: it owns the kept-alive HTTPS | ||||||||||||||||||||||||||||||||||||||||||
| 64 | # connection, so building it once here (and never per request) is what | ||||||||||||||||||||||||||||||||||||||||||
| 65 | # amortises the TCP+TLS handshake across lookups. | ||||||||||||||||||||||||||||||||||||||||||
| 66 | 15 | my $ua = $args{ua}; | |||||||||||||||||||||||||||||||||||||||||
| 67 | 15 | ▶ | if (defined $ua) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | 5 | ▶ | croak 'Parameter "ua" must be an object with "get" and "post" methods' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
builtin::blessed($ua) and $ua->can("get") and $ua->can("post")
builtin::blessed($ua) and $ua->can("get")
MC/DC33%1/3
builtin::blessed($ua) and $ua->can("get") and $ua->can("post")
builtin::blessed($ua)
$ua->can("get")
$ua->can("post")
Truth table50%2/4
A and B and C
A= builtin::blessed($ua)B= $ua->can("get")C= $ua->can("post")
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | unless blessed($ua) && $ua->can('get') && $ua->can('post'); | ||||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||
| 71 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 72 | 10 | ▶ | $ua = HTTP::Tiny->new( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | agent => "ipdata-perl/$VERSION", | ||||||||||||||||||||||||||||||||||||||||||
| 74 | timeout => $timeout, | ||||||||||||||||||||||||||||||||||||||||||
| 75 | keep_alive => $keep_alive, | ||||||||||||||||||||||||||||||||||||||||||
| 76 | # Always passed explicitly: HTTP::Tiny before 0.083 verifies the | ||||||||||||||||||||||||||||||||||||||||||
| 77 | # server certificate only when asked to, and from 0.083 on its | ||||||||||||||||||||||||||||||||||||||||||
| 78 | # default can be switched off by an environment variable. | ||||||||||||||||||||||||||||||||||||||||||
| 79 | verify_SSL => $verify_ssl, | ||||||||||||||||||||||||||||||||||||||||||
| 80 | # Forwarded only when the caller asked for one, so HTTP::Tiny's | ||||||||||||||||||||||||||||||||||||||||||
| 81 | # own default (no idle limit) applies otherwise; HTTP::Tiny | ||||||||||||||||||||||||||||||||||||||||||
| 82 | # before 0.092 has no such setting and ignores the key. | ||||||||||||||||||||||||||||||||||||||||||
| 83 | (defined $keep_alive_timeout | ||||||||||||||||||||||||||||||||||||||||||
| 84 | ? (keep_alive_timeout => $keep_alive_timeout) | ||||||||||||||||||||||||||||||||||||||||||
| 85 | : ()), | ||||||||||||||||||||||||||||||||||||||||||
| 86 | # Content-Type belongs to the one request with a body, the bulk | ||||||||||||||||||||||||||||||||||||||||||
| 87 | # POST, which adds it itself (see _post_options). | ||||||||||||||||||||||||||||||||||||||||||
| 88 | default_headers => { Accept => 'application/json' }, | ||||||||||||||||||||||||||||||||||||||||||
| 89 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 90 | # HTTP::Tiny 0.092 and later document keep_alive_timeout as a | ||||||||||||||||||||||||||||||||||||||||||
| 91 | # constructor attribute, but their constructor (checked up to 0.096) | ||||||||||||||||||||||||||||||||||||||||||
| 92 | # never copies the argument into the object, so set the attribute | ||||||||||||||||||||||||||||||||||||||||||
| 93 | # directly where it exists. | ||||||||||||||||||||||||||||||||||||||||||
| 94 | 10 | ▶ | if (defined $keep_alive_timeout && eval { HTTP::Tiny->VERSION('0.092') }) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $keep_alive_timeout and eval {
do {
"HTTP::Tiny"->VERSION("0.092")
}
}
MC/DC50%1/2
defined $keep_alive_timeout and eval {
do {
"HTTP::Tiny"->VERSION("0.092")
}
}
defined $keep_alive_timeout
eval {
do {
"HTTP::Tiny"->VERSION("0.092")
}
}
Truth table66%2/3
A and B
A= defined $keep_alive_timeoutB= eval {
do {
"HTTP::Tiny"->VERSION("0.092")
}
}
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | 1 | $ua->{keep_alive_timeout} = $keep_alive_timeout; | |||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | 13 | return bless { | |||||||||||||||||||||||||||||||||||||||||
| 100 | api_key => $api_key, | ||||||||||||||||||||||||||||||||||||||||||
| 101 | base_url => $base_url, | ||||||||||||||||||||||||||||||||||||||||||
| 102 | ua => $ua, | ||||||||||||||||||||||||||||||||||||||||||
| 103 | timeout => $timeout, | ||||||||||||||||||||||||||||||||||||||||||
| 104 | keep_alive => $keep_alive, | ||||||||||||||||||||||||||||||||||||||||||
| 105 | keep_alive_timeout => $keep_alive_timeout, | ||||||||||||||||||||||||||||||||||||||||||
| 106 | verify_ssl => $verify_ssl, | ||||||||||||||||||||||||||||||||||||||||||
| 107 | }, $class; | ||||||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | # --- Public Methods --- | ||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | 18 | ▶ | sub lookup { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
lookup: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | 18 | my ($self, $ip, %opts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | 18 | ▶ | my $path = defined $ip && length $ip ? "/$ip" : ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $ip and length $ip
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
| |||||||||||||||||||||||||||||||||||||||||||
| 116 | 18 | my @params; | |||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | 18 | ▶ | if (my $fields = $opts{fields}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | 6 | ▶ | my @field_list = ref $fields eq 'ARRAY' ? @$fields : split(/\s*,\s*/, $fields); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | 6 | _validate_fields(\@field_list); | |||||||||||||||||||||||||||||||||||||||||
| 121 | 4 | push @params, 'fields=' . join(',', @field_list); | |||||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | 16 | my $url = $self->_build_url($path, @params); | |||||||||||||||||||||||||||||||||||||||||
| 125 | 16 | return $self->_get($url); | |||||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | 1 | ▶ | sub lookup_mine { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
lookup_mine: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 129 | 1 | my ($self, %opts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 130 | 1 | return $self->lookup(undef, %opts); | |||||||||||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | 21 | ▶ | sub lookup_field { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
lookup_field: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 134 | 21 | my ($self, $ip, $field) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | 21 | ▶ | croak 'Required parameter "ip" is missing' unless defined $ip && length $ip; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $ip and length $ip
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
| |||||||||||||||||||||||||||||||||||||||||||
| 137 | 20 | ▶ | croak 'Required parameter "field" is missing' unless defined $field && length $field; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $field and length $field
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
| |||||||||||||||||||||||||||||||||||||||||||
| 138 | 19 | ▶ | croak "Invalid field: $field" unless $PATH_FIELDS{$field}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||
| 140 | 16 | my $url = $self->_build_url("/$ip/$field"); | |||||||||||||||||||||||||||||||||||||||||
| 141 | 16 | return $self->_get($url); | |||||||||||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||
| 144 | 14 | ▶ | sub bulk { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
bulk: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 145 | 14 | my ($self, $ips, %opts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||
| 147 | 14 | ▶ | croak 'Required parameter "ips" must be an array reference' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 148 | unless ref $ips eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||
| 149 | 13 | ▶ | croak 'Bulk lookup supports a maximum of ' . MAX_BULK . ' IP addresses' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 150 | if @$ips > MAX_BULK; | ||||||||||||||||||||||||||||||||||||||||||
| 151 | 12 | ▶ | croak 'Bulk lookup requires at least one IP address' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 152 | if @$ips == 0; | ||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||
| 154 | 11 | my @params; | |||||||||||||||||||||||||||||||||||||||||
| 155 | 11 | ▶ | if (my $fields = $opts{fields}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 156 | 2 | ▶ | my @field_list = ref $fields eq 'ARRAY' ? @$fields : split(/\s*,\s*/, $fields); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 157 | 2 | _validate_fields(\@field_list); | |||||||||||||||||||||||||||||||||||||||||
| 158 | 1 | push @params, 'fields=' . join(',', @field_list); | |||||||||||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||
| 161 | 10 | my $url = $self->_build_url('/bulk', @params); | |||||||||||||||||||||||||||||||||||||||||
| 162 | 10 | return $self->_post($url, $ips); | |||||||||||||||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||
| 165 | 0 | ▶ | sub asn { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
asn: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 166 | 0 | my ($self, $ip) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 167 | 0 | return $self->lookup_field($ip, 'asn'); | |||||||||||||||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||
| 170 | 1 | ▶ | sub threat { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
threat: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 171 | 1 | my ($self, $ip) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 172 | 1 | return $self->lookup_field($ip, 'threat'); | |||||||||||||||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||
| 175 | 0 | ▶ | sub carrier { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
carrier: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 176 | 0 | my ($self, $ip) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 177 | 0 | return $self->lookup_field($ip, 'carrier'); | |||||||||||||||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||
| 180 | 1 | ▶ | sub currency { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
currency: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 181 | 1 | my ($self, $ip) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 182 | 1 | return $self->lookup_field($ip, 'currency'); | |||||||||||||||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||
| 185 | 0 | ▶ | sub time_zone { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
time_zone: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 186 | 0 | my ($self, $ip) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 187 | 0 | return $self->lookup_field($ip, 'time_zone'); | |||||||||||||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||
| 190 | 0 | ▶ | sub languages { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
languages: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 191 | 0 | my ($self, $ip) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 192 | 0 | return $self->lookup_field($ip, 'languages'); | |||||||||||||||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||
| 195 | 0 | ▶ | sub country_name { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
country_name: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 196 | 0 | my ($self, $ip) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 197 | 0 | return $self->lookup_field($ip, 'country_name'); | |||||||||||||||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||
| 200 | 0 | ▶ | sub country_code { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
country_code: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 201 | 0 | my ($self, $ip) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 202 | 0 | return $self->lookup_field($ip, 'country_code'); | |||||||||||||||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||
| 205 | 2 | ▶ | sub is_eu { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_eu: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 206 | 2 | my ($self, $ip) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 207 | 2 | return $self->lookup_field($ip, 'is_eu'); | |||||||||||||||||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||||
| 210 | 1 | ▶ | sub is_satellite { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_satellite: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 211 | 1 | my ($self, $ip) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 212 | 1 | return $self->lookup_field($ip, 'is_satellite'); | |||||||||||||||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||||||
| 215 | 1 | ▶ | sub is_anycast { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_anycast: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 216 | 1 | my ($self, $ip) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 217 | 1 | return $self->lookup_field($ip, 'is_anycast'); | |||||||||||||||||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||
| 220 | # --- Accessors --- | ||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||
| 222 | 1 | ▶ | sub api_key { return $_[0]->{api_key} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
api_key: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 223 | 4 | ▶ | sub base_url { return $_[0]->{base_url} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
base_url: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 224 | 2 | ▶ | sub timeout { return $_[0]->{timeout} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
timeout: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 225 | 4 | ▶ | sub keep_alive { return $_[0]->{keep_alive} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
keep_alive: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 226 | 2 | ▶ | sub keep_alive_timeout { return $_[0]->{keep_alive_timeout} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
keep_alive_timeout: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 227 | 3 | ▶ | sub verify_ssl { return $_[0]->{verify_ssl} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
verify_ssl: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 228 | 16 | ▶ | sub ua { return $_[0]->{ua} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ua: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||||||
| 230 | # --- Private Methods --- | ||||||||||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||||||||
| 232 | 42 | ▶ | sub _build_url { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_url: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||
| 233 | 42 | my ($self, $path, @params) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||||
| 235 | 42 | unshift @params, 'api-key=' . $self->{api_key}; | |||||||||||||||||||||||||||||||||||||||||
| 236 | 42 | my $query = join('&', @params); | |||||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||||||
| 238 | 42 | ▶ | return $self->{base_url} . ($path // '') . '?' . $query; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$path // ""
MC/DC0%0/1
$path // ""
$path
Truth table50%1/2
A
A= $path
| |||||||||||||||||||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||
| 241 | 32 | ▶ | sub _get { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 242 | 32 | my ($self, $url) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||
| 244 | 32 | my $response = $self->{ua}->get($url); | |||||||||||||||||||||||||||||||||||||||||
| 245 | 32 | return $self->_handle_response($response); | |||||||||||||||||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||
| 248 | 10 | ▶ | sub _post { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_post: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 249 | 10 | my ($self, $url, $data) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||
| 251 | 10 | my $body = encode_json($data); | |||||||||||||||||||||||||||||||||||||||||
| 252 | 10 | my $response = $self->{ua}->post($url, _post_options($body)); | |||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||
| 254 | # A kept-alive connection can be closed by the server while idle. | ||||||||||||||||||||||||||||||||||||||||||
| 255 | # HTTP::Tiny retries idempotent methods on such a socket itself but never | ||||||||||||||||||||||||||||||||||||||||||
| 256 | # a POST, so retry exactly once when the failure was at the socket level | ||||||||||||||||||||||||||||||||||||||||||
| 257 | # (status 599) rather than an HTTP status. HTTP::Tiny has already dropped | ||||||||||||||||||||||||||||||||||||||||||
| 258 | # the dead connection by then, so the retry opens a fresh one. | ||||||||||||||||||||||||||||||||||||||||||
| 259 | 10 | ▶ | if (_is_stale_connection_error($response)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 260 | 4 | $response = $self->{ua}->post($url, _post_options($body)); | |||||||||||||||||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||||
| 263 | 10 | return $self->_handle_response($response); | |||||||||||||||||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||||||||||
| 266 | # Options for the bulk POST: the JSON body and its Content-Type, which only | ||||||||||||||||||||||||||||||||||||||||||
| 267 | # a request with a body sends. A fresh hashref per attempt, as HTTP::Tiny | ||||||||||||||||||||||||||||||||||||||||||
| 268 | # adds bookkeeping keys to the one it is given. | ||||||||||||||||||||||||||||||||||||||||||
| 269 | 14 | ▶ | sub _post_options { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_post_options: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 270 | 14 | my ($body) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 271 | 14 | return { content => $body, headers => { 'Content-Type' => 'application/json' } }; | |||||||||||||||||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||||||||
| 274 | # True for the status-599 pseudo-responses HTTP::Tiny returns when a reused | ||||||||||||||||||||||||||||||||||||||||||
| 275 | # socket turned out to be dead; never true for a real HTTP status. | ||||||||||||||||||||||||||||||||||||||||||
| 276 | 10 | ▶ | sub _is_stale_connection_error { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_is_stale_connection_error: called CC=5 SCAR=16.3
| |||||||||||||||||||||||||||||||||||||||||||
| 277 | 10 | my ($response) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||||||||||
| 279 | 10 | ▶ | return 0 unless ($response->{status} // 0) == 599; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$response->{"status"} // 0
MC/DC0%0/1
$response->{"status"} // 0
$response->{"status"}
Truth table50%1/2
A
A= $response->{"status"}
| |||||||||||||||||||||||||||||||||||||||||||
| 280 | 5 | ▶ | my $content = $response->{content} // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$response->{"content"} // ""
MC/DC0%0/1
$response->{"content"} // ""
$response->{"content"}
Truth table50%1/2
A
A= $response->{"content"}
| |||||||||||||||||||||||||||||||||||||||||||
| 281 | 5 | ▶ | return $content =~ /Socket closed|Unexpected end of stream|Connection reset/ ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||||||
| 284 | 42 | ▶ | sub _handle_response { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_handle_response: called CC=8 SCAR=20.9
| |||||||||||||||||||||||||||||||||||||||||||
| 285 | 42 | my ($self, $response) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||||||||||
| 287 | 42 | my $status = $response->{status}; | |||||||||||||||||||||||||||||||||||||||||
| 288 | 42 | ▶ | my $content = $response->{content} // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$response->{"content"} // ""
MC/DC0%0/1
$response->{"content"} // ""
$response->{"content"}
Truth table50%1/2
A
A= $response->{"content"}
| |||||||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||||||
| 290 | # Network-level failures from HTTP::Tiny (status 599) | ||||||||||||||||||||||||||||||||||||||||||
| 291 | 42 | ▶ | if ($status == 599) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 292 | 4 | croak "Network error: $content"; | |||||||||||||||||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||||||||
| 295 | # Decode the JSON, including the bare scalar a single-field lookup returns | ||||||||||||||||||||||||||||||||||||||||||
| 296 | 38 | my $data; | |||||||||||||||||||||||||||||||||||||||||
| 297 | 38 | eval { $data = $JSON->decode($content); }; | |||||||||||||||||||||||||||||||||||||||||
| 298 | 38 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 299 | # Not JSON at all (for example an HTML error page): return or report | ||||||||||||||||||||||||||||||||||||||||||
| 300 | # the trimmed text | ||||||||||||||||||||||||||||||||||||||||||
| 301 | 2 | $content =~ s/^\s+|\s+$//g; # trim whitespace | |||||||||||||||||||||||||||||||||||||||||
| 302 | 2 | $content =~ s/^"|"$//g; # strip surrounding quotes | |||||||||||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||||||||||
| 304 | 2 | ▶ | if ($status >= 200 && $status < 300) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$status >= 200 and $status < 300
MC/DC50%1/2
$status >= 200 and $status < 300
$status >= 200
$status < 300
Truth table66%2/3
A and B
A= $status >= 200B= $status < 300
| |||||||||||||||||||||||||||||||||||||||||||
| 305 | 1 | return $content; | |||||||||||||||||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||||||||||||||||
| 307 | 1 | croak "HTTP $status: $content"; | |||||||||||||||||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||||||||||
| 310 | # API error responses include a "message" field | ||||||||||||||||||||||||||||||||||||||||||
| 311 | 36 | ▶ | if ($status >= 400) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 312 | 4 | ▶ | my $message = $data->{message} // "HTTP $status error"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$data->{"message"} // "HTTP $status error"
MC/DC0%0/1
$data->{"message"} // "HTTP $status error"
$data->{"message"}
Truth table50%1/2
A
A= $data->{"message"}
| |||||||||||||||||||||||||||||||||||||||||||
| 313 | 4 | croak "API error ($status): $message"; | |||||||||||||||||||||||||||||||||||||||||
| 314 | } | ||||||||||||||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||||||||||||
| 316 | 32 | return $data; | |||||||||||||||||||||||||||||||||||||||||
| 317 | } | ||||||||||||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||||||||||||
| 319 | 8 | ▶ | sub _validate_fields { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_validate_fields: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 320 | 8 | my ($fields) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 321 | 8 | for my $f (@$fields) { | |||||||||||||||||||||||||||||||||||||||||
| 322 | 16 | ▶ | croak "Invalid field: $f" unless $VALID_FIELDS{$f}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 323 | } | ||||||||||||||||||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||||||||||
| 326 | 4 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||||||
| 328 | __END__ | ||||||||||||||||||||||||||||||||||||||||||