Scrappy/Scraper/Control.pm

filter: Statementstmt 20.2% 14 / 69 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 60.0% 6 / 10 Podpod 100% 3 / 3 totaltotal 15.8% 23 / 145 CC 26 SCAR 44.4
CC
26
Cov
11%
is_allowed
182.0
allow
42.0
restrict
42.0
CRAP 84.5
« Scrappy/Scraper.pm ↑ Summary Scrappy.pm »
1 # ABSTRACT: Scrappy HTTP Request Constraints System
2 # Dist::Zilla: +PodWeaver
3  
4 package Scrappy::Scraper::Control;
5  
6 10 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 $Scrappy::Scraper::Control::VERSION = '0.94112090';
8 }
9  
10 # load OO System
11 10 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # load other libraries
14 10 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 ▶has 'allowed' => (is => 'rw', isa => 'HashRef', default => sub { {} });
Subroutine __ANON__: called CC=1 SCAR=0.0
17 has 'options' => (
18 is => 'ro',
19 isa => 'HashRef',
20 2 ▶ 'default' => sub { {} }
Subroutine __ANON__: called CC=1 SCAR=0.0
21 10 );
22 2 ▶has 'restricted' => (is => 'rw', isa => 'HashRef', default => sub { {} });
Subroutine __ANON__: called CC=1 SCAR=0.0
23  
24  
25 0 ▶sub allow {
Subroutine allow: not called CC=6 SCAR=37.4
Pod documented
26 0 my ($self, $target, %constraints) = @_;
27 0 my $i = 0;
28  
29 0 $target = URI->new($target);
30  
31 next
32 0 ▶ unless $target
Branch: 1 branch
BranchTrueFalseTotal
unless $target and "URI::http" eq ref $target || "URI::https" eq ref $target 0 0 0
Condition0%0/6
$target and "URI::http" eq ref $target || "URI::https" eq ref $target
!ll&&!rl&&r
000
"URI::http" eq ref $target or "URI::https" eq ref $target
l!l&&r!l&&!r
000
MC/DC0%0/3
$target and "URI::http" eq ref $target || "URI::https" eq ref $target
$target "URI::http" eq ref $target "URI::https" eq ref $target
Truth table0%0/4
A and B or C
A= $targetB= "URI::http" eq ref $targetC= "URI::https" eq ref $target
ABCresult
0XX0
1000
11X1
1011
33 && ("URI::http" eq ref $target || "URI::https" eq ref $target);
34  
35 0 $target = $target->host;
36  
37 delete $self->restricted->{$target}
38 0 ▶ if defined $self->restricted->{$target};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->restricted->{$target} 0 0 0
39 0 $self->allowed->{$target} = {%constraints};
40 0 ▶ $i++ if defined $target;
Branch: 1 branch
BranchTrueFalseTotal
if defined $target 0 0 0
41  
42 0 return $i;
43 }
44  
45  
46 0 ▶sub restrict {
Subroutine restrict: not called CC=6 SCAR=37.4
Pod documented
47 0 my ($self, $target, %constraints) = @_;
48 0 my $i = 0;
49  
50 0 $target = URI->new($target);
51  
52 next
53 0 ▶ unless $target
Branch: 1 branch
BranchTrueFalseTotal
unless $target and "URI::http" eq ref $target || "URI::https" eq ref $target 0 0 0
Condition0%0/6
$target and "URI::http" eq ref $target || "URI::https" eq ref $target
!ll&&!rl&&r
000
"URI::http" eq ref $target or "URI::https" eq ref $target
l!l&&r!l&&!r
000
MC/DC0%0/3
$target and "URI::http" eq ref $target || "URI::https" eq ref $target
$target "URI::http" eq ref $target "URI::https" eq ref $target
Truth table0%0/4
A and B or C
A= $targetB= "URI::http" eq ref $targetC= "URI::https" eq ref $target
ABCresult
0XX0
1000
11X1
1011
54 && ("URI::http" eq ref $target || "URI::https" eq ref $target);
55  
56 0 $target = $target->host;
57  
58 0 ▶ delete $self->allowed->{$target} if defined $self->allowed->{$target};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->allowed->{$target} 0 0 0
59 0 $self->restricted->{$target} = {%constraints};
60 0 ▶ $i++ if defined $target;
Branch: 1 branch
BranchTrueFalseTotal
if defined $target 0 0 0
61  
62 0 return $i;
63 }
64  
65  
66 0 ▶sub is_allowed {
Subroutine is_allowed: not called CC=13 SCAR=52.0
Pod documented
67 0 my $self = shift;
68 0 my $url = shift;
69 0 my %options = @_;
70  
71 # empty domain not allowed
72 0 ▶ return 0 unless $url;
Branch: 1 branch
BranchTrueFalseTotal
unless $url 0 0 0
73  
74 # for advanced constraints checking, an HTTP::Response object may be passed
75 0 my $http;
76  
77 0 ▶ if ("HTTP::Response" eq ref $url) {
Branch: 1 branch
BranchTrueFalseTotal
if ("HTTP::Response" eq ref $url) 0 0 0
78 0 $http = $url;
79 0 $url = $url->request->uri;
80 }
81  
82 0 ▶ return 0 unless ("URI::http" eq ref $url || "URI::https" eq ref $url);
Branch: 1 branch
BranchTrueFalseTotal
unless "URI::http" eq ref $url or "URI::https" eq ref $url 0 0 0
Condition0%0/3
"URI::http" eq ref $url or "URI::https" eq ref $url
l!l&&r!l&&!r
000
MC/DC0%0/2
"URI::http" eq ref $url or "URI::https" eq ref $url
"URI::http" eq ref $url "URI::https" eq ref $url
Truth table0%0/3
A or B
A= "URI::http" eq ref $urlB= "URI::https" eq ref $url
ABresult
1X1
011
000
83  
84 0 $url = $url->host;
85  
86 # is anything explicitly allowed, if so everything is restricted unless
87 # explicitly defined in allowed
88 0 ▶ if (keys %{$self->allowed}) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %{$self->allowed;}) 0 0 0
89 0 ▶ if (keys %{$self->allowed}) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %{$self->allowed;}) 0 0 0
90  
91 # return $self->allowed->{$url} ? 1 : 0;
92 0 ▶ if ($self->allowed->{$url}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->allowed->{$url}) { } 0 0 0
93 0 ▶ if ($self->allowed->{$url}->{if}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->allowed->{$url}{'if'}) { } 0 0 0
94 return $self->_check_constraints(
95 0 $self->allowed->{$url}->{if}, $http);
96 }
97 else {
98 0 return 1;
99 }
100 }
101 else {
102 0 return 0;
103 }
104 }
105 }
106  
107 # is it explicitly restricted
108 0 ▶ if (keys %{$self->restricted}) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %{$self->restricted;}) 0 0 0
109 0 ▶ if (keys %{$self->restricted}) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %{$self->restricted;}) 0 0 0
110  
111 # return 0 if $self->restricted->{$url};
112 0 ▶ if ($self->restricted->{$url}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->restricted->{$url}) { } 0 0 0
113 0 ▶ if ($self->restricted->{$url}->{if}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->restricted->{$url}{'if'}) { } 0 0 0
114 return $self->_check_constraints(
115 0 $self->allowed->{$url}->{if}, $http);
116 }
117 else {
118 0 return 1;
119 }
120 }
121 else {
122 0 return 0;
123 }
124 }
125 }
126  
127 # i guess its cool
128 0 return 1;
129 }
130  
131 0 ▶sub _check_constraints {
Subroutine _check_constraints: not called CC=4 SCAR=30.0
132 0 my ($self, $constraints, $http_response) = @_;
133  
134 # check for failure, if not pass it
135 0 ▶ if ($constraints->{content_type}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($constraints->{"content_type"}) 0 0 0
136 0 my $ctype = $http_response->header('content_type');
137 my $types =
138 "ARRAY" eq ref $constraints->{content_type}
139 ? $constraints->{content_type}
140 0 ▶ : [$constraints->{content_type}];
Branch: 1 branch
BranchTrueFalseTotal
'ARRAY' eq ref $constraints->{'content_type'} ? : 0 0 0
141  
142 0 ▶ return 1 if (grep { $ctype eq $_ } @{$types});
Branch: 1 branch
BranchTrueFalseTotal
if grep {$ctype eq $_;} @{$types;} 0 0 0
143  
144 }
145 0 return 0;
146 }
147  
148 10 1;
149  
150 __END__
« Scrappy/Scraper.pm ↑ Summary Scrappy.pm »