lib/WWW/Crawler/Mojo/Job.pm

filter: Statementstmt 98.0% 49 / 50 Branchbran 87.5% 7 / 8 Conditioncond 100% 5 / 5 MC/DCmcdc 100% 3 / 3 Subroutinesub 92.3% 12 / 13 Podpod 100% 7 / 7 totaltotal 96.5% 83 / 86 CC 7 SCAR 7.2
CC
7
Cov
97%
upgrade
4.0
digest
3.0
original_url
2.0
CRAP 2.1
« lib/WWW/Crawler/Mojo/Queue/Memory.pm ↑ Summary blib/lib/WWW/Crawler/Mojo/Queue.pm »
1 package WWW::Crawler::Mojo::Job;
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 utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use Mojo::Util qw(md5_sum);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 11 has 'closed';
9 11 has 'context';
10 11 has depth => 0;
11 11 has 'literal_uri';
12 11 has 'method';
13 11 has 'referrer';
14 0 ▶has redirect_history => sub { [] };
Subroutine __ANON__: not called CC=1 SCAR=1.2
15 11 has 'tx_params';
16 11 has 'url';
17  
18 sub upgrade {
19 60 ▶ my ($class, $job) = @_;
Subroutine upgrade: called CC=4 SCAR=13.9
Pod documented
20  
21 60 ▶ if (!ref $job || ref $job ne __PACKAGE__) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $job or ref $job ne __PACKAGE__) 8 52 60
Condition100%3/3
not ref $job or ref $job ne __PACKAGE__
l!l&&r!l&&!r
4452
MC/DC100%2/2
not ref $job or ref $job ne __PACKAGE__
not ref $job ref $job ne __PACKAGE__
Truth table100%3/3
not(A) or B
A= not ref $jobB= ref $job ne __PACKAGE__
ABresult
1X1
011
000
22 8 ▶ my $url = !ref $job ? Mojo::URL->new($job) : $job;
Branch: 1 branch
BranchTrueFalseTotal
!ref($job) ? : 4 4 8
23 8 $job = $class->new(url => $url);
24 }
25  
26 60 return $job;
27 }
28  
29 sub clone {
30 1 ▶ my $self = shift;
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
31 1 return __PACKAGE__->new(%$self);
32 }
33  
34 sub close {
35 14 ▶ my $self = shift;
Subroutine close: called CC=1 SCAR=0.0
Pod documented
36 14 $self->{closed} = 1;
37 14 $self->{referrer} = undef;
38 }
39  
40 sub child {
41 77 ▶ my $self = shift;
Subroutine child: called CC=1 SCAR=0.0
Pod documented
42 77 return __PACKAGE__->new(@_, referrer => $self, depth => $self->depth + 1);
43 }
44  
45 sub digest {
46 64 ▶ my $self = shift;
Subroutine digest: called CC=3 SCAR=11.0
Pod documented
47 64 ▶ my $md5_seed = $self->url->to_string . ($self->method || '');
Condition100%2/2
$self->method || ''
l!l
658
MC/DC100%1/1
$self->method || ''
$self->method
Truth table100%2/2
A
A= $self->method
Aresult
11
00
48 64 ▶ $md5_seed .= $self->tx_params->to_string if ($self->tx_params);
Branch: 1 branch
BranchTrueFalseTotal
if $self->tx_params 3 61 64
49 64 return md5_sum($md5_seed);
50 }
51  
52 sub redirect {
53 14 ▶ my ($self, $last, @history) = @_;
Subroutine redirect: called CC=1 SCAR=0.0
Pod documented
54 14 $self->url($last);
55 14 $self->redirect_history(\@history);
56 }
57  
58 sub original_url {
59 1 ▶ my $self = shift;
Subroutine original_url: called CC=2 SCAR=7.0
Pod documented
60 1 my @histry = @{$self->redirect_history};
61 1 ▶ return $self->url unless (@histry);
Branch: 1 branch
BranchTrueFalseTotal
unless @histry 0 1 1
62 1 return $histry[$#histry];
63 }
64  
65 11 1;
66  
67 =head1 NAME
68  
69 WWW::Crawler::Mojo::Job - Single crawler job
70  
71 =head1 SYNOPSIS
72  
73 my $job1 = WWW::Crawler::Mojo::Job->new;
74 $job1->url('http://example.com/');
75 my $job2 = $job1->child;
76  
77 =head1 DESCRIPTION
78  
79 This class represents a single crawler job.
80  
81 =head1 ATTRIBUTES
82  
83 =head2 context
84  
85 Either L<Mojo::DOM> or L<Mojo::URL> instance that the job is referrered by.
86  
87 $job->context($dom);
88 say $job->context;
89  
90 =head2 closed
91  
92 A flag indecates whether the job is closed or not.
93  
94 $job->closed(1);
95 say $job->closed;
96  
97 =head2 depth
98  
99 The depth of the job in referrer series.
100  
101 my $job1 = WWW::Crawler::Mojo::Job->new;
102 my $job2 = $job1->child;
103 my $job3 = $job2->child;
104 say $job1->depth; # 0
105 say $job2->depth; # 1
106 say $job3->depth; # 2
107  
108 =head2 literal_uri
109  
110 A L<Mojo::URL> instance of the literal URL that has appeared in the referrer
111 document.
112  
113 $job1->literal_uri('./index.html');
114 say $job1->literal_uri; # './index.html'
115  
116 =head2 referrer
117  
118 A job instance that has referred the URL.
119  
120 $job1->referrer($job);
121 my $job2 = $job1->referrer;
122  
123 =head2 redirect_history
124  
125 An array reference that contains URLs of redirect history.
126  
127 $job1->redirect_history([$url1, $url2, $url3]);
128 my $history = $job1->redirect_history;
129  
130 =head2 url
131  
132 A L<Mojo::URL> instance of the resolved URL.
133  
134 $job1->url('http://example.com/');
135 say $job1->url; # 'http://example.com/'
136  
137 =head2 method
138  
139 HTTP request method such as GET or POST.
140  
141 $job1->method('GET');
142 say $job1->method; # GET
143  
144 =head2 tx_params
145  
146 A hash reference that contains params for L<Mojo::Transaction>.
147  
148 $job1->tx_params({foo => 'bar'});
149 $params = $job1->tx_params;
150  
151 =head1 METHODS
152  
153 =head2 clone
154  
155 Clones the job.
156  
157 my $job2 = $job1->clone;
158  
159 =head2 close
160  
161 Closes the job and cuts the referrer series.
162  
163 $job->close;
164  
165 =head2 child
166  
167 Instantiates a child job by parent job. The parent URL is set to child referrer.
168  
169 my $job1 = WWW::Crawler::Mojo::Job->new(url => 'http://example.com/1');
170 my $job2 = $job1->child(url => 'http://example.com/2');
171 say $job2->referrer->url # 'http://example.com/1'
172  
173 =head2 digest
174  
175 Generates digest string with C<url>, C<method>, C<tx_params> attributes.
176  
177 say $job->digest;
178  
179 =head2 redirect
180  
181 Replaces the resolved URL and history at once.
182  
183 my $job = WWW::Crawler::Mojo::Job->new;
184 $job->url($url1);
185 $job->redirect($url2, $url3);
186 say $job->url # $url2
187 say $job->redirect_history # [$url1, $url3]
188  
189 =head2 original_url
190  
191 Returns the original URL of redirected job. If redirected, returns last element
192 of C<redirect_histroy> attribute, otherwise returns C<url> attribute.
193  
194 $job1->redirect_history([$url1, $url2, $url3]);
195 my $url4 = $job1->original_url; # $url4 is $url3
196  
197 =head2 upgrade
198  
199 Instanciates a job with string or a L<Mojo::URL> instance.
200  
201 =head1 AUTHOR
202  
203 Keita Sugama, E<lt>sugama@jamadam.comE<gt>
204  
205 =head1 COPYRIGHT AND LICENSE
206  
207 Copyright (C) Keita Sugama.
208  
209 This program is free software; you can redistribute it and/or
210 modify it under the same terms as Perl itself.
211  
212 =cut
« lib/WWW/Crawler/Mojo/Queue/Memory.pm ↑ Summary blib/lib/WWW/Crawler/Mojo/Queue.pm »