InternetData/Result.pm

filter: Statementstmt 100% 43 / 43 Branchbran 100% 12 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod 75.0% 3 / 4 totaltotal 98.5% 68 / 69 CC 9 SCAR 13.0
CC
9
Cov
100%
from_wire
7.0
has
3.0
BEGIN
1.0
CRAP 3.7
« InternetData/AsnResult.pm ↑ Summary InternetData/Database.pm »
1 package InternetData::Result;
2  
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 8 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 8 our $VERSION = '1.16.0';
9  
10 8 our @FLAGS = qw(
11 is_vpn is_tor is_relay is_resproxy is_dcproxy is_mobproxy is_pubproxy is_hosting is_cdn
12 is_anycast is_carrier is_satellite is_ssh is_smtp is_web is_dns_resolver is_bittorrent
13 );
14 # Each flag's detail: an object, or for Web IP a list of the web servers found.
15 8 our @DETAILS = qw(
16 vpn tor relay resproxy dcproxy mobproxy pubproxy hosting cdn carrier satellite ssh smtp web
17 dns_resolver bittorrent
18 );
19 # Where the address is and who holds it, copied as served: objects, and the
20 # reverse-DNS hostname's string.
21 8 our @OBJECTS = qw(location asn company hostname abuse whois rwhois hosted_domains);
22 # The members of each shared object. Those the spec does not require are paid,
23 # and absent from an answer whose plan leaves them out.
24 8 our %MEMBERS = (
25 location => [qw(
26 continent continent_name country country_name timezone currency_code currency_name
27 currency_symbol flag_emoji flag_unicode region region_code city postal_code latitude
28 longitude accuracy_radius geoname_id dma_code last_changed
29 )],
30 asn => [qw(asn name domain type prefix country rpki_status)],
31 company => [qw(name domain type country)],
32 abuse => [qw(email name phone address country network validated unvalidated_since)],
33 whois => [qw(id name org country status source domain abuse_email asn)],
34 rwhois => [qw(id name country source domain abuse_email)],
35 );
36 # On answers to a key on the free plan only, and so never on a bogon's: false
37 # while its organization's daily trial lasts (every field for the first 100
38 # lookups each UTC day), true once the answer has Unlimited's. cap_reached is
39 # its paid-plan twin: false while the answer has every field, true once the
40 # organization's overage cap is reached.
41 8 our @MARKERS = qw(trial_expired cap_reached);
42  
43 8 my %KNOWN = map { $_ => 1 } 'ip', 'is_bogon', @FLAGS, @DETAILS, @OBJECTS, @MARKERS;
44  
45 # The one place a wire body becomes a result. Every field is copied on key
46 # PRESENCE and never on truthiness: a plan that includes `is_hosting` and
47 # answers false must keep it, and a plan that does not include it must not gain
48 # it. In Perl those two are one careless `if` apart, since undef, 0, '' and a
49 # missing key are all false.
50 27573 ▶sub from_wire {
Subroutine from_wire: called CC=7 SCAR=19.5
Pod undocumented
51 27573 my ($class, $body) = @_;
52 27573 my %answer = (ip => $body->{ip});
53 # A served null stays undef, as a detail's does: read as 0 it would say the
54 # address is no hosting server, or that the trial lasts, when nothing was said.
55 27573 for my $flag ('is_bogon', @FLAGS, @MARKERS) {
56 551460 ▶ next unless exists $body->{$flag};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $body->{$flag} 248128 303332 551460
57 303332 ▶ $answer{$flag} = defined $body->{$flag} ? ($body->{$flag} ? 1 : 0) : undef;
Branch: 2 branches
BranchTrueFalseTotal
defined $body->{$flag} ? : 303330 2 303332
$body->{$flag} ? : 33 303297 303330
58 }
59 27573 for my $detail (@DETAILS, @OBJECTS) {
60 661752 ▶ $answer{$detail} = $body->{$detail} if exists $body->{$detail};
Branch: 1 branch
BranchTrueFalseTotal
if exists $body->{$detail} 193069 468683 661752
61 }
62 27573 return $class->_new(\%answer, $body);
63 }
64  
65 # Whether your plan carries this field at all, which is a different question
66 # from what the field says. `has` is the only honest answer to "is this address
67 # hosting?" when the field may not have been served: absent is not false.
68 316 ▶sub has {
Subroutine has: called CC=3 SCAR=11.0
Pod documented
69 316 my ($self, $field) = @_;
70 316 ▶ Carp::croak("InternetData::Result::has: unknown field '$field'") unless $KNOWN{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless $KNOWN{$field} 1 315 316
71 315 ▶ return exists $self->{$field} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{$field} ? : 234 81 315
72 }
73  
74 # The fields this answer actually carried, in wire order. Useful for seeing what
75 # a key is entitled to without reading the plan table.
76 1 ▶sub fields {
Subroutine fields: called CC=1 SCAR=0.0
Pod documented
77 1 my ($self) = @_;
78 1 return grep { exists $self->{$_} } 'is_bogon', @FLAGS, @DETAILS, @OBJECTS, @MARKERS;
79 }
80  
81 # The response exactly as it came off the wire, with its original names and its
82 # JSON booleans intact. Empty for a bogon, which was never served.
83 2 ▶sub raw {
Subroutine raw: called CC=1 SCAR=0.0
Pod documented
84 2 return $_[0]->{_raw};
85 }
86  
87 27579 ▶sub _new {
Subroutine _new: called CC=1 SCAR=0.0
88 27579 my ($class, $answer, $raw) = @_;
89 27579 $answer->{_raw} = $raw;
90 27579 return bless $answer, $class;
91 }
92  
93 # One reader per field rather than AUTOLOAD, so a typo is a compile-adjacent
94 # "Can't locate object method" rather than a silent undef.
95 8 for my $field ('ip', 'is_bogon', @FLAGS, @DETAILS, @OBJECTS, @MARKERS) {
96 8 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
97 3179 ▶ *{__PACKAGE__ . "::$field"} = sub { $_[0]->{$field} };
Subroutine __ANON__: called CC=1 SCAR=0.0
98 }
99  
100 8 1;
101  
102 __END__
« InternetData/AsnResult.pm ↑ Summary InternetData/Database.pm »