Google.pm

filter: Statementstmt 85.1% 40 / 47 Branchbran 40.0% 4 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 81.8% 9 / 11 Podpod 71.4% 5 / 7 totaltotal 77.3% 58 / 75 CC 7 SCAR 9.7
CC
7
Cov
77%
http_proxy
6.0
_parse_args
4.1
new
2.1
CRAP 2.6
« Google/Cache.pm ↑ Summary
1 {
2  
3 3 =head1 NAME
4  
5 Net::Google - simple OOP-ish interface to the Google SOAP API
6  
7 =head1 SYNOPSIS
8  
9 use Net::Google;
10 use constant LOCAL_GOOGLE_KEY => "********************************";
11  
12 my $google = Net::Google->new(key=>LOCAL_GOOGLE_KEY);
13 my $search = $google->search();
14  
15 # Search interface
16  
17 $search->query(qw(aaron straup cope));
18 $search->lr(qw(en fr));
19 $search->starts_at(5);
20 $search->max_results(15);
21  
22 map { print $_->title()."\n"; } @{$search->results()};
23  
24 # or...
25  
26 foreach my $r (@{$search->response()}) {
27 print "Search time :".$r->searchTime()."\n";
28  
29 # returns an array ref of Result objects
30 # the same as the $search->results() method
31 map { print $_->URL()."\n"; } @{$r->resultElements()};
32 }
33  
34 # Spelling interface
35  
36 print $google->spelling(phrase=>"muntreal qwebec")->suggest(),"\n";
37  
38 # Cache interface
39  
40 my $cache = $google->cache(url=>"http://search.cpan.org/recent");
41 print $cache->get();
42  
43 =head1 DESCRIPTION
44  
45 Provides a simple OOP-ish interface to the Google SOAP API
46  
47 =head1 ENCODING
48  
49 According to the Google API docs :
50  
51 "In order to support searching documents in multiple languages
52 and character encodings the Google Web APIs perform all requests
53 and responses in the UTF-8 encoding. The parameters <ie> and
54 <oe> are required in client requests but their values are ignored.
55 Clients should encode all request data in UTF-8 and should expect
56 results to be in UTF-8."
57  
58 (This package takes care of setting both parameters in requests.)
59  
60 =cut
61  
62 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
63  
64 package Net::Google;
65 3 ▶use base qw (Net::Google::tool);
Subroutine BEGIN: called CC=1 SCAR=0.0
66  
67 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
68  
69 3 $Net::Google::VERSION = '1.0';
70  
71 3 $Net::Google::QUERY_LIMIT = 1000;
72 3 $Net::Google::KEY_QUERIES = {};
73  
74 =head1 PACKAGE METHODS
75  
76 =cut
77  
78 =head2 __PACKAGE__->new(\%args)
79  
80 Valid arguments are :
81  
82 =over 4
83  
84 =item *
85  
86 B<key>
87  
88 I<string>. A Google API key.
89  
90 =item *
91  
92 B<http_proxy>
93  
94 I<url>. A URL for proxy-ing HTTP requests.
95  
96 =item *
97  
98 B<debug>
99  
100 Valid options are:
101  
102 =over 4
103  
104 =item *
105  
106 I<boolean>
107  
108 If true prints debugging information returned by SOAP::Lite
109 to STDERR
110  
111 =item *
112  
113 I<coderef>.
114  
115 Your own subroutine for munging the debugging information
116 returned by SOAP::Lite.
117  
118 =back
119  
120 =back
121  
122 Note that prior to version 0.60, arguments were not passed
123 by reference. Versions >= 0.60 are backwards compatible.
124  
125 Returns an object. Woot!
126  
127 =cut
128  
129 3 ▶sub new {
Subroutine new: called CC=2 SCAR=7.2
Pod documented
130 3 my $pkg = shift;
131  
132 3 my $self = {};
133 3 bless $self,$pkg;
134  
135 3 ▶ if (! $self->init(@_)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->init(@_)) 0 3 3
136 0 return undef;
137 }
138  
139 3 return $self;
140 }
141  
142 3 ▶sub init {
Subroutine init: called CC=2 SCAR=7.0
Pod undocumented
143 3 my $self = shift;
144 3 ▶ my $args = (ref($_[0]) eq "HASH") ? shift : {@_};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 3 3
145  
146 3 $self->{'_debug'} = $args->{'debug'};
147 3 $self->{'_key'} = $args->{'key'};
148 3 $self->{'_http_proxy'} = $args->{'http_proxy'};
149  
150 # Do *not* call parent
151 # class' init() method.
152  
153 3 return 1;
154 }
155  
156 =head1 OBJECT METHODS
157  
158 =cut
159  
160 =head2 $obj->key($string)
161  
162 Get/set the Google API key for this object.
163  
164 =cut
165  
166 # Defined in Net::Google::tool
167  
168 =head2 $obj->http_proxy($url)
169  
170 Get/set the HTTP proxy for this object.
171  
172 Returns a string.
173  
174 =cut
175  
176 # Note we subclass the method normally inherited
177 # from ::tool.pm. Since this is just a wrapper
178 # module, we don't have a SOAP thingy to actually
179 # set the proxy for so we'll just cache it for
180 # later.
181  
182 0 ▶sub http_proxy {
Subroutine http_proxy: not called CC=2 SCAR=17.9
Pod documented
183 0 my $self = shift;
184 0 my $uri = shift;
185  
186 0 ▶ if ($uri) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri) 0 0 0
187 0 $self->{'_http_proxy'} = $uri;
188 }
189  
190 0 return $self->{'_http_proxy'};
191 }
192  
193 =head2 $obj->search(\%args)
194  
195 Valid arguments are :
196  
197 =over 4
198  
199 =item *
200  
201 B<key>
202  
203 I<string>. A Google API key.
204  
205 If none is provided then the key passed to the parent
206 I<Net::Google> object will be used.
207  
208 =item *
209  
210 B<starts_at>
211  
212 I<int>. First result number to display.
213  
214 Default is 0.
215  
216 =item *
217  
218 B<max_results>
219  
220 I<int>. Number of results to return.
221  
222 Default is 10.
223  
224 =item *
225  
226 B<lr>
227  
228 I<string> or I<array reference>. Language restrictions.
229  
230 =item *
231  
232 B<safe>
233  
234 I<boolean>.
235  
236 =item *
237  
238 B<filter>
239  
240 I<boolean>.
241  
242 =item *
243  
244 B<http_proxy>
245  
246 I<url>. A URL for proxy-ing HTTP requests.
247  
248  
249 =item *
250  
251 B<debug>
252  
253 Valid options are:
254  
255 =over 4
256  
257 =item *
258  
259 I<boolean>
260  
261 If true prints debugging information returned by SOAP::Lite
262 to STDERR
263  
264 =item *
265  
266 I<coderef>
267  
268 Your own subroutine for munging the debugging information
269 returned by SOAP::Lite.
270  
271 =back
272  
273 =back
274  
275 Note that prior to version 0.60, arguments were not passed
276 by reference. Versions >= 0.60 are backwards compatible.
277  
278 Returns a I<Net::Google::Search> object. Woot!
279  
280 Returns undef if there was an error.
281  
282 =cut
283  
284 1 ▶sub search {
Subroutine search: called CC=1 SCAR=0.0
Pod documented
285 1 my $self = shift;
286 1 require Net::Google::Search;
287 1 return Net::Google::Search->new($self->_parse_args(@_));
288 }
289  
290 =head2 $obj->spelling(\%args)
291  
292 Valid arguments are:
293  
294 =over 4
295  
296 =item *
297  
298 B<key>
299  
300 I<string>. A Google API key.
301  
302 If none is provided then the key passed to the parent
303 I<Net::Google> object will be used.
304  
305 =item *
306  
307 B<phrase>
308  
309 I<string> or I<array reference>.
310  
311 =item *
312  
313 B<http_proxy>
314  
315 I<url>. A URL for proxy-ing HTTP requests.
316  
317 =item *
318  
319 B<debug>
320  
321 =over 4
322  
323 =item *
324  
325 B<boolean>
326  
327 Prints debugging information returned by SOAP::Lite to STDERR
328  
329 =item *
330  
331 B<coderef>
332  
333 Your own subroutine for munging the debugging information
334 returned by SOAP::Lite.
335  
336 =back
337  
338 If no option is defined then the debug argument passed to the parent
339 I<Net::Google> object will be used.
340  
341 =back
342  
343 Note that prior to version 0.60, arguments were not passed
344 by reference. Versions >= 0.60 are backwards compatible.
345  
346 Returns a I<Net::Google::Spelling> object. Woot!
347  
348 Returns undef if there was an error.
349  
350 =cut
351  
352 1 ▶sub spelling {
Subroutine spelling: called CC=1 SCAR=0.0
Pod documented
353 1 my $self = shift;
354 1 require Net::Google::Spelling;
355 1 return Net::Google::Spelling->new($self->_parse_args(@_));
356 }
357  
358 # Small things are good because you can
359 # fit them in your hand *and* your mouth.
360  
361 0 ▶sub speling { return shift->spelling(@_); }
Subroutine speling: not called CC=1 SCAR=6.9
Pod undocumented
362  
363 =head2 $obj->cache(\%args)
364  
365 Valid arguments are :
366  
367 =over 4
368  
369 =item *
370  
371 B<key>
372  
373 String. Google API key.
374  
375 If none is provided then the key passed to the parent I<Net::Google>
376 object will be used.
377  
378 =item *
379  
380 B<url>
381  
382 I<string>
383  
384 =item *
385  
386 B<http_proxy>
387  
388 I<url>. A URL for proxy-ing HTTP requests.
389  
390 =item *
391  
392 B<debug>
393  
394 Valid options are:
395  
396 =over 4
397  
398 =item *
399  
400 I<boolean>
401  
402 If true, prints debugging information returned by SOAP::Lite
403 to STDERR
404  
405 =item *
406  
407 I<coderef>
408  
409 Your own subroutine for munging the debugging information
410 returned by SOAP::Lite.
411  
412 =back
413  
414 If no option is defined then the debug argument passed to the parent
415 I<Net::Google> object will be used.
416  
417 =back
418  
419 Note that prior to version 0.60, arguments were not passed
420 by reference. Versions >= 0.60 are backwards compatible.
421  
422 Returns a I<Net::Google::Cache> object. Woot!
423  
424 Returns undef if there was an error.
425  
426 =cut
427  
428 1 ▶sub cache {
Subroutine cache: called CC=1 SCAR=0.0
Pod documented
429 1 my $self = shift;
430 1 require Net::Google::Cache;
431 1 return Net::Google::Cache->new($self->_parse_args(@_));
432 }
433  
434 =head2 $obj->queries_exhausted()
435  
436 Returns true or false depending on whether or not the current in-memory
437 B<session> has exhausted the Google API 1000 query limit.
438  
439 =cut
440  
441 # Defined in Net::Google::tool
442  
443 #
444  
445 3 ▶sub _parse_args {
Subroutine _parse_args: called CC=4 SCAR=14.2
446 3 my $self = shift;
447 3 ▶ my $args = (ref($_[0]) eq "HASH") ? shift : {@_};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 3 3
448  
449 3 foreach my $el ("key","debug","http_proxy") {
450 9 ▶ next if (defined($args->{$el}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $args->{$el} 0 9 9
451 9 $args->{$el} = $self->{"_$el"};
452 }
453  
454 3 return $args;
455 }
456  
457 =head1 VERSION
458  
459 1.0
460  
461 =head1 DATE
462  
463 $Date: 2005/03/26 20:49:03 $
464  
465 =head1 AUTHOR
466  
467 Aaron Straup Cope
468  
469 =head1 CONTRIBUTORS
470  
471 Marc Hedlund <marc@precipice.org>
472  
473 =head1 SEE ALSO
474  
475 http://www.google.com/apis
476  
477 L<Net::Google::Search>
478  
479 L<Net::Google::Spelling>
480  
481 L<Net::Google::Cache>
482  
483 L<Net::Google::Response>
484  
485 L<Net::Google::Service>
486  
487 http://aaronland.info/weblog/archive/4231
488  
489 =head1 TO DO
490  
491 =over 4
492  
493 =item *
494  
495 Tickle the tests so that they will pass on systems without
496 Test::More.
497  
498 =item *
499  
500 Add tests for filters.
501  
502 =item *
503  
504 Add some sort of functionality for managing multiple keys.
505 Sort of like what is describe here :
506  
507 http://aaronland.net/weblog/archive/4204
508  
509 This will probably happen around the time Hell freezes over
510 so if you think you can do it faster, go nuts.
511  
512 =back
513  
514 =head1 BUGS
515  
516 Please report all bugs via http://rt.cpan.org
517  
518 =head1 LICENSE
519  
520 Copyright (c) 2002-2005, Aaron Straup Cope. All Rights Reserved.
521  
522 This is free software, you may use it and distribute it under the
523 same terms as Perl itself.
524  
525 =cut
526  
527 3 return 1;
528  
529 }
« Google/Cache.pm ↑ Summary