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.15.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.
39 8 our @MARKERS = qw(trial_expired);
40  
41 8 my %KNOWN = map { $_ => 1 } 'ip', 'is_bogon', @FLAGS, @DETAILS, @OBJECTS, @MARKERS;
42  
43 # The one place a wire body becomes a result. Every field is copied on key
44 # PRESENCE and never on truthiness: a plan that includes `is_hosting` and
45 # answers false must keep it, and a plan that does not include it must not gain
46 # it. In Perl those two are one careless `if` apart, since undef, 0, '' and a
47 # missing key are all false.
48 27572 ▶sub from_wire {
Subroutine from_wire: called CC=7 SCAR=19.5
Pod undocumented
49 27572 my ($class, $body) = @_;
50 27572 my %answer = (ip => $body->{ip});
51 # A served null stays undef, as a detail's does: read as 0 it would say the
52 # address is no hosting server, or that the trial lasts, when nothing was said.
53 27572 for my $flag ('is_bogon', @FLAGS, @MARKERS) {
54 523868 ▶ next unless exists $body->{$flag};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $body->{$flag} 220550 303318 523868
55 303318 ▶ $answer{$flag} = defined $body->{$flag} ? ($body->{$flag} ? 1 : 0) : undef;
Branch: 2 branches
BranchTrueFalseTotal
defined $body->{$flag} ? : 303316 2 303318
$body->{$flag} ? : 32 303284 303316
56 }
57 27572 for my $detail (@DETAILS, @OBJECTS) {
58 661728 ▶ $answer{$detail} = $body->{$detail} if exists $body->{$detail};
Branch: 1 branch
BranchTrueFalseTotal
if exists $body->{$detail} 193062 468666 661728
59 }
60 27572 return $class->_new(\%answer, $body);
61 }
62  
63 # Whether your plan carries this field at all, which is a different question
64 # from what the field says. `has` is the only honest answer to "is this address
65 # hosting?" when the field may not have been served: absent is not false.
66 266 ▶sub has {
Subroutine has: called CC=3 SCAR=11.0
Pod documented
67 266 my ($self, $field) = @_;
68 266 ▶ Carp::croak("InternetData::Result::has: unknown field '$field'") unless $KNOWN{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless $KNOWN{$field} 1 265 266
69 265 ▶ return exists $self->{$field} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{$field} ? : 212 53 265
70 }
71  
72 # The fields this answer actually carried, in wire order. Useful for seeing what
73 # a key is entitled to without reading the plan table.
74 1 ▶sub fields {
Subroutine fields: called CC=1 SCAR=0.0
Pod documented
75 1 my ($self) = @_;
76 1 return grep { exists $self->{$_} } 'is_bogon', @FLAGS, @DETAILS, @OBJECTS, @MARKERS;
77 }
78  
79 # The response exactly as it came off the wire, with its original names and its
80 # JSON booleans intact. Empty for a bogon, which was never served.
81 2 ▶sub raw {
Subroutine raw: called CC=1 SCAR=0.0
Pod documented
82 2 return $_[0]->{_raw};
83 }
84  
85 27578 ▶sub _new {
Subroutine _new: called CC=1 SCAR=0.0
86 27578 my ($class, $answer, $raw) = @_;
87 27578 $answer->{_raw} = $raw;
88 27578 return bless $answer, $class;
89 }
90  
91 # One reader per field rather than AUTOLOAD, so a typo is a compile-adjacent
92 # "Can't locate object method" rather than a silent undef.
93 8 for my $field ('ip', 'is_bogon', @FLAGS, @DETAILS, @OBJECTS, @MARKERS) {
94 8 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
95 3084 ▶ *{__PACKAGE__ . "::$field"} = sub { $_[0]->{$field} };
Subroutine __ANON__: called CC=1 SCAR=0.0
96 }
97  
98 8 1;
99  
100 __END__
« InternetData/AsnResult.pm ↑ Summary InternetData/Database.pm »