| 1 | package WWW::TasteKid; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | #$Id$ | ||||||||||||||||||||||||||||
| 4 | # /* vim:et: set ts=4 sw=4 sts=4 tw=78: */ | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 8 | ▶ | use 5.008001; # require perl 5.8.1 or later | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 8 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | #use criticism 'brutal'; | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 8 | ▶ | use version; our $VERSION = qv('0.1.3'); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 8 | ▶ | use Readonly; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 8 | ▶ | use XML::LibXML (); # qw/:libxml/; # :all :libxml, :encoding :w3c | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 8 | ▶ | use Carp qw/croak/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 8 | ▶ | use Encode qw/encode/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 8 | ▶ | use LWP::Simple qw/get/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 8 | ▶ | use Data::Dumper qw/Dumper/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 8 | ▶ | use Scalar::Util qw/refaddr/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 8 | ▶ | use URI::Escape qw/uri_escape/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 8 | ▶ | use HTML::Entities qw/decode_entities/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 8 | ▶ | use Class::InsideOut qw/public private/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | # should probably be using moose, just seems like | ||||||||||||||||||||||||||||
| 26 | # overkill for a module this simple/small | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 8 | ▶ | use WWW::TasteKidResult; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 8 | Readonly my $API_URL => 'http://www.tastekid.com/ask/ws?q='; | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 8 | private query_store => my %query_store; | |||||||||||||||||||||||||||
| 33 | 8 | public set_xml_result => my %xml_result; | |||||||||||||||||||||||||||
| 34 | 8 | public get_xml_result => %xml_result; | |||||||||||||||||||||||||||
| 35 | 8 | public get_encoded_query => my %encoded_query; | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 38 | 1 | my $class = shift; | |||||||||||||||||||||||||||
| 39 | 1 | my $self = bless \do { my $s }, $class; | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 1 | Class::InsideOut::register($self); | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 1 | $xml_result{ refaddr $self } = undef; | |||||||||||||||||||||||||||
| 44 | 1 | $encoded_query{ refaddr $self } = undef; | |||||||||||||||||||||||||||
| 45 | 1 | $query_store{ refaddr $self } = undef; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 1 | return $self; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 0 | ▶ | sub query { | ||||||||||||||||||||||||||
|
Subroutine
query: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 51 | 0 | my ( $self, $arg_ref ) = @_; | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 0 | ▶ | if ( !$arg_ref->{'name'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 0 | croak 'name argument is mandatory'; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 0 | ▶ | if ( !exists $query_store{'query'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 0 | $query_store{'query'} = []; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 0 | push @{ $query_store{'query'} }, $arg_ref; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 0 | return; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 0 | ▶ | sub query_inspection { | ||||||||||||||||||||||||||
|
Subroutine
query_inspection: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 67 | 0 | print Dumper $query_store{'query'}; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 0 | ▶ | sub ask { | ||||||||||||||||||||||||||
|
Subroutine
ask: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||||||||||
| 71 | 0 | my ( $self, $arg_ref ) = @_; | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 0 | my $query_str = q{}; | |||||||||||||||||||||||||||
| 74 | 0 | foreach my $q ( @{ $query_store{'query'} } ) { | |||||||||||||||||||||||||||
| 75 | 0 | ▶ | my $t = $q->{'type'} || q{}; | ||||||||||||||||||||||||||
|
Condition0%0/2
$q->{'type'} || ''
MC/DC0%0/1
$q->{'type'} || ''
$q->{'type'}
Truth table0%0/2
A
A= $q->{'type'}
| |||||||||||||||||||||||||||||
| 76 | 0 | ▶ | my $n = $q->{'name'} || q{}; | ||||||||||||||||||||||||||
|
Condition0%0/2
$q->{'name'} || ''
MC/DC0%0/1
$q->{'name'} || ''
$q->{'name'}
Truth table0%0/2
A
A= $q->{'name'}
| |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 0 | ▶ | if ($t) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 0 | $query_str .= "$t:"; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 0 | ▶ | if ($n) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 0 | $query_str .= "$n,"; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | # purge queries list | ||||||||||||||||||||||||||||
| 88 | 0 | delete $query_store{ refaddr $self }; | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 0 | $query_str =~ s/\,\z//xms; | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 0 | my $query = $API_URL . uri_escape($query_str); | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 0 | ▶ | if ( $arg_ref->{'filter'} ) { $query .= "//$arg_ref->{'filter'}" } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 0 | ▶ | if ( $arg_ref->{'verbose'} ) { $query .= '&verbose=1' } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 0 | $encoded_query{ refaddr $self } = $query; | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 0 | my $r = get($query); | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 0 | ▶ | if ( !$r ) { croak qq{unable to get $query} } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 0 | $self->set_xml_result($r); | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 0 | return; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 0 | ▶ | sub info_resource { | ||||||||||||||||||||||||||
|
Subroutine
info_resource: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 110 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 111 | 0 | return $self->_common_resource('info'); | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 0 | ▶ | sub results_resource { | ||||||||||||||||||||||||||
|
Subroutine
results_resource: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 115 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 116 | 0 | return $self->_common_resource('results'); | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 1 | ▶ | sub _common_resource { | ||||||||||||||||||||||||||
|
Subroutine
_common_resource: called CC=4 SCAR=24.1
| |||||||||||||||||||||||||||||
| 120 | 1 | my ( $self, $elem ) = @_; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 1 | ▶ | if ( caller ne 'WWW::TasteKid' ) { croak 'private method'; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 0 | my @return_req = (); | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 0 | my $parser = XML::LibXML->new(); | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 0 | my $tstkd_xml = $parser->parse_string( $self->get_xml_result ); | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 0 | my $xml_root = $tstkd_xml->documentElement; | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 0 | ▶ | if ( !$xml_root | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $xml_root or $xml_root->nodeName ne "similar"
MC/DC0%0/2
not $xml_root or $xml_root->nodeName ne "similar"
not $xml_root
$xml_root->nodeName ne "similar"
Truth table0%0/3
not(A) or B
A= not $xml_rootB= $xml_root->nodeName ne "similar"
| |||||||||||||||||||||||||||||
| 133 | || $xml_root->nodeName ne 'similar' ) | ||||||||||||||||||||||||||||
| 134 | { | ||||||||||||||||||||||||||||
| 135 | 0 | croak 'unknown file format recieved, cannot continue'; | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 0 | return _parse_response( $xml_root, $elem ); | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 1 | ▶ | sub _parse_response { | ||||||||||||||||||||||||||
|
Subroutine
_parse_response: called CC=8 SCAR=38.9
| |||||||||||||||||||||||||||||
| 142 | 1 | my ( $xml_root, $elem ) = @_; | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 1 | ▶ | if ( caller ne 'WWW::TasteKid' ) { croak 'private method'; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 0 | my $results_ref = []; | |||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | #TODO 3 nested foreach?! geez, refactor me | ||||||||||||||||||||||||||||
| 149 | 0 | foreach my $node ( $xml_root->childNodes ) { | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | #warn $node->toString; | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | #next unless $node->nodeName eq $elem; | ||||||||||||||||||||||||||||
| 154 | 0 | ▶ | if ( $node->nodeName ne $elem ) { next } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 0 | foreach my $c_node ( $node->childNodes ) { | |||||||||||||||||||||||||||
| 157 | 0 | ▶ | if ( $c_node->nodeName eq 'resource' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 0 | my $tkr = WWW::TasteKidResult->new; | |||||||||||||||||||||||||||
| 160 | 0 | foreach my $cc_node ( $c_node->childNodes ) { | |||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | #next unless $tkr->can( lc $cc_node->nodeName ); | ||||||||||||||||||||||||||||
| 163 | 0 | ▶ | if ( !$tkr->can( lc $cc_node->nodeName ) ) { next } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 0 | my $text_content = $cc_node->textContent; | |||||||||||||||||||||||||||
| 166 | 0 | $text_content = encode( 'utf8', $text_content ); | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 0 | $text_content = decode_entities($text_content); | |||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 0 | my $method_name = lc $cc_node->nodeName; | |||||||||||||||||||||||||||
| 171 | 0 | $tkr->$method_name($text_content); | |||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | 0 | push @{$results_ref}, $tkr; | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | 0 | return $results_ref; | |||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 8 | 1; | |||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | __END__ | ||||||||||||||||||||||||||||