Prog/Aggregator/Spider/UA.pm

filter: Statementstmt 89.1% 66 / 74 Branchbran 50.0% 11 / 22 Conditioncond 40.0% 6 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 88.2% 15 / 17 Podpod 100% 8 / 8 totaltotal 73.1% 106 / 145 CC 17 SCAR 18.4
CC
17
Cov
75%
get
11.5
head
10.4
get_response_class
2.5
CRAP 6.3
« Prog/Aggregator/Spider/Response.pm ↑ Summary Prog/ReplaceRules.pm »
1 package SWISH::Prog::Aggregator::Spider::UA;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use base qw( LWP::RobotUA );
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use HTTP::Message;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Data::Dump qw( dump );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Search::Tools::UTF8;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use SWISH::Prog::Aggregator::Spider::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 our $VERSION = '0.75';
13  
14 # if Compress::Zlib is installed, this should handle gzip transparently.
15 # thanks to
16 # http://stackoverflow.com/questions/1285305/how-can-i-accept-gzip-compressed-content-using-lwpuseragent
17 2 my $can_accept = HTTP::Message::decodable();
18  
19 # TODO handle when Zlib is *not* installed, via Content-Encoding header
20  
21 #warn "Accept-Encoding: $can_accept\n";
22  
23 2 ▶our $Debug = $ENV{SPIDER_DEBUG} || 0;
Condition50%1/2
$ENV{'SPIDER_DEBUG'} || 0
l!l
02
MC/DC0%0/1
$ENV{'SPIDER_DEBUG'} || 0
$ENV{'SPIDER_DEBUG'}
Truth table50%1/2
A
A= $ENV{'SPIDER_DEBUG'}
Aresult
11
00
24  
25 2 our $Response_Class = 'SWISH::Prog::Aggregator::Spider::Response';
26  
27 =pod
28  
29 =head1 NAME
30  
31 SWISH::Prog::Aggregator::Spider::UA - spider user agent
32  
33 =head1 SYNOPSIS
34  
35 use SWISH::Prog::Aggregator::Spider::UA;
36 my $ua = SWISH::Prog::Aggregator::Spider::UA->new;
37
38 # $ua is a LWP::RobotUA object
39  
40 =head1 DESCRIPTION
41  
42 SWISH::Prog::Aggregator::Spider::UA is a subclass of
43 LWP::RobotUA.
44  
45 =head1 METHODS
46  
47 =cut
48  
49 =head2 get( I<args> )
50  
51 I<args> is an array of key/value pairs. I<uri> is required.
52  
53 I<delay> will sleep() I<delay> seconds before fetching I<uri>.
54  
55 Also supported: I<user> and I<pass> for authorization.
56  
57 =cut
58  
59 9 ▶sub get {
Subroutine get: called CC=9 SCAR=24.4
Pod documented
60 9 my $self = shift;
61 9 my %args = @_;
62 9 ▶ my $uri = delete $args{uri} or croak "URI required";
Branch: 1 branch
BranchTrueFalseTotal
my $uri = delete $args{"uri"} or croak("URI required") 9 0 9
63 9 ▶ my $delay = delete $args{delay} || 0;
Condition50%1/2
delete $args{'delay'} || 0
l!l
09
MC/DC0%0/1
delete $args{'delay'} || 0
delete $args{'delay'}
Truth table50%1/2
A
A= delete $args{'delay'}
Aresult
11
00
64  
65 9 ▶ sleep($delay) if $delay;
Branch: 1 branch
BranchTrueFalseTotal
if $delay 0 9 9
66  
67 9 my $request = HTTP::Request->new( 'GET' => $uri );
68 9 $request->header( 'Accept-Encoding' => $can_accept, );
69 9 ▶ if ( $args{user} && $args{pass} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'user'} and $args{'pass'}) { } 9 0 9
Condition33%1/3
$args{"user"} and $args{"pass"}
!ll&&!rl&&r
009
MC/DC0%0/2
$args{"user"} and $args{"pass"}
$args{"user"} $args{"pass"}
Truth table33%1/3
A and B
A= $args{"user"}B= $args{"pass"}
ABresult
0X0
100
111
70 $request->authorization_basic( delete $args{user},
71 9 delete $args{pass} );
72 }
73 else {
74 # if either one was set, but not the other, delete them both
75 # to prevent response_class from dying
76 0 delete $args{pass};
77 0 delete $args{user};
78 }
79  
80 9 ▶ ( $Debug & 2 ) and dump $request;
Branch: 1 branch
BranchTrueFalseTotal
$SWISH::Prog::Aggregator::Spider::UA::Debug & 2 and SWISH::Prog::Aggregator::Spider::UA::dump($request) 0 9 9
81 9 ▶ ( $Debug & 3 ) and dump \%args;
Branch: 1 branch
BranchTrueFalseTotal
$SWISH::Prog::Aggregator::Spider::UA::Debug & 3 and SWISH::Prog::Aggregator::Spider::UA::dump(\%args) 0 9 9
82  
83 my $resp = $self->get_response_class->new(
84 http_response => $self->request($request),
85 link_tags => $self->{_swish_link_tags},
86 9 %args,
87 );
88 9 $self->{_swish_last_uri} = URI->new($uri);
89 9 $self->{_swish_last_resp} = $resp;
90  
91 9 ▶ ( $Debug & 2 ) and dump $resp;
Branch: 1 branch
BranchTrueFalseTotal
$SWISH::Prog::Aggregator::Spider::UA::Debug & 2 and SWISH::Prog::Aggregator::Spider::UA::dump($resp) 0 9 9
92  
93 9 return $resp;
94 }
95  
96 =head2 head( I<args> )
97  
98 Like get(), I<args> is an array of key/value pairs. I<uri> is required.
99  
100 I<delay> will sleep() I<delay> seconds before fetching I<uri>.
101  
102 Also supported: I<user> and I<pass> for authorization.
103  
104 =cut
105  
106 13 ▶sub head {
Subroutine head: called CC=8 SCAR=23.4
Pod documented
107 13 my $self = shift;
108 13 my %args = @_;
109 13 ▶ my $uri = delete $args{uri} or croak "URI required";
Branch: 1 branch
BranchTrueFalseTotal
my $uri = delete $args{"uri"} or croak("URI required") 13 0 13
110 13 ▶ my $delay = delete $args{delay} || 0;
Condition50%1/2
delete $args{'delay'} || 0
l!l
013
MC/DC0%0/1
delete $args{'delay'} || 0
delete $args{'delay'}
Truth table50%1/2
A
A= delete $args{'delay'}
Aresult
11
00
111  
112 13 ▶ sleep($delay) if $delay;
Branch: 1 branch
BranchTrueFalseTotal
if $delay 0 13 13
113  
114 13 my $request = HTTP::Request->new( 'HEAD' => $uri );
115 13 $request->header( 'Accept-Encoding' => $can_accept, );
116 13 ▶ if ( $args{user} && $args{pass} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'user'} and $args{'pass'}) { } 13 0 13
Condition33%1/3
$args{"user"} and $args{"pass"}
!ll&&!rl&&r
0013
MC/DC0%0/2
$args{"user"} and $args{"pass"}
$args{"user"} $args{"pass"}
Truth table33%1/3
A and B
A= $args{"user"}B= $args{"pass"}
ABresult
0X0
100
111
117 $request->authorization_basic( delete $args{user},
118 13 delete $args{pass} );
119 }
120 else {
121 # if either one was set, but not the other, delete them both
122 # to prevent response_class from dying
123 0 delete $args{pass};
124 0 delete $args{user};
125 }
126  
127 13 ▶ ( $Debug & 2 ) and dump $request;
Branch: 1 branch
BranchTrueFalseTotal
$SWISH::Prog::Aggregator::Spider::UA::Debug & 2 and SWISH::Prog::Aggregator::Spider::UA::dump($request) 0 13 13
128  
129 my $resp = $self->get_response_class->new(
130 http_response => $self->request($request),
131 link_tags => $self->{_swish_link_tags},
132 13 %args,
133 );
134  
135 13 ▶ ( $Debug & 2 ) and dump $resp;
Branch: 1 branch
BranchTrueFalseTotal
$SWISH::Prog::Aggregator::Spider::UA::Debug & 2 and SWISH::Prog::Aggregator::Spider::UA::dump($resp) 0 13 13
136  
137 13 return $resp;
138 }
139  
140 =head2 redirect_ok
141  
142 Returns 0 (false) to override parent class behavior.
143  
144 =cut
145  
146 6 ▶sub redirect_ok {
Subroutine redirect_ok: called CC=1 SCAR=0.0
Pod documented
147 6 return 0; # do not follow any redirects
148 }
149  
150 =head2 response
151  
152 Returns most recent Response object.
153  
154 =cut
155  
156 0 ▶sub response {
Subroutine response: not called CC=1 SCAR=6.9
Pod documented
157 0 my $self = shift;
158 0 return $self->{_swish_last_resp};
159 }
160  
161 =head2 uri
162  
163 Returns most recently requested URI object.
164  
165 =cut
166  
167 5 ▶sub uri {
Subroutine uri: called CC=1 SCAR=0.0
Pod documented
168 5 return shift->{_swish_last_uri};
169 }
170  
171 =head2 set_link_tags( I<hashref> )
172  
173 Set hashref of tags considered valid "links". Passed into every
174 Response object in the link_tags() accessor.
175  
176 =cut
177  
178 1 ▶sub set_link_tags {
Subroutine set_link_tags: called CC=1 SCAR=0.0
Pod documented
179 1 my $self = shift;
180 1 $self->{_swish_link_tags} = shift;
181 }
182  
183 =head2 set_response_class( I<class> )
184  
185 Set the Response class. Default is B<SWISH::Prog::Aggregator::Spider::Response>.
186  
187 =cut
188  
189 0 ▶sub set_response_class {
Subroutine set_response_class: not called CC=1 SCAR=6.9
Pod documented
190 0 my $self = shift;
191 0 $self->{_swish_response_class} = shift;
192 }
193  
194 =head2 get_response_class
195  
196 Returns the class name of objects returned from get() and head().
197  
198 =cut
199  
200 22 ▶sub get_response_class {
Subroutine get_response_class: called CC=2 SCAR=9.2
Pod documented
201 22 ▶ return shift->{_swish_response_class} || $Response_Class;
Condition33%1/3
+(shift())->{'_swish_response_class'} || $SWISH::Prog::Aggregator::Spider::UA::Response_Class
l!l&&r!l&&!r
0220
MC/DC0%0/2
+(shift())->{'_swish_response_class'} || $SWISH::Prog::Aggregator::Spider::UA::Response_Class
+(shift())->{'_swish_response_class'} $SWISH::Prog::Aggregator::Spider::UA::Response_Class
Truth table33%1/3
A || B
A= +(shift())->{'_swish_response_class'}B= $SWISH::Prog::Aggregator::Spider::UA::Response_Class
ABresult
1X1
011
000
202 }
203  
204 2 1;
205  
206 __END__
« Prog/Aggregator/Spider/Response.pm ↑ Summary Prog/ReplaceRules.pm »