VPNDetection/Result.pm

filter: Statementstmt 100% 44 / 44 Branchbran 100% 14 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod 75.0% 3 / 4 totaltotal 98.6% 71 / 72 CC 10 SCAR 14.5
CC
10
Cov
100%
from_wire
8.0
has
3.0
BEGIN
1.0
CRAP 4.3
« VPNDetection/Error.pm ↑ Summary VPNDetection/Cache.pm »
1 package VPNDetection::Result;
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 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 7 our $VERSION = '3.16.0';
9  
10 7 our @FLAGS = qw(
11 is_vpn is_hosting is_relay is_tor is_cdn is_resproxy is_dcproxy is_mobproxy
12 is_pubproxy
13 );
14 7 our @DETAILS = qw(vpn hosting relay tor cdn resproxy dcproxy mobproxy pubproxy);
15 # Where the address is and the AS announcing it, on every answer, with or without
16 # a key, each with every member present: an empty string, or AS 0, where nothing
17 # is known.
18 7 our @OBJECTS = qw(location asn);
19 7 our @LOCATION = qw(
20 continent continent_name country country_name timezone currency_code currency_name
21 currency_symbol flag_emoji flag_unicode
22 );
23 7 our @ASN = qw(asn name domain type prefix);
24 # On answers to a key on the free plan only, and so never on a bogon's: false
25 # while its organization's daily trial lasts (every field for the first 100
26 # lookups each UTC day), true once the answer has Unlimited's. cap_reached is the
27 # same on a paid plan's answers: false while the answer has every field, true
28 # once its organization's overage cap is reached.
29 7 our @MARKERS = qw(trial_expired cap_reached);
30  
31 7 my %KNOWN = map { $_ => 1 } 'ip', 'is_bogon', @FLAGS, @DETAILS, @OBJECTS, @MARKERS;
32  
33 # The one place a wire body becomes a result. Every field is copied on key
34 # PRESENCE and never on truthiness: a plan that includes `is_hosting` and
35 # answers false must keep it, and a plan that does not include it must not gain
36 # it. In Perl those two are one careless `if` apart, since undef, 0, '' and a
37 # missing key are all false.
38 27629 ▶sub from_wire {
Subroutine from_wire: called CC=8 SCAR=20.8
Pod undocumented
39 27629 my ($class, $body) = @_;
40 27629 ▶ my %answer = (ip => $body->{ip}, is_bogon => $body->{is_bogon} ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$body->{'is_bogon'} ? : 1 27628 27629
41 # A served null stays undef, as a detail's does: read as 0 it would say the
42 # address is no hosting server, or that the trial lasts, when nothing was said.
43 27629 for my $flag (@FLAGS, @MARKERS) {
44 303919 ▶ next unless exists $body->{$flag};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $body->{$flag} 276237 27682 303919
45 27682 ▶ $answer{$flag} = defined $body->{$flag} ? ($body->{$flag} ? 1 : 0) : undef;
Branch: 2 branches
BranchTrueFalseTotal
defined $body->{$flag} ? : 27680 2 27682
$body->{$flag} ? : 80 27600 27680
46 }
47 27629 for my $detail (@DETAILS, @OBJECTS) {
48 303919 ▶ $answer{$detail} = $body->{$detail} if exists $body->{$detail};
Branch: 1 branch
BranchTrueFalseTotal
if exists $body->{$detail} 432 303487 303919
49 }
50 27629 return $class->_new(\%answer, $body);
51 }
52  
53 # Whether your plan carries this field at all, which is a different question
54 # from what the field says. `has` is the only honest answer to "is this address
55 # hosting?" when the field may not have been served: absent is not false.
56 193 ▶sub has {
Subroutine has: called CC=3 SCAR=11.0
Pod documented
57 193 my ($self, $field) = @_;
58 193 ▶ Carp::croak("VPNDetection::Result::has: unknown field '$field'") unless $KNOWN{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless $KNOWN{$field} 1 192 193
59 192 ▶ return exists $self->{$field} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{$field} ? : 104 88 192
60 }
61  
62 # The fields this answer actually carried, in wire order. Useful for seeing what
63 # a key is entitled to without reading the plan table.
64 1 ▶sub fields {
Subroutine fields: called CC=1 SCAR=0.0
Pod documented
65 1 my ($self) = @_;
66 1 return grep { exists $self->{$_} } @FLAGS, @DETAILS, @OBJECTS, @MARKERS;
67 }
68  
69 # The response exactly as it came off the wire, with its original names and its
70 # JSON booleans intact. Empty for a bogon, which was never served.
71 2 ▶sub raw {
Subroutine raw: called CC=1 SCAR=0.0
Pod documented
72 2 return $_[0]->{_raw};
73 }
74  
75 27635 ▶sub _new {
Subroutine _new: called CC=1 SCAR=0.0
76 27635 my ($class, $answer, $raw) = @_;
77 27635 $answer->{_raw} = $raw;
78 27635 return bless $answer, $class;
79 }
80  
81 # One reader per field rather than AUTOLOAD, so a typo is a compile-adjacent
82 # "Can't locate object method" rather than a silent undef.
83 7 for my $field ('ip', 'is_bogon', @FLAGS, @DETAILS, @OBJECTS, @MARKERS) {
84 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
85 2798 ▶ *{__PACKAGE__ . "::$field"} = sub { $_[0]->{$field} };
Subroutine __ANON__: called CC=1 SCAR=0.0
86 }
87  
88 7 1;
89  
90 __END__
« VPNDetection/Error.pm ↑ Summary VPNDetection/Cache.pm »