| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Scrabble::Dict; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 1 |  |  | 1 |  | 593 | use base qw/Exporter/; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 133 |  | 
| 4 | 1 |  |  | 1 |  | 1152 | use LWP::UserAgent; | 
|  | 1 |  |  |  |  | 63671 |  | 
|  | 1 |  |  |  |  | 40 |  | 
| 5 | 1 |  |  | 1 |  | 12 | use strict; | 
|  | 1 |  |  |  |  | 9 |  | 
|  | 1 |  |  |  |  | 651 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | our @EXPORT_OK = qw/scrabble_define/; | 
| 8 |  |  |  |  |  |  | our $VERSION = '0.01'; | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | 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 |  |  |  |  |  |  | sub new { | 
| 18 | 1 |  |  | 1 | 1 | 2 | my $this = shift; | 
| 19 | 1 |  | 33 |  |  | 7 | my $class = ref($this) || $this; | 
| 20 | 1 | 50 |  |  |  | 5 | my %init = @_ & 1 ? die "argument hash expected" : @_; | 
| 21 |  |  |  |  |  |  |  | 
| 22 | 1 |  | 33 |  |  | 23 | my $self = { | 
|  |  |  | 33 |  |  |  |  | 
|  |  |  | 33 |  |  |  |  | 
|  |  |  | 33 |  |  |  |  | 
| 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 |  |  |  |  |  |  | }; | 
| 28 |  |  |  |  |  |  |  | 
| 29 | 1 |  |  |  |  | 12 | $self->{_ua} = LWP::UserAgent->new(%init, cookie_jar => {}); | 
| 30 |  |  |  |  |  |  |  | 
| 31 | 1 |  |  |  |  | 11966 | return bless $self => $class; | 
| 32 |  |  |  |  |  |  | } | 
| 33 |  |  |  |  |  |  |  | 
| 34 |  |  |  |  |  |  | sub define { | 
| 35 | 1 |  |  | 1 | 1 | 4 | my ($self, $word) = (shift, lc shift); | 
| 36 | 1 |  |  |  |  | 7 | my $ua = $self->{_ua}; | 
| 37 |  |  |  |  |  |  |  | 
| 38 | 1 |  |  |  |  | 7 | 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 |  |  |  |  | 4 | my $url_home = "http://$self->{_host}$self->{_uri_home}"; | 
| 47 | 1 | 0 |  | 0 |  | 5 | my $cookie_scan = sub { $_[1] eq 'CFID' && $_[4] eq $self->{_host} }; | 
|  | 0 |  |  |  |  | 0 |  | 
| 48 |  |  |  |  |  |  |  | 
| 49 | 1 | 50 |  |  |  | 4 | $ua->get($url_home) if not $ua->cookie_jar->scan($cookie_scan); | 
| 50 |  |  |  |  |  |  |  | 
| 51 | 1 |  |  |  |  | 3534131 | my $url_dict = "http://$self->{_host}$self->{_uri_dict}"; | 
| 52 | 1 |  |  |  |  | 3 | my $html = ''; | 
| 53 |  |  |  |  |  |  |  | 
| 54 | 1 |  |  | 0 |  | 12 | $ua->post($url_dict, \%param, ':content_cb' => sub { $html .= shift }); | 
|  | 0 |  |  |  |  | 0 |  | 
| 55 |  |  |  |  |  |  |  | 
| 56 | 1 |  |  |  |  | 144931 | return $self->scrape_definition($html, $word); | 
| 57 |  |  |  |  |  |  | } | 
| 58 |  |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  | sub scrape_definition { | 
| 60 | 1 |  |  | 1 | 1 | 3 | my ($self, $html, $word) = (shift, shift, shift); | 
| 61 |  |  |  |  |  |  |  | 
| 62 | 1 | 50 |  |  |  | 47 | return $1 if $html =~ | 
| 63 |  |  |  |  |  |  | m{   | 
| 64 |  |  |  |  |  |  | .*? | 
| 65 |  |  |  |  |  |  |  | 
| 66 |  |  |  |  |  |  | \Q$word\E .*? \\ \s+ (.*?) | 
| 67 |  |  |  |  |  |  |  | 
| 68 |  |  |  |  |  |  | .*? | 
| 69 |  |  |  |  |  |  | }xsm; | 
| 70 |  |  |  |  |  |  |  | 
| 71 | 1 |  |  |  |  | 13 | return $self->{_undefined_as}; | 
| 72 |  |  |  |  |  |  | } | 
| 73 |  |  |  |  |  |  |  | 
| 74 |  |  |  |  |  |  | sub scrabble_define { | 
| 75 | 1 |  |  | 1 | 1 | 24 | my $word = shift; | 
| 76 | 1 |  |  |  |  | 10 | return __PACKAGE__->new(@_)->define($word); | 
| 77 |  |  |  |  |  |  | } | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | 1; | 
| 80 |  |  |  |  |  |  |  | 
| 81 |  |  |  |  |  |  | __END__ |