Google/Response.pm

filter: Statementstmt 30.0% 24 / 80 Branchbran 0.0% 0 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 41.1% 7 / 17 Podpod 75.0% 3 / 4 totaltotal 29.0% 34 / 117 CC 11 SCAR 18.7
CC
11
Cov
25%
init
20.0
new
6.0
resultElements
6.0
CRAP 6.5
« Google/tool.pm ↑ Summary Google/Service.pm »
1 {
2  
3 1 =head1 NAME
4  
5 Net::Google::Response - simple OOP-ish interface to the Google SOAP
6 API search responses
7  
8 =head1 SYNOPSIS
9  
10 my $service = Net::Google->new(key=>LOCAL_GOOGLE_KEY);
11 my $session = $service->search();
12  
13 $session->query(qw(Perl modules));
14  
15 # You are probably better off calling
16 # $session->results() but if you want
17 # the raw response object(s) here ya go :
18  
19 my $responses = $session->response();
20 my $count = scalar(@$responses);
21  
22 # $r is a Net::Google::Response object
23  
24 foreach my $r (@$responses) {
25 print sprintf("%s : %s\n",$r->searchQuery(),$r->estimatedTotalResults());
26 }
27  
28 =head1 DESCRIPTION
29  
30 Provides a simple OOP-ish interface to the Google SOAP API for
31 searching. This package is used by I<Net::Google>.
32  
33 The Net::Google::Response object is used to contain response
34 information provided by the Google search service in response to a
35 search query. The Response object allows the client program to easily
36 access the data returned from a search.
37  
38 Response data is accessed using methods with identical names to the
39 elements of a search response (as documented in the Google Web APIs
40 Reference, section 3). For instance, the first example in the SYNOPSIS
41 section, above, would return the estimated number of total results for
42 the query.
43  
44 Response objects may contain other response objects, where an element
45 would return an array of other elements. For instance, calling
46 C<$response-E<gt>resultElements()> will return a reference to an array
47 of Net::Google::Response objects, each one representing one result
48 from the search.
49  
50 The Response module will automatically provide methods for the search
51 response, as described by the service WSDL file. The results format
52 is described by the Google APIs documentation, to which you should
53 refer for the most up-to-date information. As of the April 8th, 2002
54 release of the Google APIs, the methods below are provided for each
55 search result.
56  
57 =cut
58  
59 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61 package Net::Google::Response;
62  
63 1 ▶use vars qw ($AUTOLOAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
64  
65 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
66  
67 1 $Net::Google::Response::VERSION = '1.0';
68  
69 # Note that we handle 'resultElements' separately
70 # Maybe we should doing the same w/ directoryCategories...
71  
72 1 ▶use constant RESPONSE_FIELDS => qw [ directoryCategories estimateIsExact startIndex searchTime estimatedTotalResultsCount searchTips searchComments searchQuery endIndex documentFiltering ];
Subroutine BEGIN: called CC=1 SCAR=0.0
73  
74 =head1 PACKAGE METHODS
75  
76 =cut
77  
78 =head2 __PACKAGE__->new(\%args)
79  
80 =cut
81  
82 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
83 0 my $pkg = shift;
84  
85 0 my $self = {};
86 0 bless $self,$pkg;
87  
88 0 ▶ if (! $self->init(@_)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->init(@_)) 0 0 0
89 0 return undef;
90 }
91  
92 0 return $self;
93 }
94  
95 0 ▶sub init {
Subroutine init: not called CC=4 SCAR=30.0
Pod undocumented
96 0 my $self = shift;
97 0 my $response = shift;
98  
99 0 ▶ if (ref($response) ne "GoogleSearchResult") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $response ne "GoogleSearchResult") 0 0 0
100 0 carp "Unknown response object.";
101 0 return 0;
102 }
103  
104 0 foreach my $el (@{$response->{'resultElements'}}) {
105 0 ▶ if (my $res = Result->new($el)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $res = "Result"->new($el)) 0 0 0
106 0 push @{$self->{'__resultElements'}},$res;
107 }
108 }
109  
110 0 map { $self->{'__'.$_} = $response->{$_}; } &RESPONSE_FIELDS;
111 0 return 1;
112 }
113  
114 =head1 I<Net::Google::Response> OBJECT METHODS
115  
116 =cut
117  
118 =head2 $response->documentFiltering()
119  
120 Returns 0 if false, 1 if true.
121  
122 =head2 $response->searchComments()
123  
124 Returns a string.
125  
126 =head2 $response->estimatedTotalResultsCount()
127  
128 Returns an integer.
129  
130 =head2 $response->estimateIsExact()
131  
132 Returns 0 if false, 1 if true.
133  
134 =head2 $response->resultElements()
135  
136 Returns a reference to an array of I<Response> objects.
137  
138 =cut
139  
140 0 ▶sub resultElements {
Subroutine resultElements: not called CC=2 SCAR=17.9
Pod documented
141 0 my $self = shift;
142  
143 0 ▶ if(ref($self->{'__resultElements'}) eq "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{"__resultElements"} eq "ARRAY") 0 0 0
144 0 return $self->{'__resultElements'};
145 }
146  
147 0 return [];
148 }
149  
150 =head2 $response->searchQuery()
151  
152 Returns a string.
153  
154 =head2 $response->startIndex()
155  
156 Returns an integer.
157  
158 =head2 $response->endIndex()
159  
160 Returns an integer.
161  
162 =head2 $response->searchTips()
163  
164 Returns a string.
165  
166 =head2 $response->directoryCategories()
167  
168 Returns a reference to an array of Response objects (one per directory
169 category -- see below).
170  
171 =head2 $response->searchTime()
172  
173 Returns a float.
174  
175 =head2 $pkg->to_string()
176  
177 =cut
178  
179 0 ▶sub to_string {
Subroutine to_string: not called CC=2 SCAR=17.9
Pod documented
180 0 my $self = shift;
181 0 my $string = '';
182  
183 0 foreach my $key (keys %$self) {
184 0 $string .= "$key: $$self{$key}\n";
185 }
186  
187 0 return $string;
188 }
189  
190 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=1 SCAR=6.9
191 0 return 1;
192 }
193  
194 0 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: not called CC=2 SCAR=17.9
195 0 my $self = shift;
196  
197 0 $AUTOLOAD =~ s/.*:://;
198
199 0 ▶ unless (grep/^($AUTOLOAD)$/,&RESPONSE_FIELDS) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep /^($Net::Google::Response::AUTOLOAD)$/, &RESPONSE_FIELDS) 0 0 0
200 0 carp "Unknown attribute : ".$AUTOLOAD;
201 0 return undef;
202 }
203  
204 0 return $self->{'__'.$AUTOLOAD};
205 }
206  
207 package Result;
208 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
209  
210 1 ▶use vars qw ($AUTOLOAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
211  
212 1 ▶use constant RESULT_FIELDS => qw [ title URL snippet cachedSize directoryTitle summary hostName directoryCategory relatedInformationPresent ];
Subroutine BEGIN: called CC=1 SCAR=0.0
213  
214 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
215 0 my $pkg = shift;
216  
217 0 my $self = {};
218 0 bless $self,$pkg;
219  
220 0 ▶ if (! $self->init(@_)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->init(@_)) 0 0 0
221 0 return undef;
222 }
223  
224 0 return $self;
225 }
226  
227 0 ▶sub init {
Subroutine init: not called CC=2 SCAR=17.9
228 0 my $self = shift;
229 0 my $res = shift;
230  
231 0 ▶ unless (ref($res) eq "ResultElement") {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $res eq "ResultElement") 0 0 0
232 0 carp "Unknown result object";
233 0 return 0;
234 }
235  
236 0 map { $self->{'__'.$_} = $res->{$_}; } &RESULT_FIELDS;
237 0 return 1;
238 }
239  
240 =head1 I<Result> OBJECT METHODS
241  
242 =cut
243  
244 =head2 $result->title()
245  
246 Returns a string.
247  
248 =head2 $result->URL()
249  
250 Returns a string.
251  
252 =head2 $result->snippet()
253  
254 Returns a string, formatted in HTML.
255  
256 =head2 $result->cachedSize()
257  
258 Returns a string.
259  
260 =head2 $result->directoryTitle()
261  
262 Returns a string.
263  
264 =head2 $result->summary()
265  
266 Returns a string.
267  
268 =head2 $result->hostName()
269  
270 Returns a string.
271  
272 =head2 $result->directoryCategory()
273  
274 Returns a hash reference.
275  
276 =cut
277  
278 0 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: not called CC=2 SCAR=17.9
279 0 my $self = shift;
280 0 $AUTOLOAD =~ s/.*:://;
281  
282 0 ▶ unless (grep/^($AUTOLOAD)$/,&RESULT_FIELDS) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep /^($Result::AUTOLOAD)$/, &RESULT_FIELDS) 0 0 0
283 0 carp "Unknown attribute :".$AUTOLOAD;
284 0 return undef;
285 }
286  
287 0 return $self->{'__'.$AUTOLOAD};
288 }
289  
290 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=1 SCAR=6.9
291 0 return 1;
292 }
293  
294 =head1 VERSION
295  
296 1.0
297  
298 =head1 DATE
299  
300 $Date: 2005/04/02 21:51:16 $
301  
302 =head1 AUTHOR
303  
304 Aaron Straup Cope
305  
306 =head1 CONTRIBUTORS
307  
308 Marc Hedlund <marc@precipice.org>
309  
310 =head1 SEE ALSO
311  
312 L<Net::Google>
313  
314 =head1 LICENSE
315  
316 Copyright (c) 2002-2005, Aaron Straup Cope. All Rights Reserved.
317  
318 This is free software, you may use it and distribute it under the
319 same terms as Perl itself.
320  
321 =cut
322  
323 1 return 1;
324  
325 }
« Google/tool.pm ↑ Summary Google/Service.pm »