TasteKid.pm

filter: Statementstmt 51.5% 65 / 126 Branchbran 7.6% 2 / 26 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 78.2% 18 / 23 Podpod 100% 6 / 6 totaltotal 47.4% 91 / 192 CC 21 SCAR 34.1
CC
21
Cov
42%
ask
90.0
_parse_response
49.0
query
12.0
CRAP 30.3
↑ Summary TasteKidResult.pm »
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
BranchTrueFalseTotal
unless ($arg_ref->{"name"}) 0 0 0
54 0 croak 'name argument is mandatory';
55 }
56  
57 0 ▶ if ( !exists $query_store{'query'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $query_store{"query"}) 0 0 0
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'} || ''
l!l
00
MC/DC0%0/1
$q->{'type'} || ''
$q->{'type'}
Truth table0%0/2
A
A= $q->{'type'}
Aresult
11
00
76 0 ▶ my $n = $q->{'name'} || q{};
Condition0%0/2
$q->{'name'} || ''
l!l
00
MC/DC0%0/1
$q->{'name'} || ''
$q->{'name'}
Truth table0%0/2
A
A= $q->{'name'}
Aresult
11
00
77  
78 0 ▶ if ($t) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t) 0 0 0
79 0 $query_str .= "$t:";
80 }
81  
82 0 ▶ if ($n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n) 0 0 0
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
BranchTrueFalseTotal
if ($arg_ref->{"filter"}) 0 0 0
95 0 ▶ if ( $arg_ref->{'verbose'} ) { $query .= '&verbose=1' }
Branch: 1 branch
BranchTrueFalseTotal
if ($arg_ref->{"verbose"}) 0 0 0
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
BranchTrueFalseTotal
unless ($r) 0 0 0
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
BranchTrueFalseTotal
if (caller ne "WWW::TasteKid") 1 0 1
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
BranchTrueFalseTotal
if (not $xml_root or $xml_root->nodeName ne "similar") 0 0 0
Condition0%0/3
not $xml_root or $xml_root->nodeName ne "similar"
l!l&&r!l&&!r
000
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"
ABresult
1X1
011
000
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
BranchTrueFalseTotal
if (caller ne "WWW::TasteKid") 1 0 1
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
BranchTrueFalseTotal
if ($node->nodeName ne $elem) 0 0 0
155  
156 0 foreach my $c_node ( $node->childNodes ) {
157 0 ▶ if ( $c_node->nodeName eq 'resource' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c_node->nodeName eq "resource") 0 0 0
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
BranchTrueFalseTotal
unless ($tkr->can(lc $cc_node->nodeName)) 0 0 0
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__
↑ Summary TasteKidResult.pm »