blib/lib/Scrabble/Dict.pm

filter: Statementstmt 94.4% 34 / 36 Branchbran 37.5% 3 / 8 Conditioncond 33.3% 5 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 77.7% 7 / 9 Podpod 100% 4 / 4 totaltotal 64.6% 53 / 82 CC 10 SCAR 17.9
CC
10
Cov
71%
new
12.4
__ANON__
3.7
define
2.1
CRAP 6.0
↑ Summary
1 package Scrabble::Dict;
2  
3 1 ▶use base qw/Exporter/;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our @EXPORT_OK = qw/scrabble_define/;
8 1 our $VERSION = '0.01';
9  
10 1 my %defaults = (
11 host => 'www.hasbro.com',
12 uri_home => '/games/adult-games/scrabble/home.cfm',
13 uri_dict => '/games/adult-games/scrabble/home.cfm?page=Dictionary/dict',
14 undefined_as => ''
15 );
16  
17 1 ▶sub new {
Subroutine new: called CC=7 SCAR=25.1
Pod documented
18 1 my $this = shift;
19 1 ▶ my $class = ref($this) || $this;
Condition33%1/3
ref $this || $this
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table33%1/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
20 1 ▶ my %init = @_ & 1 ? die "argument hash expected" : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ & 1 ? : 0 1 1
21  
22 my $self = {
23 _host => delete $init{host} || $defaults{host},
24 _uri_home => delete $init{uri_home} || $defaults{uri_home},
25 _uri_dict => delete $init{uri_dict} || $defaults{uri_dict},
26 _undefined_as => delete $init{undefined_as} || $defaults{undefined_as},
27 1 ▶ };
Condition33%4/12
delete $init{'host'} || $defaults{'host'}
l!l&&r!l&&!r
010
delete $init{'uri_home'} || $defaults{'uri_home'}
l!l&&r!l&&!r
010
delete $init{'uri_dict'} || $defaults{'uri_dict'}
l!l&&r!l&&!r
010
delete $init{'undefined_as'} || $defaults{'undefined_as'}
l!l&&r!l&&!r
001
MC/DC0%0/2
delete $init{'host'} || $defaults{'host'}
delete $init{'host'} $defaults{'host'}
MC/DC0%0/2
delete $init{'uri_home'} || $defaults{'uri_home'}
delete $init{'uri_home'} $defaults{'uri_home'}
MC/DC0%0/2
delete $init{'uri_dict'} || $defaults{'uri_dict'}
delete $init{'uri_dict'} $defaults{'uri_dict'}
MC/DC0%0/2
delete $init{'undefined_as'} || $defaults{'undefined_as'}
delete $init{'undefined_as'} $defaults{'undefined_as'}
Truth table33%1/3
A || B
A= delete $init{'host'}B= $defaults{'host'}
ABresult
1X1
011
000
Truth table33%1/3
A || B
A= delete $init{'uri_home'}B= $defaults{'uri_home'}
ABresult
1X1
011
000
Truth table33%1/3
A || B
A= delete $init{'uri_dict'}B= $defaults{'uri_dict'}
ABresult
1X1
011
000
Truth table33%1/3
A || B
A= delete $init{'undefined_as'}B= $defaults{'undefined_as'}
ABresult
1X1
011
000
28  
29 1 $self->{_ua} = LWP::UserAgent->new(%init, cookie_jar => {});
30  
31 1 return bless $self => $class;
32 }
33  
34 1 ▶sub define {
Subroutine define: called CC=2 SCAR=7.5
Pod documented
35 1 my ($self, $word) = (shift, lc shift);
36 1 my $ua = $self->{_ua};
37  
38 1 my %param = (
39 type => 'dictionary',
40 exact => 'on',
41 Word => $word
42 );
43  
44 # hasbro site forces us to eat ColdFusion cookies the first time
45  
46 1 my $url_home = "http://$self->{_host}$self->{_uri_home}";
47 0 ▶ my $cookie_scan = sub { $_[1] eq 'CFID' && $_[4] eq $self->{_host} };
Subroutine __ANON__: not called CC=2 SCAR=13.0
Branch: 1 branch
BranchTrueFalseTotal
$_[1] eq "CFID" and $_[4] eq $self->{"_host"} 0 0 0
48  
49 1 ▶ $ua->get($url_home) if not $ua->cookie_jar->scan($cookie_scan);
Branch: 1 branch
BranchTrueFalseTotal
unless $ua->cookie_jar->scan($cookie_scan) 1 0 1
50  
51 1 my $url_dict = "http://$self->{_host}$self->{_uri_dict}";
52 1 my $html = '';
53  
54 0 ▶ $ua->post($url_dict, \%param, ':content_cb' => sub { $html .= shift });
Subroutine __ANON__: not called CC=1 SCAR=1.2
55  
56 1 return $self->scrape_definition($html, $word);
57 }
58  
59 1 ▶sub scrape_definition {
Subroutine scrape_definition: called CC=2 SCAR=7.1
Pod documented
60 1 my ($self, $html, $word) = (shift, shift, shift);
61  
62 1 ▶ return $1 if $html =~
Branch: 1 branch
BranchTrueFalseTotal
if $html =~ m[<div \s+ id="dict_return"> .*? <span \s+ .*?> \Q$word\E .*? \\ \s+ (.*?) </span> .*? </div>]msx 0 1 1
63 m{<div \s+ id="dict_return">
64 .*?
65 <span \s+ .*?>
66 \Q$word\E .*? \\ \s+ (.*?)
67 </span>
68 .*?
69 </div>}xsm;
70  
71 1 return $self->{_undefined_as};
72 }
73  
74 1 ▶sub scrabble_define {
Subroutine scrabble_define: called CC=1 SCAR=0.0
Pod documented
75 1 my $word = shift;
76 1 return __PACKAGE__->new(@_)->define($word);
77 }
78  
79 1 1;
80  
81 __END__
↑ Summary