Prog/Aggregator/Spider/Response.pm

filter: Statementstmt 97.8% 91 / 93 Branchbran 58.3% 14 / 24 Conditioncond 58.3% 7 / 12 MC/DCmcdc 28.5% 2 / 7 Subroutinesub 95.4% 21 / 22 Podpod 100% 8 / 8 totaltotal 86.1% 143 / 166 CC 19 SCAR 18.7
CC
19
Cov
87%
links
15.6
title
3.2
is_html
3.1
CRAP 6.5
« Prog/Searcher.pm ↑ Summary Prog/Aggregator/Spider/UA.pm »
1 package SWISH::Prog::Aggregator::Spider::Response;
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( SWISH::Prog::Class );
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Data::Dump qw( dump );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Search::Tools::UTF8;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use HTML::LinkExtor;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use HTML::Tagset;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use HTML::HeadParser;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 our $VERSION = '0.75';
14  
15 2 __PACKAGE__->mk_accessors(
16 qw(
17 http_response
18 link_tags
19 )
20 );
21  
22 =pod
23  
24 =head1 NAME
25  
26 SWISH::Prog::Aggregator::Spider::Response - spider response
27  
28 =head1 SYNOPSIS
29  
30 use SWISH::Prog::Aggregator::Spider::UA;
31 my $ua = SWISH::Prog::Aggregator::Spider::UA->new;
32 my $response = $ua->get('http://swish-e.org/');
33 my $http_response = $response->http_response;
34
35 # $ua isa LWP::RobotUA subclass
36 # $response isa SWISH::Prog::Aggregator::Spider::Response
37 # $http_response isa HTTP::Response
38  
39 =head1 DESCRIPTION
40  
41 SWISH::Prog::Aggregator::Spider::Response wraps the
42 HTTP::Response class and provides some convenience methods.
43  
44 =head1 METHODS
45  
46 =cut
47  
48 =head2 init
49  
50 Override internal constructor setup method.
51  
52 =cut
53  
54 22 ▶sub init {
Subroutine init: called CC=1 SCAR=0.0
Pod documented
55 22 my $self = shift;
56 22 $self->SUPER::init(@_);
57  
58 # TODO set by our UA. duplicate?
59 #$self->{link_tags} ||= { a => 1, frame => 1, iframe => 1, };
60 22 return $self;
61 }
62  
63 =head2 http_response
64  
65 Returns internal HTTP::Response object.
66  
67 =cut
68  
69 =head2 success
70  
71 Shortcut for $response->http_response->is_success.
72  
73 =cut
74  
75 30 ▶sub success {
Subroutine success: called CC=1 SCAR=0.0
Pod documented
76 30 return shift->http_response->is_success;
77 }
78  
79 =head2 status
80  
81 Shortcut for $response->http_response->code.
82  
83 =cut
84  
85 26 ▶sub status {
Subroutine status: called CC=1 SCAR=0.0
Pod documented
86 26 return shift->http_response->code;
87 }
88  
89 =head2 ct
90  
91 Shortcut for $response->response->header('content-type').
92 Any encoding will be stripped from the returned string.
93  
94 =cut
95  
96 32 ▶sub ct {
Subroutine ct: called CC=2 SCAR=7.0
Pod documented
97 32 my $self = shift;
98 32 my $ct = $self->http_response->header('content-type');
99 32 ▶ $ct =~ s/;.+// if $ct;
Branch: 1 branch
BranchTrueFalseTotal
if $ct 32 0 32
100 32 return $ct;
101 }
102  
103 =head2 is_html
104  
105 Returns true if ct() looks like HTML or XHTML.
106  
107 =cut
108  
109 21 ▶sub is_html {
Subroutine is_html: called CC=3 SCAR=11.3
Pod documented
110 21 my $self = shift;
111 21 my $ct = $self->ct;
112 21 ▶ return defined $ct
Condition66%4/6
defined $ct && ($ct eq 'text/html' || $ct eq 'application/xhtml+xml')
!ll&&!rl&&r
0516
$ct eq "text/html" or $ct eq "application/xhtml+xml"
l!l&&r!l&&!r
1605
MC/DC33%1/3
defined $ct && ($ct eq 'text/html' || $ct eq 'application/xhtml+xml')
defined $ct $ct eq "text/html" $ct eq "application/xhtml+xml"
Truth table50%2/4
A && B or C
A= defined $ctB= $ct eq "text/html"C= $ct eq "application/xhtml+xml"
ABCresult
0XX0
1000
11X1
1011
113 && ( $ct eq 'text/html' || $ct eq 'application/xhtml+xml' );
114 }
115  
116 =head2 content
117  
118 Shortcut for $response->http_response->decoded_content.
119  
120 =cut
121  
122 14 ▶sub content {
Subroutine content: called CC=1 SCAR=0.0
Pod documented
123 14 return shift->http_response->decoded_content;
124 }
125  
126 =head2 links
127  
128 Returns array of href targets in content(). Parsed
129 using HTML::LinkExtor.
130  
131 =cut
132  
133 6 ▶sub links {
Subroutine links: called CC=14 SCAR=27.5
Pod documented
134 6 my $self = shift;
135 6 my @links = ();
136 6 my $http_response = $self->http_response;
137 6 my $debug = $self->debug;
138  
139 6 ▶ if ( $http_response and $self->is_html ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($http_response and $self->is_html) 4 2 6
Condition66%2/3
$http_response and $self->is_html
!ll&&!rl&&r
024
MC/DC50%1/2
$http_response and $self->is_html
$http_response $self->is_html
Truth table66%2/3
A and B
A= $http_responseB= $self->is_html
ABresult
0X0
100
111
140 4 my $le = HTML::LinkExtor->new();
141 4 my $base = $http_response->base;
142 4 $le->parse( $self->content );
143  
144 4 my %skipped_tags;
145  
146 4 for my $link ( $le->links ) {
147 17 my ( $tag, %attr ) = @$link;
148  
149 # which tags to use
150 17 my $attr = join ' ', map {qq[$_="$attr{$_}"]} keys %attr;
151  
152 17 ▶ $debug and SWISH::Prog::Utils->write_log(
Branch: 1 branch
BranchTrueFalseTotal
$debug and "SWISH::Prog::Utils"->write_log("uri", $base, "msg", "extracted tag '<$tag $attr>'") 0 17 17
153 uri => $base,
154 msg => "extracted tag '<$tag $attr>'"
155 );
156  
157 17 ▶ if ( !exists $self->link_tags->{$tag} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->link_tags->{$tag}) 1 16 17
158 1 ▶ $debug
Branch: 1 branch
BranchTrueFalseTotal
$debug and "SWISH::Prog::Utils"->write_log("uri", $base, "msg", "skipping tag '<$tag $attr>', not on whitelist") 0 1 1
159 and SWISH::Prog::Utils->write_log(
160 uri => $base,
161 msg => "skipping tag '<$tag $attr>', not on whitelist"
162 );
163 1 next;
164 }
165  
166 # Grab which attribute(s) which might contain links for this tag
167 16 my $links = $HTML::Tagset::linkElements{$tag};
168 16 ▶ $links = [$links] unless ref $links;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $links 0 16 16
169  
170 16 my $found = 0;
171  
172 # check each attribute to see if a link exists
173 16 for my $attribute (@$links) {
174 16 ▶ if ( $attr{$attribute} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr{$attribute}) 16 0 16
175  
176 # strip any anchors as noise
177 16 $attr{$attribute} =~ s/#.*//;
178  
179 16 my $u = URI->new_abs( $attr{$attribute}, $base );
180 16 push @links, $u;
181 16 ▶ $debug
Branch: 1 branch
BranchTrueFalseTotal
$debug and "SWISH::Prog::Utils"->write_log("uri", $base, "msg", "added '${u}' to links") 0 16 16
182 and SWISH::Prog::Utils->write_log(
183 uri => $base,
184 msg => "added '$u' to links",
185 );
186 16 $found++;
187 }
188 }
189  
190 16 ▶ if ( !$found && $debug ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $found and $debug) 0 16 16
Condition33%1/3
not $found and $debug
!ll&&!rl&&r
1600
MC/DC0%0/2
not $found and $debug
not $found $debug
Truth table33%1/3
not(A) and B
A= not $foundB= $debug
ABresult
0X0
100
111
191 0 SWISH::Prog::Utils->write_log(
192 uri => $base,
193 msg => "tag <$tag $attr> has no links or is a duplicate",
194 );
195 }
196  
197 }
198  
199 $debug
200 4 ▶ and SWISH::Prog::Utils->write_log(
Branch: 1 branch
BranchTrueFalseTotal
$debug and "SWISH::Prog::Utils"->write_log("uri", $base, "msg", sprintf("found %d links", scalar @links)) 0 4 4
201 uri => $base,
202 msg => sprintf( "found %d links", scalar @links ),
203 );
204  
205 }
206 6 return @links;
207 }
208  
209 =head2 link_tags( I<hashref> )
210  
211 Set hashref of tags considered valid "links". Used by the links()
212 method.
213  
214 =cut
215  
216 =head2 title
217  
218 Returns document title, verifying that UTF-8
219 flag is set correctly on the response content.
220  
221 =cut
222  
223 4 ▶sub title {
Subroutine title: called CC=3 SCAR=11.6
Pod documented
224 4 my $self = shift;
225 4 ▶ return unless $self->is_html;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_html 0 4 4
226  
227 4 my $p = HTML::HeadParser->new;
228  
229 # HTML::HeadParser throws warning if utf-8 flag is not on for utf-8 bytes.
230 # So we trust the content-type header and
231 # verify that the utf-8 flag is on.
232 4 ▶ if ( $self->http_response->header('content-type') =~ m/utf-8/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->http_response->header('content-type') =~ /utf-8/i) { } 0 4 4
233 0 $p->parse( to_utf8( $self->content ) );
234 }
235 else {
236 4 $p->parse( $self->content );
237 }
238 4 return $p->header('Title');
239 }
240  
241 # delegate all other method calls the the http_response object.
242 # cribbed from HTTP::Message
243 2 our $AUTOLOAD;
244  
245 4 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=1 SCAR=0.0
246 4 my $method = substr( $AUTOLOAD, rindex( $AUTOLOAD, '::' ) + 2 );
247  
248 # We create the function here so that it will not need to be
249 # autoloaded the next time.
250 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
251 36 ▶ *$method = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
252 36 local $Carp::Internal{ +__PACKAGE__ } = 1;
253 36 shift->http_response->$method(@_);
254 4 };
255 4 goto &$method;
256 }
257  
258 0 ▶sub DESTROY { } # avoid AUTOLOADing it
Subroutine DESTROY: not called CC=1 SCAR=0.0
259  
260 2 1;
261  
262 __END__
« Prog/Searcher.pm ↑ Summary Prog/Aggregator/Spider/UA.pm »