Search/Response.pm

filter: Statementstmt 7.7% 8 / 103 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 8.7% 2 / 23 Podpod 95.0% 19 / 20 totaltotal 15.4% 29 / 188 CC 19 SCAR 29.7
CC
19
Cov
6%
NextResult
56.0
new
42.0
NextRequest
20.0
CRAP 19.6
« Search.pm ↑ Summary
1 package Yahoo::Search::Response;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use Yahoo::Search::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 our $VERSION = "20100614.1"; # just to make CPAN happy
6  
7 =head1 NAME
8  
9  
10 =head1 VERSION
11  
12 version 1.11.3
13 Yahoo::Search::Response -- Container object for the result set of one query
14 to the Yahoo! Search API.
15 (This package is included in, and automatically loaded by, the Yahoo::Search package.)
16  
17 =head1 Package Use
18  
19 You never need to C<use> this package directly -- it is loaded
20 automatically by Yahoo::Search.
21  
22 =head1 Object Creation
23  
24 C<Response> objects are created by the C<Fetch()> method of a C<Request>
25 (Yahoo::Search::Request) object, e.g. by
26  
27 my $Response = Yahoo::Search->new(...)->Request()->Fetch();
28  
29 or by shortcuts to the same, such as:
30  
31 my $Response = Yahoo::Search->Query(...);
32  
33 =cut
34  
35 ##
36 ## Called from Request.pm after grokking the xml returned as the results of
37 ## a specific Request.
38 ##
39 sub new
40 0 ▶{
Subroutine new: not called CC=6 SCAR=37.4
Pod undocumented
41 0 my $class = shift;
42 0 my $Response = shift; # hashref of info
43  
44 ## We have the data; now bless it
45 0 bless $Response, $class;
46  
47 ## Initialize iterator for NextResult() method
48 0 $Response->{_NextIterator} = 0;
49  
50 ## But do a bit of cleanup and other preparation....
51 0 ▶ if (not $Response->{firstResultPosition}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Response->{"firstResultPosition"}) 0 0 0
52 ## Y! server bug -- this is sometimes empty
53 0 $Response->{firstResultPosition} = 1;
54 }
55  
56 ##
57 ## Fix up and bless each internal "Result" item -- turn into a Result
58 ## object. Set the ordinal to support the i() and I() methods.
59 ##
60 0 ▶ for (my $i = 0; $i < @{$Response->{Result}}; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$Response->{"Result"};}) 0 0 0
61 {
62 0 my $Result = $Response->{Result}->[$i];
63  
64 0 $Result->{_ResponseOrdinal} = $i;
65 0 $Result->{_Response} = $Response;
66  
67 ##
68 ## Something like
69 ## <Channels></Channels>
70 ## ends up being a ref to an empty hash. We'll remove those.
71 ##
72 0 for my $key (keys %$Result)
73 {
74 0 ▶ if (ref($Result->{$key}) eq "HASH"
Branch: 1 branch
BranchTrueFalseTotal
if (ref $Result->{$key} eq "HASH" and not keys %{$$Result{$key};}) 0 0 0
Condition0%0/3
ref $Result->{$key} eq "HASH" and not keys %{$$Result{$key};}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $Result->{$key} eq "HASH" and not keys %{$$Result{$key};}
ref $Result->{$key} eq "HASH" not keys %{$$Result{$key};}
Truth table0%0/3
A and not(B)
A= ref $Result->{$key} eq "HASH"B= not keys %{$$Result{$key};}
ABresult
0X0
100
111
75 and
76 0 not keys %{$Result->{$key}})
77 {
78 0 delete $Result->{$key};
79 }
80 }
81  
82 0 bless $Result, "Yahoo::Search::Result";
83 }
84  
85 0 return $Response;
86 }
87  
88  
89 =head1 Methods
90  
91 A C<Response> object has the following methods:
92  
93 =over 4
94  
95 =cut
96  
97  
98 ###########################################################################
99  
100 =item $Response->Count()
101  
102 Returns the number of C<Result> objects available in this C<Response>. See
103 Yahoo::Search::Result for details on C<Result> objects.
104  
105 =cut
106  
107 sub Count
108 0 ▶{
Subroutine Count: not called CC=1 SCAR=6.9
Pod documented
109 0 my $Response = shift; #self;
110 0 return scalar @{$Response->{Result}};
111 }
112  
113  
114  
115  
116 ###########################################################################
117 sub _commaize($$)
118 0 ▶{
Subroutine _commaize: not called CC=2 SCAR=17.9
119 0 my $num = shift;
120 0 my $comma = shift; # "," (English), "." (European), undef.....
121  
122 0 ▶ if ($comma) {
Branch: 1 branch
BranchTrueFalseTotal
if ($comma) 0 0 0
123 0 $num =~ s/(?<=\d)(?=(?:\d\d\d)+$)/$comma/g;
124 }
125 0 return $num;
126 }
127 ###########################################################################
128  
129 =item $Response->FirstOrdinal([ I<separator> ])
130  
131 Returns the index of the first C<Result> object (e.g. the "30" of I<results
132 30 through 40 out of 5,329>). This is the same as the C<Start> arg of the
133 C<Request> that generated this C<Response>.
134  
135 If an optional argument is given and is true, it is used as a separator
136 every three digits. In the US, one would use
137  
138 $Response->FirstOrdinal(',')
139  
140 to return, say, "1,230" instead of the "1230" that
141  
142 $Response->FirstOrdinal()
143  
144 might return.
145  
146 =cut
147  
148 sub FirstOrdinal
149 0 ▶{
Subroutine FirstOrdinal: not called CC=2 SCAR=17.9
Pod documented
150 0 my $Response = shift; #self;
151 0 my $Comma = shift; # optional
152  
153 ## do the '-1' to convert from Y!'s 1-based system to our 0-based system
154 0 ▶ return _commaize(($Response->{firstResultPosition}||0) - 1, $Comma);
Condition0%0/2
$Response->{'firstResultPosition'} || 0
l!l
00
MC/DC0%0/1
$Response->{'firstResultPosition'} || 0
$Response->{'firstResultPosition'}
Truth table0%0/2
A
A= $Response->{'firstResultPosition'}
Aresult
11
00
155 }
156  
157  
158  
159 ###########################################################################
160  
161 =item $Response->CountAvail([ I<separator> ])
162  
163 Returns an approximate number of total search results available, were you
164 to ask for them all (e.g. the "5329" of the I<results 30 through 40 out of
165 5329>).
166  
167 If an optional argument is given and is true, it is used as a separator
168 every three digits. In the US, one would use
169  
170 $Response->CountAvail(',')
171  
172 to return, say, "5,329" instead of the "5329" that
173  
174 $Response->CountAvail()
175  
176 might return.
177  
178 =cut
179  
180 sub CountAvail
181 0 ▶{
Subroutine CountAvail: not called CC=2 SCAR=17.9
Pod documented
182 0 my $Response = shift; #self;
183 0 my $Comma = shift; # optional
184 0 ▶ return _commaize($Response->{totalResultsAvailable} || 0, $Comma)
Condition0%0/2
$Response->{'totalResultsAvailable'} || 0
l!l
00
MC/DC0%0/1
$Response->{'totalResultsAvailable'} || 0
$Response->{'totalResultsAvailable'}
Truth table0%0/2
A
A= $Response->{'totalResultsAvailable'}
Aresult
11
00
185 }
186  
187  
188  
189 ###########################################################################
190  
191 =item $Response->Links()
192  
193 Returns a list of links from the response (one link per result):
194  
195 use Yahoo::Search;
196 if (my $Response = Yahoo::Search->Query(Doc => 'Britney'))
197 {
198 for my $link ($Response->Links) {
199 print "<br>$link\n";
200 }
201 }
202  
203 This prints one
204  
205 <br><a href="...">title of the link</a>
206  
207 line per result returned from the query.
208  
209 (I<Not appropriate for B<Spell> and B<Related> search results>)
210  
211 =cut
212  
213 sub Links
214 0 ▶{
Subroutine Links: not called CC=1 SCAR=6.9
Pod documented
215 0 my $Response = shift; #self;
216 0 return map { $_->Link } $Response->Results;
217 }
218  
219  
220  
221  
222 ###########################################################################
223  
224 =item $Response->Terms()
225  
226 (I<Appropriate for B<Spell> and B<Related> search results>)
227  
228 Returns a list of text terms.
229  
230 =cut
231  
232 sub Terms
233 0 ▶{
Subroutine Terms: not called CC=1 SCAR=6.9
Pod documented
234 0 my $Response = shift; #self;
235 0 return map { $_->Terms } $Response->Results;
236 }
237  
238  
239  
240  
241 ###########################################################################
242  
243 =item $Response->Results()
244  
245 Returns a list of Yahoo::Search::Result C<Result> objects representing
246 all the results held in this C<Response>. For example:
247  
248 use Yahoo::Search;
249 if (my $Response = Yahoo::Search->Query(Doc => 'Britney'))
250 {
251 for my $Result ($Response->Results) {
252 printf "%d: %s\n", $Result->I, $Result->Url;
253 }
254 }
255  
256 This is not valid for I<Spell> and I<Related> searches.
257  
258 =cut
259  
260 sub Results
261 0 ▶{
Subroutine Results: not called CC=1 SCAR=6.9
Pod documented
262 0 my $Response = shift; #self;
263 0 return @{$Response->{Result}};
264 }
265  
266  
267  
268  
269 ###########################################################################
270  
271 =item $Response->NextResult(options)
272  
273 Returns a C<Result> object, or nothing. (On error, returns nothing and sets
274 C<$@>.)
275  
276 The first time C<NextResult> is called for a given C<Response> object, it
277 returns the C<Result> object for the first result in the set. Returns
278 subsequent C<Result> objects for subsequent calls, until there are none
279 left, at which point what is returned depends upon whether the
280 auto-continuation feature is turned on (more on that in a moment).
281  
282 The following produces the same results as the C<Results()> example above:
283  
284 use Yahoo::Search;
285 if (my $Response = Yahoo::Search->Query(Doc => 'Britney')) {
286 while (my $Result = $Response->NextResult) {
287 printf "%d: %s\n", $Result->I, $Result->Url;
288 }
289 }
290  
291 B<Auto-Continuation>
292  
293 If auto-continuation is turned on, then upon reaching the end of the result
294 set, C<NextResult> automatically fetches the next set of results and
295 returns I<its> first result.
296  
297 This can be convenient, but B<can be very dangerous>, as it means that a
298 loop which calls C<NextResult>, unless otherwise exited, will fetch results
299 from Yahoo! until there are no more results for the query, or until you
300 have exhausted your access limits.
301  
302 Auto-continuation can be turned on in several ways:
303  
304 =over 3
305  
306 =item *
307  
308 On a per C<NextResult> basis by calling as
309  
310 $Response->NextResult(AutoContinue => 1)
311  
312 as with this example
313  
314 use Yahoo::Search;
315 ##
316 ## WARNING: DANGEROUS DANGEROUS DANGEROUS
317 ##
318 if (my $Response = Yahoo::Search->Query(Doc => 'Britney')) {
319 while (my $Result = $Response->NextResult(AutoContinue => 1)) {
320 printf "%d: %s\n", $Result->I, $Result->Url;
321 }
322 }
323  
324  
325 =item *
326  
327 By using
328  
329 AutoContinue => 1
330  
331 when creating the request (e.g. in a Yahoo::Search->Query call), as
332 with this example:
333  
334 use Yahoo::Search;
335 ##
336 ## WARNING: DANGEROUS DANGEROUS DANGEROUS
337 ##
338 if (my $Response = Yahoo::Search->Query(Doc => 'Britney',
339 AutoContinue => 1))
340 {
341 while (my $Result = $Response->NextResult) {
342 printf "%d: %s\n", $Result->I, $Result->Url;
343 }
344 }
345  
346 =item *
347  
348 By creating a query via a search-engine object created with
349  
350 AutoContinue => 1
351  
352 as with this example:
353  
354 use Yahoo::Search;
355 ##
356 ## WARNING: DANGEROUS DANGEROUS DANGEROUS
357 ##
358 my $SearchEngine = Yahoo::Search->new(AutoContinue => 1);
359  
360 if (my $Response = $SearchEngine->Query(Doc => 'Britney')) {
361 while (my $Result = $Response->NextResult) {
362 printf "%d: %s\n", $Result->I, $Result->Url;
363 }
364 }
365  
366  
367 =item *
368  
369 By creating a query when Yahoo::Search had been loaded via:
370  
371 use Yahoo::Search AutoContinue => 1;
372  
373 as with this example:
374  
375 use Yahoo::Search AutoContinue => 1;
376 ##
377 ## WARNING: DANGEROUS DANGEROUS DANGEROUS
378 ##
379 if (my $Response = Yahoo::Search->Query(Doc => 'Britney')) {
380 while (my $Result = $Response->NextResult) {
381 printf "%d: %s\n", $Result->I, $Result->Url;
382 }
383 }
384  
385  
386 =back
387  
388  
389 All these examples are dangerous because they loop through results,
390 fetching more and more, until either all results that Yahoo! has for the
391 query at hand have been fetched, or the Yahoo! Search server access limits
392 have been reached and further access is denied. So, be sure to rate-limit
393 the accesses, or explicitly break out of the loop at some appropriate
394 point.
395  
396 =cut
397  
398 sub NextResult
399 0 ▶{
Subroutine NextResult: not called CC=7 SCAR=40.3
Pod documented
400 0 my $Response = shift; #self;
401 0 ▶ if (@_ % 2 != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ % 2 != 0) 0 0 0
402 0 return Yahoo::Search::_carp_on_error("wrong number of args to NextResult");
403 }
404 0 my $AutoContinue = $Response->{_Request}->{AutoContinue};
405  
406 ## isolate args we allow...
407 0 my %Args = @_;
408 0 ▶ if (exists $Args{AutoContinue}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Args{"AutoContinue"}) 0 0 0
409 0 $AutoContinue = delete $Args{AutoContinue};
410 }
411  
412 ## anything left over is unexpected
413 0 ▶ if (%Args) {
Branch: 1 branch
BranchTrueFalseTotal
if (%Args) 0 0 0
414 0 my $list = join ', ', keys %Args;
415 0 return Yahoo::Search::_carp_on_error("unexpected args to NextResult: $list");
416 }
417  
418 ##
419 ## Setup is done -- now the real thing.
420 ## If the next slot is filled, return the result sitting there.
421 ##
422 0 ▶ if ($Response->{_NextIterator} < @{$Response->{Result}})
Branch: 1 branch
BranchTrueFalseTotal
if ($Response->{"_NextIterator"} < @{$Response->{"Result"};}) 0 0 0
423 {
424 0 return $Response->{Result}->[$Response->{_NextIterator}++];
425 }
426  
427 ##
428 ## If we're auto-continuing and there is another response...
429 ##
430 0 ▶ if ($AutoContinue and my $next = $Response->NextResponse)
Branch: 1 branch
BranchTrueFalseTotal
if ($AutoContinue and my $next = $Response->NextResponse) 0 0 0
Condition0%0/3
$AutoContinue and my $next = $Response->NextResponse
!ll&&!rl&&r
000
MC/DC0%0/2
$AutoContinue and my $next = $Response->NextResponse
$AutoContinue my $next = $Response->NextResponse
Truth table0%0/3
A and B
A= $AutoContinueB= my $next = $Response->NextResponse
ABresult
0X0
100
111
431 {
432 ## replace this $Response with the new one, _in_place_
433 ## (this destroys the old one)
434 0 %$Response = %$next;
435  
436 ## and return the first result from it...
437 0 return $Response->NextResult;
438 }
439  
440 ##
441 ## Oh well, reset the iterator and return nothing.
442 ##
443 0 $Response->{_NextIterator} = 0;
444 0 return ();
445 }
446  
447  
448 ###########################################################################
449  
450 =item $Response->Reset()
451  
452 Rests the iterator so that the next C<NextResult> returns the first of the
453 C<Response> object's C<Result> objects.
454  
455 =cut '
456  
457 sub Reset
458 0 ▶{
Subroutine Reset: not called CC=1 SCAR=6.9
Pod documented
459 0 my $Response = shift; #self;
460 0 $Response->{_NextIterator} = 0;
461 }
462  
463  
464  
465 ###########################################################################
466  
467 =item $Response->Request()
468  
469 Returns the C<Request> object from which this C<Response> object was
470 derived.
471  
472 =cut
473  
474 sub Request
475 0 ▶{
Subroutine Request: not called CC=1 SCAR=6.9
Pod documented
476 0 my $Response = shift; #self;
477 0 return $Response->{_Request};
478 }
479  
480  
481 ###########################################################################
482  
483 =item $Response->NextRequest()
484  
485 Returns a C<Request> object which will fetch the subsequent set of results
486 (e.g. if the current C<Response> object represents the first 10 query
487 results, C<NextRequest()> returns a C<Request> object that represents a
488 query for the I<next> 10 results.)
489  
490 Returns nothing if there were no results in the current C<Response> object
491 (thereby eliminating the possibility of there being a I<next> result set).
492 On error, sets C<$@> and returns nothing.
493  
494 =cut
495  
496 sub NextRequest
497 0 ▶{
Subroutine NextRequest: not called CC=4 SCAR=30.0
Pod documented
498 0 my $Response = shift; #self
499  
500 0 ▶ if (not $Response->Count) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Response->Count) 0 0 0
501 ## No results last time, so can't expect any next time
502 0 return ();
503 }
504  
505 0 ▶ if ($Response->FirstOrdinal + $Response->Count >= $Response->CountAvail)
Branch: 1 branch
BranchTrueFalseTotal
if ($Response->FirstOrdinal + $Response->Count >= $Response->CountAvail) 0 0 0
506 {
507 ## we have them all, so no reason to get more
508 0 return ();
509 }
510  
511 0 ▶ if ($Response->{_NoFurtherRequests}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Response->{"_NoFurtherRequests"}) 0 0 0
512 ## no reason to get more
513 0 return ();
514 }
515  
516  
517 ## Make a copy of the request
518 0 my %Request = %{$Response->{_Request}};
519 ## want that copy to be deep
520 0 $Request{Params} = { %{$Request{Params}} };
521  
522 ## update the 'start' param
523 0 $Request{Params}->{start} += $Response->Count;
524  
525 0 return Yahoo::Search::Request->new(%Request);
526 }
527  
528  
529  
530 ###########################################################################
531  
532 =item $Response->NextResponse()
533  
534 Like C<NextRequest>, but goes ahead and calls the C<Request> object's
535 C<Fetch> method to return the C<Result> object for the next set of results.
536  
537 =cut '
538  
539 sub NextResponse
540 0 ▶{
Subroutine NextResponse: not called CC=2 SCAR=17.9
Pod documented
541 0 my $Response = shift; #self
542  
543 0 ▶ if (my $Request = $Response->NextRequest) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $Request = $Response->NextRequest) { } 0 0 0
544 0 return $Request->Fetch();
545 } else {
546 # $@ must already be set
547 0 return ();
548 }
549 }
550  
551 ###########################################################################
552  
553 =item $Response->Uri()
554  
555 Returns the C<URI::http> object that was fetched to create this response.
556 It is the same as:
557  
558 $Response->Request->Uri()
559  
560 =cut
561  
562 sub Uri
563 0 ▶{
Subroutine Uri: not called CC=1 SCAR=6.9
Pod documented
564 0 my $Response = shift; #self;
565 0 return $Response->{_Request}->Uri;
566 }
567  
568  
569  
570  
571 ###########################################################################
572  
573 =item $Response->Url()
574  
575 Returns the url that was fetched to create this response.
576 It is the same as:
577  
578 $Response->Request->Url()
579  
580 =cut
581  
582 sub Url
583 0 ▶{
Subroutine Url: not called CC=1 SCAR=6.9
Pod documented
584 0 my $Response = shift; #self;
585 0 return $Response->Request->Url;
586 }
587  
588  
589  
590 ###########################################################################
591  
592 =item $Response->RawXml()
593  
594 Returns a string holding the raw xml returned from the Yahoo! Search
595 servers.
596  
597 =cut
598  
599 sub RawXml
600 0 ▶{
Subroutine RawXml: not called CC=1 SCAR=6.9
Pod documented
601 0 my $Response = shift; #self;
602 0 return $Response->{_XML};
603 }
604  
605 ##############################################################################
606  
607 =item $Response->MapUrl()
608  
609 Valid only for a I<Local> search, returns a url to a map showing all
610 results. (This is the same as each C<Result> object's C<AllMapUrl> method.)
611  
612 =cut
613  
614 sub MapUrl
615 0 ▶{
Subroutine MapUrl: not called CC=1 SCAR=6.9
Pod documented
616 0 my $Response = shift; #self;
617 0 return $Response->{ResultSetMapUrl};
618 }
619  
620  
621  
622  
623 ##############################################################################
624  
625 =item $Response->RelatedRequest
626  
627 =item $Response->RelatedResponse
628  
629 Perform a I<Related> request for search terms related to the query phrase
630 of the current request, returning the new C<Request> or C<Response> object,
631 respectively.
632  
633 Both return nothing if the current request is already for a I<Related>
634 search.
635  
636 For example:
637  
638 print "Did you mean ", join(" or ", $Response->RelatedResponse->Terms()), "?";
639  
640 =cut
641  
642 sub RelatedRequest
643 0 ▶{
Subroutine RelatedRequest: not called CC=1 SCAR=6.9
Pod documented
644 0 my $Response = shift;
645 0 return $Response->Request->RelatedRequest;
646 }
647  
648 sub RelatedResponse
649 0 ▶{
Subroutine RelatedResponse: not called CC=1 SCAR=6.9
Pod documented
650 0 my $Response = shift;
651 0 return $Response->Request->RelatedResponse;
652 }
653  
654  
655 ##############################################################################
656  
657 =item $Response->SpellRequest
658  
659 =item $Response->SpellResponse
660  
661 Perform a I<Spell> request for a search term that may reflect proper
662 spelling of the query phrase of the current request, returning the new
663 C<Request> or C<Response> object, respectively.
664  
665 Both return nothing if the current request is already for a I<Spell>
666 search.
667  
668 =cut
669  
670  
671 sub SpellRequest
672 0 ▶{
Subroutine SpellRequest: not called CC=1 SCAR=6.9
Pod documented
673 0 my $Response = shift;
674 0 return $Response->Request->SpellRequest;
675 }
676  
677 sub SpellResponse
678 0 ▶{
Subroutine SpellResponse: not called CC=1 SCAR=6.9
Pod documented
679 0 my $Response = shift;
680 0 return $Response->Request->SpellResponse;
681 }
682  
683  
684  
685 ##############################################################################
686  
687  
688  
689 =pod
690  
691 =back
692  
693 =head1 Author
694  
695 Jeffrey Friedl (jfriedl@yahoo.com)
696  
697 =cut
698  
699  
700 2 1;
« Search.pm ↑ Summary