blib/lib/WWW/MSA/Hadith.pm

filter: Statementstmt 61.7% 58 / 94 Branchbran 23.5% 8 / 34 Conditioncond 4.1% 1 / 24 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 84.6% 11 / 13 Podpod 0.0% 0 / 7 totaltotal 42.1% 78 / 185 CC 27 SCAR 46.9
CC
27
Cov
44%
read
232.4
result_count
6.0
_is_hadith
5.2
CRAP 108.8
↑ Summary
1 package WWW::MSA::Hadith;
2  
3 # $Id: Hadith.pm,v 1.3 2003/06/21 09:03:22 sherzodr Exp $
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use vars qw($VERSION $PROXY $HADITH_URLF);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 $VERSION = '1.01';
10 1 $PROXY = 'http://www.usc.edu/cgi-bin/msasearch';
11 1 $HADITH_URLF = 'http://www.usc.edu/dept/MSA/fundamentals/hadithsunnah/%s/%03d.sbt.html';
12  
13  
14 # Preloaded methods go here.
15  
16  
17  
18 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.4
Pod undocumented
19 1 my $class = shift;
20 1 ▶ $class = ref($class) || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
21  
22 1 my $self = {
23 _USER_AGENT => undef,
24 _QUERY => "",
25 _RESULTS => [],
26 };
27  
28 1 return bless ($self, $class);
29 }
30  
31  
32  
33 0 ▶sub DESTROY { }
Subroutine DESTROY: not called CC=1 SCAR=0.0
34  
35  
36 4 ▶sub query {
Subroutine query: called CC=2 SCAR=6.9
Pod undocumented
37 4 my ($self, $str) = @_;
38  
39 4 ▶ if ( defined $str ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $str) 1 3 4
40 1 $self->{_QUERY} = $str;
41 }
42  
43 4 return $self->{_QUERY};
44 }
45  
46  
47 1 ▶sub submit {
Subroutine submit: called CC=3 SCAR=11.7
Pod undocumented
48 1 my $self = shift;
49  
50 1 ▶ unless ( defined $self->query ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->query) 0 1 1
51 0 croak "You didn't call query() yet";
52 }
53  
54 1 my $user_agent = $self->user_agent();
55 1 my %form_ref = (
56 querystring => $self->query(),
57 database => 'bukhari',
58 dbpath => '/www/dept/MSA/reference/indices',
59 logpath => '/www/dept/MSA/reference/indices/logfile',
60 xpath => '/www/dept/MSA/reference/xfile'
61 );
62  
63 1 my $response = $user_agent->post($PROXY, \%form_ref);
64 1 ▶ if ( $response->is_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_error) 0 1 1
65 0 croak "couldn't fetch the results: " . $response->status_line;
66 }
67  
68 1 $self->{_CONTENT} = $response->content_ref;
69 1 return $self->_parse_response($response);
70 }
71  
72  
73  
74 1 ▶sub _parse_response {
Subroutine _parse_response: called CC=2 SCAR=8.4
75 1 my ($self, $response) = @_;
76  
77 1 my $content = ${$self->{_CONTENT}};
78  
79 1 require HTML::TreeBuilder;
80 1 my $html = new HTML::TreeBuilder();
81 1 $html->parse($content);
82  
83 1 for my $a ( $html->look_down(\&_is_hadith) ) {
84 0 my $href = $a->attr('href');
85 0 my $abs_href = URI->new_abs($href, $response->base);
86 0 my $id = $abs_href->fragment;
87 0 my ($volume, $book, $report) = $id =~ m/^(\d+)\.(\d+)\.(\d+w?)/;
88  
89 0 push @{$self->{_RESULTS}}, {
90 id => $id,
91 url => $abs_href->as_string,
92 book => $book,
93 volume => $volume,
94 report => $report
95 };
96 }
97 1 $html->delete();
98 }
99  
100  
101  
102  
103  
104  
105 1 ▶sub get_result {
Subroutine get_result: called CC=1 SCAR=0.0
Pod undocumented
106 1 my $self = shift;
107  
108 1 return shift @{$self->{_RESULTS}};
109 }
110  
111  
112  
113 1 ▶sub read {
Subroutine read: called CC=18 SCAR=54.5
Pod undocumented
114 1 my ($self, $id) = @_;
115  
116 1 my $text = "";
117  
118 1 my ($volume, $book, $report) = $id =~ m/^(\d+)\.(\d+)\.(\d+\w?)$/;
119 1 my $url = sprintf($HADITH_URLF, 'bukhari', $book);
120  
121 1 my $user_agent = $self->user_agent();
122 1 my $response = $user_agent->get($url);
123 1 ▶ if ( $response->is_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_error) 1 0 1
124 1 die $response->status_line;
125 }
126 0 require HTML::TokeParser;
127 0 ▶ my $html = HTML::TokeParser->new($response->content_ref) or die $!;
Branch: 1 branch
BranchTrueFalseTotal
my $html = "HTML::TokeParser"->new($response->content_ref) or die $! 0 0 0
128  
129 0 my $header = 0;
130 0 my $inside = 0;
131 0 ▶ while ( my $token = $html->get_token() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $token = $html->get_token) 0 0 0
132 0 ▶ if ( !$header && ($token->[0] eq 'S') && ($token->[1] eq 'a')
Branch: 1 branch
BranchTrueFalseTotal
if (not $header and $token->[0] eq "S" and $token->[1] eq "a" and $token->[2]{"name"} and $token->[2]{"name"} eq $id) 0 0 0
Condition0%0/12
not $header and $token->[0] eq "S" and $token->[1] eq "a" and $token->[2]{"name"} and $token->[2]{"name"} eq $id
!ll&&!rl&&r
000
not $header and $token->[0] eq "S" and $token->[1] eq "a" and $token->[2]{"name"}
!ll&&!rl&&r
000
not $header and $token->[0] eq "S" and $token->[1] eq "a"
!ll&&!rl&&r
000
not $header and $token->[0] eq "S"
!ll&&!rl&&r
000
MC/DC0%0/5
not $header and $token->[0] eq "S" and $token->[1] eq "a" and $token->[2]{"name"} and $token->[2]{"name"} eq $id
not $header $token->[0] eq "S" $token->[1] eq "a" $token->[2]{"name"} $token->[2]{"name"} eq $id
Truth table0%0/6
not(A) and B and C and D and E
A= not $headerB= $token->[0] eq "S"C= $token->[1] eq "a"D= $token->[2]{"name"}E= $token->[2]{"name"} eq $id
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
133 && ($token->[2]->{name}) && ($token->[2]->{name} eq $id) ) {
134 0 $header = 1;
135 0 next;
136 }
137 0 ▶ $header or next;
Branch: 1 branch
BranchTrueFalseTotal
$header or next 0 0 0
138 0 ▶ if ( ($token->[0] eq 'E') && ($token->[1] eq 'blockquote') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->[0] eq "E" and $token->[1] eq "blockquote") 0 0 0
Condition0%0/3
$token->[0] eq "E" and $token->[1] eq "blockquote"
!ll&&!rl&&r
000
MC/DC0%0/2
$token->[0] eq "E" and $token->[1] eq "blockquote"
$token->[0] eq "E" $token->[1] eq "blockquote"
Truth table0%0/3
A and B
A= $token->[0] eq "E"B= $token->[1] eq "blockquote"
ABresult
0X0
100
111
139 0 $header = 0;
140 0 $inside = 0;
141 0 last;
142 }
143 0 ▶ if ( ($token->[0] eq 'S') && ($token->[1] eq 'blockquote') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->[0] eq "S" and $token->[1] eq "blockquote") 0 0 0
Condition0%0/3
$token->[0] eq "S" and $token->[1] eq "blockquote"
!ll&&!rl&&r
000
MC/DC0%0/2
$token->[0] eq "S" and $token->[1] eq "blockquote"
$token->[0] eq "S" $token->[1] eq "blockquote"
Truth table0%0/3
A and B
A= $token->[0] eq "S"B= $token->[1] eq "blockquote"
ABresult
0X0
100
111
144 0 $inside = 1;
145 0 next;
146 }
147 0 ▶ $inside or next;
Branch: 1 branch
BranchTrueFalseTotal
$inside or next 0 0 0
148  
149 0 ▶ if ( ($token->[0] eq 'S') && ($token->[1] eq 'p') ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($token->[0] eq 'S' and $token->[1] eq 'p') { } 0 0 0
elsif ($token->[0] eq 'T') { } 0 0 0
Condition0%0/3
$token->[0] eq "S" and $token->[1] eq "p"
!ll&&!rl&&r
000
MC/DC0%0/2
$token->[0] eq "S" and $token->[1] eq "p"
$token->[0] eq "S" $token->[1] eq "p"
Truth table0%0/3
A and B
A= $token->[0] eq "S"B= $token->[1] eq "p"
ABresult
0X0
100
111
150 0 $text .= "\n";
151 } elsif ( $token->[0] eq 'T' ) {
152 0 $text .= $token->[1];
153 }
154 }
155 0 return $text;
156 }
157  
158  
159  
160 0 ▶sub result_count {
Subroutine result_count: not called CC=2 SCAR=17.9
Pod undocumented
161 0 my $self = shift;
162  
163 0 ▶ unless ( $self->{_RESULTS} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_RESULTS"}) 0 0 0
164 0 return 0;
165 }
166 0 return scalar @{$self->{_RESULTS}};
167 }
168  
169  
170 3 ▶sub _is_hadith {
Subroutine _is_hadith: called CC=3 SCAR=16.5
171 3 my $el = shift;
172  
173 3 ▶ $el->tag() eq 'a' or return;
Branch: 1 branch
BranchTrueFalseTotal
$el->tag eq "a" or return 0 3 3
174 0 ▶ $el->attr('href') or return;
Branch: 1 branch
BranchTrueFalseTotal
$el->attr("href") or return 0 0 0
175  
176 0 return $el->attr('href') =~ m!bukhari!;
177 }
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188 3 ▶sub user_agent {
Subroutine user_agent: called CC=2 SCAR=6.9
Pod undocumented
189 3 my $self = shift;
190  
191 3 ▶ if ( defined $self->{_USER_AGENT} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"_USER_AGENT"}) 2 1 3
192 2 return $self->{_USER_AGENT};
193 }
194 1 require LWP::UserAgent;
195 1 my $ua = LWP::UserAgent->new(from => 'sherzodr@handalak.com');
196 1 $ua->agent( sprintf("%s (%s/%s)", $ua->agent(), ref($self), $self->VERSION) );
197  
198 1 $self->{_USER_AGENT} = $ua;
199 1 return $self->user_agent();
200 }
201  
202  
203 1 1;
204 __END__
↑ Summary