lib/WWW/Crawler/Mojo/UserAgent.pm

filter: Statementstmt 100% 57 / 57 Branchbran 90.0% 18 / 20 Conditioncond 100% 5 / 5 MC/DCmcdc 100% 3 / 3 Subroutinesub 100% 13 / 13 Podpod 100% 3 / 3 totaltotal 98.0% 99 / 101 CC 13 SCAR 11.3
CC
13
Cov
98%
_host_key
6.0
active_host
4.0
__ANON__
3.0
CRAP 3.1
« lib/WWW/Crawler/Mojo/Job.pm ↑ Summary lib/WWW/Crawler/Mojo/Queue/Memory.pm »
1 package WWW::Crawler::Mojo::UserAgent;
2 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use Mojo::Base 'Mojo::UserAgent';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use Mojo::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 11 has active_conn => 0;
9 5 ▶has active_conn_per_host => sub { {} };
Subroutine __ANON__: called CC=1 SCAR=0.0
10 11 has '_creds';
11 11 has keep_credentials => 1;
12  
13 16 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
14 16 my $class = shift;
15 16 my $self = $class->SUPER::new(@_);
16  
17 16 ▶ if ($self->keep_credentials) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->keep_credentials) 16 0 16
18 16 $self->_creds({});
19 $self->on(
20 22 ▶ start => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
21 22 my ($self, $tx) = @_;
22 22 my $url = $tx->req->url;
23 22 ▶ my $host_key = _host_key($url) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $host_key = _host_key($url) or return 22 0 22
24 22 ▶ if ($url->userinfo) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url->userinfo) { } 2 20 22
25 2 $self->{_creds}->{$host_key} = $url->userinfo;
26 }
27 else {
28 20 $url->userinfo($self->{_creds}->{$host_key});
29 }
30 }
31 16 );
32 }
33  
34 $self->on(
35 22 ▶ start => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
36 22 my ($self, $tx) = @_;
37 22 my $url = $tx->req->url;
38 22 $self->active_host($url, 1);
39 22 ▶ $tx->on(finish => sub { $self->active_host($url, -1) });
Subroutine __ANON__: called CC=1 SCAR=0.0
40 }
41 16 );
42  
43 16 return $self;
44 }
45  
46 83 ▶sub active_host {
Subroutine active_host: called CC=4 SCAR=13.9
Pod documented
47 83 my ($self, $url, $inc) = @_;
48 83 my $key = _host_key($url);
49 83 my $hosts = $self->active_conn_per_host;
50 83 ▶ if ($inc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inc) 64 19 83
51 64 $self->{active_conn} += $inc;
52 64 $hosts->{$key} += $inc;
53 64 ▶ delete($hosts->{$key}) unless ($hosts->{$key});
Branch: 1 branch
BranchTrueFalseTotal
unless $hosts->{$key} 24 40 64
54 }
55 83 ▶ return $hosts->{$key} || 0;
Condition100%2/2
$hosts->{$key} || 0
l!l
4241
MC/DC100%1/1
$hosts->{$key} || 0
$hosts->{$key}
Truth table100%2/2
A
A= $hosts->{$key}
Aresult
11
00
56 }
57  
58 2 ▶sub credentials {
Subroutine credentials: called CC=2 SCAR=6.9
Pod documented
59 2 my ($self, %credentials) = @_;
60 2 ▶ while (my ($url, $cred) = each(%credentials)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %credentials) 3 2 5
61 3 $self->{_creds}->{_host_key($url)} = $cred;
62 }
63 }
64  
65 118 ▶sub _host_key {
Subroutine _host_key: called CC=6 SCAR=17.9
66 118 state $well_known_ports = {http => 80, https => 443};
67 118 my $url = shift;
68 118 ▶ $url = Mojo::URL->new($url) unless ref $url;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $url 9 109 118
69 118 ▶ return unless $url->is_abs && (my $wkp = $well_known_ports->{$url->scheme});
Branch: 1 branch
BranchTrueFalseTotal
unless $url->is_abs and my $wkp = $$well_known_ports{$url->scheme} 2 116 118
Condition100%3/3
$url->is_abs and my $wkp = $$well_known_ports{$url->scheme}
!ll&&!rl&&r
11116
MC/DC100%2/2
$url->is_abs and my $wkp = $$well_known_ports{$url->scheme}
$url->is_abs my $wkp = $$well_known_ports{$url->scheme}
Truth table100%3/3
A and B
A= $url->is_absB= my $wkp = $$well_known_ports{$url->scheme}
ABresult
0X0
100
111
70 116 my $key = $url->scheme . '://' . $url->ihost;
71 116 ▶ return $key unless (my $port = $url->port);
Branch: 1 branch
BranchTrueFalseTotal
unless my $port = $url->port 16 100 116
72 100 ▶ $key .= ':' . $port if $port != $wkp;
Branch: 1 branch
BranchTrueFalseTotal
if $port != $wkp 96 4 100
73 100 return $key;
74 }
75  
76 11 1;
77  
78 =head1 NAME
79  
80 WWW::Crawler::Mojo::UserAgent - Crawler specific featured user agent
81  
82 =head1 SYNOPSIS
83  
84 my $ua = WWW::Crawler::Mojo::UserAgent->new;
85 $ua->keep_credentials(1);
86 $ua->credentials(
87 'http://example.com:8080' => 'jamadam:password1',
88 'http://example2.com:8080' => 'jamadam:password2',
89 );
90 my $tx = $ua->get('http://example.com/');
91 say $tx->req->url # http://jamadam:passowrd@example.com/
92
93 if ($ua->active_conn < $max_conn) {
94 $ua->get(...);
95 }
96
97 if ($ua->active_host($url) < $max_conn_per_host) {
98 $ua->get(...);
99 }
100  
101 =head1 DESCRIPTION
102  
103 This class inherits L<Mojo::UserAgent> and adds credential storage and
104 active connection counter.
105  
106 =head1 ATTRIBUTES
107  
108 WWW::Crawler::Mojo::UserAgent inherits all attributes from Mojo::UserAgent.
109  
110 =head2 active_conn
111  
112 A number of current connections.
113  
114 $bot->active_conn($bot->active_conn + 1);
115 say $bot->active_conn;
116  
117 =head2 active_conn_per_host
118  
119 A number of current connections per host.
120  
121 $bot->active_conn_per_host($bot->active_conn_per_host + 1);
122 say $bot->active_conn_per_host;
123  
124 =head2 keep_credentials
125  
126 Sets true to activate the feature. Defaults to 1.
127  
128 $ua->keep_credentials(1);
129  
130 =head1 METHODS
131  
132 WWW::Crawler::Mojo::UserAgent inherits all methods from L<Mojo::UserAgent>.
133  
134 =head2 active_host
135  
136 Maintenances the numbers of active connections.
137  
138 $ua->active_host($url, 1);
139 $ua->active_host($url, -1);
140 my $amount = $ua->active_host($url);
141  
142 =head2 credentials
143  
144 Stores credentials.
145  
146 $ua->credentials(
147 'http://example.com:8080' => 'jamadam:password1',
148 'http://example2.com:8080' => 'jamadam:password2',
149 );
150  
151 =head2 new
152  
153 Constructer.
154  
155 $ua = WWW::Crawler::Mojo::UserAgent->new;
156  
157 =head1 AUTHOR
158  
159 Keita Sugama, E<lt>sugama@jamadam.comE<gt>
160  
161 =head1 COPYRIGHT AND LICENSE
162  
163 Copyright (C) Keita Sugama.
164  
165 This program is free software; you can redistribute it and/or
166 modify it under the same terms as Perl itself.
167  
168 =cut
« lib/WWW/Crawler/Mojo/Job.pm ↑ Summary lib/WWW/Crawler/Mojo/Queue/Memory.pm »