Search.pm

filter: Statementstmt 54.1% 143 / 264 Branchbran 38.7% 55 / 142 Conditioncond 17.7% 8 / 45 MC/DCmcdc 0.0% 0 / 25 Subroutinesub 54.5% 18 / 33 Podpod 100% 9 / 9 totaltotal 44.9% 233 / 518 CC 91 SCAR 40.8
CC
91
Cov
46%
Request
190.2
__ANON__
29.4
__ANON__
20.0
CRAP 59.0
« Search/Request.pm ↑ Summary Search/Response.pm »
1 package Yahoo::Search;
2 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 $Yahoo::Search::VERSION = '1.11.3';
4 }
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Yahoo::Search::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 ##
10 ## This is the interface to Yahoo!'s web search API.
11 ## Written by Jeffrey Friedl <jfriedl@yahoo.com>
12 ##
13 ## Master source:
14 ##
15 ## http://search.cpan.org/search?mode=module&n=1&query=Yahoo::Search
16 ##
17  
18 ##
19 ## CLASS OVERVIEW
20 ##
21 ## The main class
22 ## Yahoo::Search
23 ## begets
24 ## Yahoo::Search::Request
25 ## which begets
26 ## Yahoo::Search::Response
27 ## which begets a bunch of
28 ## Yahoo::Search::Result
29 ## which beget urls, summaries, etc.
30 ##
31 ## There are plenty of "convenience functions" which appear to bypass some
32 ## of these steps as far as the user's view is concerned.
33 ##
34  
35 ##
36 ## Configuration details for each search space (Doc, Images, Video, ## etc.)...
37 ##
38 2 my %Config =
39 (
40 ######################################################################
41 # Normal web search
42 #
43 Doc =>
44 {
45 Url => 'http://search.yahooapis.com/WebSearchService/V1/webSearch',
46 ContextUrl => 'http://search.yahooapis.com/WebSearchService/V1/contextSearch',
47  
48 MaxCount => 50,
49  
50 ## The 'Defaults' keys indicate the universe of allowable arguments,
51 ## while the values are the defaults for those arguments.
52 Defaults => {
53 Mode => undef,
54 Context => undef,
55 Count => 10,
56 Start => 0,
57 Type => 'any',
58 AllowAdult => 0,
59 AllowSimilar => 0,
60 Language => undef,
61 Country => undef,
62 License => undef,
63 Region => undef,
64 },
65  
66 QueryOptional => 1,
67  
68 AllowedLicense => {
69 any => 1, #default
70 cc_any => 1,
71 cc_commercial => 1,
72 cc_modifiable => 1,
73 },
74  
75 AllowedMode => {
76 all => 1,
77 any => 1,
78 phrase => 1,
79 },
80  
81 AllowedType => {
82 all => 1, # deprecated
83 any => 1,
84  
85 html => 1,
86 msword => 1,
87 pdf => 1,
88 ppt => 1,
89 rss => 1,
90 txt => 1,
91 xls => 1,
92 },
93 },
94  
95  
96 ######################################################################
97 # Image search
98 #
99 Image =>
100 {
101 Url => 'http://search.yahooapis.com/ImageSearchService/V1/imageSearch',
102 MaxCount => 50,
103  
104 Defaults => {
105 Mode => undef,
106 Count => 10,
107 Start => 0,
108 Type => 'any',
109 AllowAdult => 0,
110 Color => undef,
111 },
112  
113 AllowedMode => {
114 all => 1,
115 any => 1,
116 phrase => 1,
117 },
118  
119 AllowedType => {
120 all => 1, # deprecated
121 any => 1,
122  
123 bmp => 1,
124 gif => 1,
125 jpeg => 1,
126 png => 1,
127 },
128  
129 AllowedColor => {
130 any => 1, # default
131 color => 1,
132 bw => 1,
133 }
134  
135 },
136  
137 ######################################################################
138 # Video file search
139 #
140 Video =>
141 {
142 Url => 'http://search.yahooapis.com/VideoSearchService/V1/videoSearch',
143  
144 MaxCount => 50,
145  
146 Defaults => {
147 Mode => undef,
148 Count => 10,
149 Start => 0,
150 Type => 'any',
151 AllowAdult => 0,
152 },
153  
154 AllowedMode => {
155 all => 1,
156 any => 1,
157 phrase => 1,
158 },
159  
160 AllowedType => {
161 all => 1, # deprecated
162 any => 1,
163  
164 avi => 1,
165 flash => 1,
166 mpeg => 1,
167 msmedia => 1,
168 quicktime => 1,
169 realmedia => 1,
170 },
171 },
172  
173  
174 ######################################################################
175 # "Y! Local" (like Yellow Pages) search
176 #
177 Local =>
178 {
179 Url => 'http://local.yahooapis.com/LocalSearchService/V1/localSearch',
180  
181 MaxCount => 20,
182  
183 Defaults => {
184 Count => 10,
185 Start => 0,
186 Mode => undef,
187 Radius => undef,
188 Street => undef,
189 City => undef,
190 State => undef,
191 PostalCode => undef,
192 Location => undef,
193 Lat => undef,
194 Long => undef,
195 Sort => undef,
196 },
197  
198 AllowedSort => {
199 relevance => 1,
200 distance => 1,
201 rating => 1,
202 title => 1
203 }
204 },
205  
206  
207 ######################################################################
208 # News search
209 #
210 News =>
211 {
212 Url => 'http://search.yahooapis.com/NewsSearchService/V1/newsSearch',
213  
214 MaxCount => 50,
215  
216 Defaults => {
217 Mode => undef,
218 Count => 10,
219 Start => 0,
220 Sort => undef,
221 Language => undef,
222 },
223  
224 AllowedMode => {
225 all => 1,
226 any => 1,
227 phrase => 1,
228 },
229  
230 AllowedSort => {
231 rank => 1,
232 date => 1,
233 },
234 },
235  
236  
237 Terms =>
238 {
239 Url => 'http://search.yahooapis.com/ContentAnalysisService/V1/termExtraction',
240  
241 Defaults => {
242 Context => undef,
243 },
244 Required => {
245 Context => 1,
246 },
247 QueryOptional => 1,
248 },
249  
250 Spell =>
251 {
252 Url => 'http://search.yahooapis.com/WebSearchService/V1/spellingSuggestion',
253 },
254  
255 Related =>
256 {
257 Url => 'http://search.yahooapis.com/WebSearchService/V1/relatedSuggestion',
258 Defaults => {
259 Count => 10,
260 },
261 },
262 );
263  
264 2 our $UseXmlSimple = $ENV{YAHOO_SEARCH_XMLSIMPLE}; ## used in Search/Request.pm
265 2 our $RecentRequestUrl; ## filled in Search/Request.pm
266  
267  
268 ##
269 ## These args are allowed for any Query()
270 ##
271 2 my @ExtraQueryArgs = qw[AutoContinue Debug AppId];
272  
273 ##
274 ## Global defaults -- this list may be modified via import()
275 ## and Default();
276 ##
277 2 my %GlobalDefault =
278 (
279 ##
280 ## Debug is a string with any of: url (show the url as fetched)
281 ## xml (show the resulting xml)
282 ## hash (show the resulting hash)
283 ## stdout (show to stdout instead of stderr)
284 ## e.g. "url hash stdout"
285 Debug => "",
286  
287 ##
288 ## if AutoCarp is true (as it is by default), carp on programming errors
289 ## (but not 404s, etc.)
290 ##
291 AutoCarp => 1,
292  
293 AutoContinue => 0,
294  
295 PreRequestCallback => undef,
296 );
297  
298 ##
299 ## Helper function to set $@ and, if needed, carp.
300 ##
301 sub _carp_on_error($)
302 0 ▶{
Subroutine _carp_on_error: not called CC=2 SCAR=17.9
303 0 $@ = shift;
304 0 ▶ if ($GlobalDefault{AutoCarp}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($GlobalDefault{"AutoCarp"}) 0 0 0
305 0 carp $@;
306 }
307  
308 0 return ();
309 }
310  
311  
312 ##
313 ## The following private subs are used to validate arguments. They are
314 ## generally called with two args: the search space (Doc, Image, etc.), and
315 ## the text to validate.
316 ##
317 ## If called without the "text to validate" arg, they return a description
318 ## of what args are allowed (tailored to the search space, if appropriate).
319 ##
320 ## Otherwise, they return ($valid, $value).
321 ##
322 my $allow_positive_integer = sub
323 5 ▶{
Subroutine __ANON__: called CC=4 SCAR=16.6
324 5 my $space = shift; # unused
325 5 ▶ if (not @_) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 0 5 5
326 0 return "positive integer";
327 }
328 5 my $val = shift;
329  
330 5 ▶ if (not $val =~ m/^\d+$/) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $val =~ /^\d+$/) { } 0 5 5
elsif ($val == 0) { } 0 5 5
331 0 return (0); # invalid: not a number
332 } elsif ($val == 0) {
333 0 return (0); # invalid: not positive
334 } else {
335 5 return (1, $val);
336 }
337 2 };
338  
339 my $allow_nonnegative_integer = sub
340 3 ▶{
Subroutine __ANON__: called CC=3 SCAR=12.3
341 3 my $space = shift; # unused
342 3 ▶ if (not @_) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 0 3 3
343 0 return "non-negative integer";
344 }
345 3 my $val = shift;
346  
347 3 ▶ if (not $val =~ m/^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $val =~ /^\d+$/) { } 0 3 3
348 0 return (0); # invalid: not a number
349 } else {
350 3 return (1, $val);
351 }
352 2 };
353  
354 my $allow_positive_float = sub
355 0 ▶{
Subroutine __ANON__: not called CC=4 SCAR=30.0
356 0 my $space = shift; # unused
357 0 ▶ if (not @_) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 0 0 0
358 0 return "positive number";
359 }
360 0 my $val = shift;
361  
362 0 ▶ if (not $val =~ m/^(?: \d+(?: \.\d* )?$ | \.\d+$ )/x) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $val =~ /^(?: \d+(?: \.\d* )?$ | \.\d+$ )/x) { } 0 0 0
elsif ($val == 0) { } 0 0 0
363 0 return (0); # invalid: not a number
364 } elsif ($val == 0) {
365 0 return (0); # invalid: not positive
366 } else {
367 0 return (1, $val);
368 }
369 2 };
370  
371 my $allow_float = sub
372 0 ▶{
Subroutine __ANON__: not called CC=3 SCAR=24.8
373 0 my $space = shift; # unused
374 0 ▶ if (not @_) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 0 0 0
375 0 return "number";
376 }
377 0 my $val = shift;
378  
379 0 ▶ if (not $val =~ m/^-? (?: \d+(?: \.\d* )?$ | \.\d+$ )/x) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $val =~ /^-? (?: \d+(?: \.\d* )?$ | \.\d+$ )/x) { } 0 0 0
380 0 return (0); # invalid: not a number
381 } else {
382 0 return (1, $val);
383 }
384 2 };
385  
386 my $allow_country_code = sub
387 0 ▶{
Subroutine __ANON__: not called CC=3 SCAR=24.8
388 0 my $space = shift; # unused
389 0 ▶ if (not @_) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 0 0 0
390 0 return "country code";
391 }
392 0 my $val = shift;
393  
394 0 ▶ if (not $val =~ m/^(?: [a-z][a-z]$ | default$ )/x) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $val =~ /^(?: [a-z][a-z]$ | default$ )/x) { } 0 0 0
395 0 return (0); # not a country code and not "default"
396 } else {
397 0 return (1, $val);
398 }
399 2 };
400  
401 my $allow_language_code = sub
402 0 ▶{
Subroutine __ANON__: not called CC=3 SCAR=24.8
403 0 my $space = shift; # unused
404 0 ▶ if (not @_) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 0 0 0
405 0 return "language code";
406 }
407 0 my $val = shift;
408  
409 0 ▶ if (not $val =~ m/^(?: [a-z][a-z][a-z]?$ | default$ )/x) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $val =~ /^(?: [a-z][a-z][a-z]?$ | default$ )/x) { } 0 0 0
410 0 return (0); # not a language code and not "default"
411 } else {
412 0 return (1, $val);
413 }
414 2 };
415  
416  
417 ##
418 ## This has different args than the others: it has two args ($allow_multi
419 ## and $hashref) prepended before $space
420 ##
421 my $allow_from_hash = sub
422 3 ▶{
Subroutine __ANON__: called CC=10 SCAR=33.8
423 3 my $allow_multi = shift;
424 3 my $hashref = shift; #hash in which to check
425 3 my $space = shift; #unused
426  
427 3 ▶ if (not @_) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 0 3 3
428 0 return join '|', sort keys %$hashref;
429 }
430 3 my $val = shift;
431  
432 3 ▶ if (not $hashref) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($hashref) 0 3 3
433 0 return (1, $val); # can't tell, so say it's valid
434 }
435  
436 3 ▶ if (not defined($val) or not length($val))
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $val and length $val) 0 3 3
Condition33%1/3
defined $val and length $val
!ll&&!rl&&r
003
MC/DC0%0/2
defined $val and length $val
defined $val length $val
Truth table33%1/3
A and B
A= defined $valB= length $val
ABresult
0X0
100
111
437 {
438 0 return (0); # not valid
439 }
440  
441 3 ▶ if (not $allow_multi)
Branch: 1 branch
BranchTrueFalseTotal
if (not $allow_multi) { } 3 0 3
442 {
443 3 ▶ if ($hashref->{$val}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hashref->{$val}) { } 3 0 3
444 3 return (1, $val); # is specifically valid
445 } else {
446 0 return (0); # not valid
447 }
448 }
449 else
450 {
451 0 my @items = split /[+,\s]+/, $val;
452 0 ▶ if (not @items) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@items) 0 0 0
453 0 return (0); # not valid
454 }
455  
456 0 for my $item (@items)
457 {
458 0 ▶ if (not $hashref->{$item}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($hashref->{$item}) 0 0 0
459 0 return (0); # not valid
460 }
461 }
462 0 return (1, $val); # valid
463 }
464 2 };
465  
466 my $allow_boolean = sub
467 9 ▶{
Subroutine __ANON__: called CC=3 SCAR=12.0
468 9 my $space = shift; #unused
469 9 ▶ if (not @_) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 0 9 9
470 0 return "true or false";
471 }
472 9 my $val = shift;
473 9 ▶ return (1, $val ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$val ? : 0 9 9
474 2 };
475  
476 my $allow_any = sub
477 3 ▶{
Subroutine __ANON__: called CC=2 SCAR=7.4
478 3 my $space = shift; #unused
479 3 ▶ if (not @_) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 0 3 3
480 0 return "any value";
481 }
482 3 my $val = shift;
483 3 return (1, $val);
484 2 };
485  
486 my $allow_postal_code = sub
487 0 ▶{
Subroutine __ANON__: not called CC=3 SCAR=24.8
488 0 my $space = shift; #unused
489 ## only U.S. Zone Improvement Program codes allowed
490 0 ▶ if (not @_) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 0 0 0
491 0 return "a US ZIP code"
492 }
493  
494 0 my $val = shift;
495 0 ▶ if ($val =~ m/^\d\d\d\d\d(?:-?\d\d\d\d)?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /^\d\d\d\d\d(?:-?\d\d\d\d)?$/) { } 0 0 0
496 0 return (1, $val);
497 } else {
498 0 return (0);
499 }
500 2 };
501  
502 my $allow_coderef = sub
503 0 ▶{
Subroutine __ANON__: not called CC=2 SCAR=17.9
504 0 my $space = shift; #unused
505 0 my $val = shift;
506 0 ▶ if (ref($val) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val eq 'CODE') { } 0 0 0
507 0 return (1, $val);
508 } else {
509 0 return (0);
510 }
511 2 };
512  
513 my $allow_appid = sub
514 5 ▶{
Subroutine __ANON__: called CC=3 SCAR=12.3
515 5 my $space = shift; #unused
516  
517 5 ▶ if (not @_) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 0 5 5
518 0 return "something which matches /^[- A-Za-z0-9_()[\\]*+=,.:\@\\\\]{8,}\$/";
519 }
520  
521 5 my $val = shift;
522 5 ▶ if ($val =~ m/^[- A-Za-z0-9_()\[\]*+=,.:\@\\]{8,}$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /^[- A-Za-z0-9_()\[\]*+=,.:\@\\]{8,}$/) { } 5 0 5
523 5 return (1, $val);
524 } else {
525 0 return (0);
526 }
527 2 };
528  
529 2 our %KnownLanguage =
530 (
531 default => 'any/all languages',
532  
533 ar => 'Arabic',
534 bg => 'Bulgarian',
535 ca => 'Catalan',
536 szh => 'Chinese (simplified)',
537 tzh => 'Chinese (traditional)',
538 hr => 'Croatian',
539 cs => 'Czech',
540 da => 'Danish',
541 nl => 'Dutch',
542 en => 'English',
543 et => 'Estonian',
544 fi => 'Finnish',
545 fr => 'French',
546 de => 'German',
547 el => 'Greek',
548 he => 'Hebrew',
549 hu => 'Hungarian',
550 is => 'Icelandic',
551 it => 'Italian',
552 ja => 'Japanese',
553 ko => 'Korean',
554 lv => 'Latvian',
555 lt => 'Lithuanian',
556 no => 'Norwegian',
557 fa => 'Persian',
558 pl => 'Polish',
559 pt => 'Portuguese',
560 ro => 'Romanian',
561 ru => 'Russian',
562 sk => 'Slovak',
563 sl => 'Slovenian',
564 es => 'Spanish',
565 sv => 'Swedish',
566 th => 'Thai',
567 tr => 'Turkish',
568 );
569  
570 2 our %KnownCountry =
571 (
572 default => "any/all countries",
573  
574 ar => 'Argentina',
575 au => 'Australia',
576 at => 'Austria',
577 be => 'Belgium',
578 br => 'Brazil',
579 ca => 'Canada',
580 cn => 'China',
581 cz => 'Czech Republic',
582 dk => 'Denmark',
583 fi => 'Finland',
584 fr => 'France',
585 de => 'Germany',
586 it => 'Italy',
587 jp => 'Japan',
588 kr => 'Korea',
589 nl => 'Netherlands',
590 no => 'Norway',
591 pl => 'Poland',
592 rf => 'Russian Federation',
593 es => 'Spain',
594 se => 'Sweden',
595 ch => 'Switzerland',
596 tw => 'Taiwan',
597 uk => 'United Kingdom',
598 us => 'United States',
599 );
600  
601  
602 ##
603 ## Mapping from arg name to value validation routine.
604 ##
605 my %ValidateRoutine =
606 (
607 Count => $allow_positive_integer,
608 Start => $allow_nonnegative_integer,
609  
610 Radius => $allow_positive_float,
611  
612 AllowAdult => $allow_boolean,
613 AllowSimilar => $allow_boolean,
614  
615 Context => $allow_any,
616  
617 Street => $allow_any,
618 City => $allow_any,
619 State => $allow_any,
620 Location => $allow_any,
621 Lat => $allow_float,
622 Long => $allow_float,
623  
624 PostalCode => $allow_postal_code,
625 Language => $allow_language_code,
626 Country => $allow_country_code,
627 Region => $allow_country_code,
628  
629 0 ▶ Mode => sub { $allow_from_hash->(0, $Config{$_[0]}->{AllowedMode}, @_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
630 0 ▶ Sort => sub { $allow_from_hash->(0, $Config{$_[0]}->{AllowedSort}, @_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
631 3 ▶ Type => sub { $allow_from_hash->(0, $Config{$_[0]}->{AllowedType}, @_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
632 0 ▶ License => sub { $allow_from_hash->(1, $Config{$_[0]}->{AllowedLicense}, @_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
633 0 ▶ Color => sub { $allow_from_hash->(0, $Config{$_[0]}->{Color}, @_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
634  
635 2 Debug => $allow_any,
636 AutoContinue => $allow_boolean,
637 AutoCarp => $allow_boolean,
638  
639 AppId => $allow_appid,
640  
641 PreRequestCallback => $allow_coderef,
642 );
643  
644 ##
645 ## returns ($newvalue, $error);
646 ##
647 sub _validate($$$;$)
648 31 ▶{
Subroutine _validate: called CC=8 SCAR=24.0
649 31 my $global = shift; # true if for a global setting
650 31 my $space = shift; # Doc, Image, etc.
651 31 my $key = shift; # "Count", "State", etc.
652 31 ▶ my $have_val = @_ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 28 3 31
653 31 my $val = shift;
654  
655 31 ▶ if (not $ValidateRoutine{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ValidateRoutine{$key}) 0 31 31
656 0 return (undef, "unknown argument '$key'");
657 }
658  
659 31 ▶ if (not $global and $key eq 'AutoCarp') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $global and $key eq "AutoCarp") 0 31 31
Condition66%2/3
not $global and $key eq "AutoCarp"
!ll&&!rl&&r
4270
MC/DC0%0/2
not $global and $key eq "AutoCarp"
not $global $key eq "AutoCarp"
Truth table66%2/3
not(A) and B
A= not $globalB= $key eq "AutoCarp"
ABresult
0X0
100
111
660 0 return (undef, "AutoCarp is a global setting which can not be used in this context");
661 }
662  
663 31 ▶ if (not $have_val) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($have_val) 3 28 31
664 3 return (1);
665 }
666  
667 28 my ($valid, $newval) = $ValidateRoutine{$key}->($space, $val);
668  
669 28 ▶ if ($valid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($valid) 28 0 28
670 28 return ($newval, undef);
671 }
672  
673 0 my $expected = $ValidateRoutine{$key}->($space);
674 0 ▶ if ($space) {
Branch: 1 branch
BranchTrueFalseTotal
if ($space) { } 0 0 0
675 0 return (undef, "invalid value \"$val\" for $space\'s \"$key\" argument, expected: $expected");
676 } else {
677 0 return (undef, "invalid value \"$val\" for \"$key\" argument, expected: $expected");
678 }
679 }
680  
681  
682 ##
683 ## 'import' accepts key/value pairs:
684 ##
685 sub import
686 2 ▶{
Subroutine import: called CC=4 SCAR=14.6
687 2 my $class = shift;
688  
689 2 ▶ if (@_ % 2 != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ % 2 != 0) 0 2 2
690 0 Carp::confess("bad number of args to 'use $class'");
691 }
692 2 my %Args = @_;
693  
694 2 ▶ while (my ($key, $val) = each %Args)
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %Args) 4 2 6
695 {
696 4 my ($newval, $error) = _validate(1, undef, $key, $val);
697 4 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) { } 0 4 4
698 0 Carp::confess("$error, in 'use $class'");
699 } else {
700 4 $GlobalDefault{$key} = $newval;
701 }
702 }
703 }
704  
705  
706 ##
707 ## Get (or set) one of the default global values. They can be set this way
708 ## (either as Yahoo::Search->Default or $SearchEngine->Default), or via
709 ## Yahoo::Search->new(), or on the 'use' line.
710 ##
711 ## When used with a $SearchEngine object, the value returned is the value
712 ## in effect, which is the global one if the $SearchEngine does not have
713 ## one itself.
714 ##
715 sub Default
716 3 ▶{
Subroutine Default: called CC=8 SCAR=28.2
Pod documented
717 3 my $class_or_obj = shift; # Yahoo::Search->Default or $SearchEngine->Default
718 3 my $key = shift;
719 3 ▶ my $have_val = @_ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 3 3
720 3 my $val = shift;
721  
722 3 my $global = not ref $class_or_obj;
723  
724 3 my $old;
725 3 ▶ if ($global or not exists $class_or_obj->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($global or not exists $class_or_obj->{$key}) { } 3 0 3
Condition33%1/3
$global or not exists $class_or_obj->{$key}
l!l&&r!l&&!r
030
MC/DC0%0/2
$global or not exists $class_or_obj->{$key}
$global not exists $class_or_obj->{$key}
Truth table33%1/3
A or not(B)
A= $globalB= not exists $class_or_obj->{$key}
ABresult
1X1
011
000
726 3 $old = $GlobalDefault{$key};
727 } else {
728 0 $old = $class_or_obj->{$key};
729 }
730  
731 3 ▶ if ($have_val)
Branch: 1 branch
BranchTrueFalseTotal
if ($have_val) { } 0 3 3
732 {
733 0 my ($newval, $error) = _validate($global, undef, $key, $val);
734 0 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 0 0 0
735 0 return _carp_on_error($error);
736 }
737  
738 0 ▶ if (ref $class_or_obj) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $class_or_obj) { } 0 0 0
739 0 $class_or_obj->{$key} = $newval;
740 } else {
741 0 $GlobalDefault{$key} = $newval;
742 }
743 }
744 else
745 {
746 3 my ($okay, $error) = _validate($global, undef, $key);
747 3 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 0 3 3
748 0 return _carp_on_error($error);
749 }
750 }
751  
752 3 return $old;
753 }
754  
755  
756  
757 ##
758 ## Maps Yahoo::Search->Query arguments to Y! API parameters.
759 ##
760 2 my %ArgToParam =
761 (
762 AllowAdult => 'adult_ok',
763 AllowSimilar => 'similar_ok',
764 AppId => 'appid',
765 City => 'city',
766 Context => 'context',
767 Count => 'results',
768 Country => 'country',
769 Color => 'coloration',
770 Language => 'language',
771 Lat => 'latitude',
772 License => 'license',
773 Location => 'location',
774 Long => 'longitude',
775 Mode => 'type',
776 PostalCode => 'zip',
777 Radius => 'radius',
778 Region => 'region',
779 Sort => 'sort',
780 Start => 'start',
781 State => 'state',
782 Street => 'street',
783 Type => 'format',
784 );
785  
786  
787 ##
788 ## The search-engine constructor.
789 ##
790 ## No args are needed, but any of %ValidateRoutine keys except AutoCarp are
791 ## allowed (they'll be used as the defaults when queries are later
792 ## constructed via this object).
793 ##
794 sub new
795 3 ▶{
Subroutine new: called CC=4 SCAR=19.4
Pod documented
796 3 my $class = shift;
797  
798 3 ▶ if (@_ % 2 != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ % 2 != 0) 0 3 3
799 0 return _carp_on_error("wrong arg count to $class->new");
800 }
801  
802 3 my $SearchEngine = { @_ };
803  
804 3 for my $key (keys %$SearchEngine)
805 {
806 0 my ($newval, $error) = _validate(0, undef, $key, $SearchEngine->{$key});
807 0 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 0 0 0
808 0 return _carp_on_error("$error, in call to $class->new");
809 }
810 0 $SearchEngine->{$key} = $newval;
811 }
812  
813 3 return bless $SearchEngine, $class;
814 }
815  
816 ##
817 ## Request method (can also be called like a constructor).
818 ## Specs to a specific query are provided, and a Request object is returned.
819 ##
820 sub Request
821 3 ▶{
Subroutine Request: called CC=33 SCAR=52.5
Pod documented
822 3 my $SearchEngine = shift; # self
823 3 my $SearchSpace = shift; # "Doc", "Image", "News", etc..
824 3 my $QueryText = shift; # "Briteny", "egregious compensation semel", etc.
825  
826 3 ▶ if (@_ % 2 != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ % 2 != 0) 0 3 3
827 0 return _carp_on_error("wrong arg count");
828 }
829  
830 3 my %Args = @_;
831  
832 3 ▶ if (not defined $SearchSpace or not $Config{$SearchSpace}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $SearchSpace and $Config{$SearchSpace}) 0 3 3
Condition33%1/3
defined $SearchSpace and $Config{$SearchSpace}
!ll&&!rl&&r
003
MC/DC0%0/2
defined $SearchSpace and $Config{$SearchSpace}
defined $SearchSpace $Config{$SearchSpace}
Truth table33%1/3
A and B
A= defined $SearchSpaceB= $Config{$SearchSpace}
ABresult
0X0
100
111
833 0 my $list = join '|', sort keys %Config;
834 0 return _carp_on_error("bad search-space identifier, expecting one of: $list");
835 }
836  
837 ##
838 ## Ensure that required args are there
839 ##
840 3 ▶ if (my $ref = $Config{$SearchSpace}->{Required})
Branch: 1 branch
BranchTrueFalseTotal
if (my $ref = $Config{$SearchSpace}{"Required"}) 0 3 3
841 {
842 0 for my $arg (keys %$ref)
843 {
844 0 ▶ if (not defined($Args{$arg}) or not length($Args{$arg})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $Args{$arg} and length $Args{$arg}) 0 0 0
Condition0%0/3
defined $Args{$arg} and length $Args{$arg}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $Args{$arg} and length $Args{$arg}
defined $Args{$arg} length $Args{$arg}
Truth table0%0/3
A and B
A= defined $Args{$arg}B= length $Args{$arg}
ABresult
0X0
100
111
845 0 return _carp_on_error("argument '$arg' required");
846 }
847 }
848 }
849  
850  
851 ##
852 ## %Param holds the key/vals we'll send in the request to Yahoo!
853 ##
854 3 my %Param;
855  
856 ##
857 ## Special case for some searches: query not required
858 ##
859 3 ▶ if (not defined($QueryText) or length($QueryText) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $QueryText or length $QueryText == 0) { } 0 3 3
Condition33%1/3
not defined $QueryText or length $QueryText == 0
l!l&&r!l&&!r
003
MC/DC0%0/2
not defined $QueryText or length $QueryText == 0
not defined $QueryText length $QueryText == 0
Truth table33%1/3
not(A) or B
A= not defined $QueryTextB= length $QueryText == 0
ABresult
1X1
011
000
860 {
861 0 ▶ if ($Args{Context} and $Config{$SearchSpace}->{QueryOptional}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Args{'Context'} and $Config{$SearchSpace}{'QueryOptional'}) { } 0 0 0
Condition0%0/3
$Args{"Context"} and $Config{$SearchSpace}{"QueryOptional"}
!ll&&!rl&&r
000
MC/DC0%0/2
$Args{"Context"} and $Config{$SearchSpace}{"QueryOptional"}
$Args{"Context"} $Config{$SearchSpace}{"QueryOptional"}
Truth table0%0/3
A and B
A= $Args{"Context"}B= $Config{$SearchSpace}{"QueryOptional"}
ABresult
0X0
100
111
862 ## query text not required
863 } else {
864 0 return _carp_on_error("missing query");
865 }
866 }
867 else
868 {
869 ## normal query
870 3 $Param{query} = $QueryText;
871 }
872  
873  
874 ##
875 ## This can be called as a constructor -- if so, $SearchEngine will be
876 ## the class name, and we'll want to turn into an object.
877 ##
878 3 ▶ if (not ref $SearchEngine) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $SearchEngine) 0 3 3
879 0 $SearchEngine = $SearchEngine->new();
880 }
881  
882 3 my %OtherRequestArgs;
883  
884 ##
885 ## Go through most allowed args, taking the value from this call's arg
886 ## list if provided, from the defaults that were registered with the
887 ## SearchEngine, or failing those, the defaults for this type of query.
888 ##
889 3 for my $key (keys %{ $Config{$SearchSpace}->{Defaults} }, @ExtraQueryArgs)
890 {
891 ##
892 ## Isolate the value we'll use for this request: from our args,
893 ## from the defaults registered with the search-engine, or from
894 ## the search-space defaults.
895 ##
896 42 my $val;
897 42 ▶ if (exists $Args{$key}) {
Branch: 4 branches
BranchTrueFalseTotal
if (exists $Args{$key}) { } 2 40 42
elsif (exists $SearchEngine->{$key}) { } 0 40 40
elsif (exists $GlobalDefault{$key}) { } 10 30 40
elsif (exists $Config{$SearchSpace}{'Defaults'}{$key}) { } 30 0 30
898 2 $val = delete $Args{$key};
899 } elsif (exists $SearchEngine->{$key}) {
900 0 $val = $SearchEngine->{$key};
901 } elsif (exists $GlobalDefault{$key}) {
902 10 $val = $GlobalDefault{$key};
903 } elsif (exists $Config{$SearchSpace}->{Defaults}->{$key}) {
904 30 $val = $Config{$SearchSpace}->{Defaults}->{$key};
905 } else {
906 0 $val = undef;
907 }
908  
909 42 ▶ if (defined $val)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) 24 18 42
910 {
911 24 my ($newval, $error) = _validate(0, $SearchSpace, $key, $val);
912  
913 24 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 0 24 24
914 0 return _carp_on_error($error);
915 }
916  
917 24 ▶ if (my $param = $ArgToParam{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $param = $ArgToParam{$key}) { } 18 6 24
918 18 $Param{$param} = $newval;
919 } else {
920 6 $OtherRequestArgs{$key} = $newval;
921 }
922 }
923 }
924  
925 ##
926 ## Any leftover args are bad
927 ##
928 3 ▶ if (%Args) {
Branch: 1 branch
BranchTrueFalseTotal
if (%Args) 0 3 3
929 0 my $list = join(', ', keys %Args);
930 0 return _carp_on_error("unknown args for '$SearchSpace' query: $list");
931 }
932  
933 ##
934 ## An AppId is required for all calls
935 ##
936 3 ▶ if (not $Param{'appid'})
Branch: 1 branch
BranchTrueFalseTotal
unless ($Param{"appid"}) 0 3 3
937 {
938 0 return _carp_on_error("an AppId is required -- please make one up");
939 }
940  
941 ##
942 ## Do some special per-arg-type processing
943 ##
944  
945 ##
946 ## If we're doing a Doc context search, be sure to use the proper
947 ## action url
948 ##
949 3 my $ActionUrl = $Config{$SearchSpace}->{Url};
950  
951 3 ▶ if ($Param{context} and $Config{$SearchSpace}->{ContextUrl}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Param{"context"} and $Config{$SearchSpace}{"ContextUrl"}) 0 3 3
Condition33%1/3
$Param{"context"} and $Config{$SearchSpace}{"ContextUrl"}
!ll&&!rl&&r
300
MC/DC0%0/2
$Param{"context"} and $Config{$SearchSpace}{"ContextUrl"}
$Param{"context"} $Config{$SearchSpace}{"ContextUrl"}
Truth table33%1/3
A and B
A= $Param{"context"}B= $Config{$SearchSpace}{"ContextUrl"}
ABresult
0X0
100
111
952 0 $ActionUrl = $Config{$SearchSpace}->{ContextUrl};
953 }
954  
955 ##
956 ## Ensure that the Count, if given, is not over max
957 ## No longer enforced here. MaxCount is only advisory now.
958 #if (defined $Param{count} and $Param{count} > $Config{$SearchSpace}->{MaxCount}) {
959 # return _carp_on_error("maximum allowed Count for a $SearchSpace search is $Config{$SearchSpace}->{MaxCount}");
960 #}
961  
962 ##
963 ## If License is given, it an have multiple values (space, comma, or
964 ## plus-separated).
965 ##
966 3 ▶ if ($Param{license}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Param{"license"}) 0 3 3
967 0 $Param{license} = [ split /[+,\s]+/, $Param{license} ];
968 }
969  
970 ##
971 ## In Perl universe, Start is 0-based, but the Y! API's "start" is 1-based.
972 ##
973 3 $Param{start}++;
974  
975 # 'Local' has special required parameters
976 3 ▶ if ($SearchSpace eq 'Local'
Branch: 1 branch
BranchTrueFalseTotal
if ($SearchSpace eq "Local" and not $Param{"location"} || $Param{"zip"} || $Param{"state"} && $Param{"city"} || defined $Param{"latitude"} && defined $Param{"longitude"}) 0 3 3
Condition5%1/18
$SearchSpace eq "Local" and not $Param{"location"} || $Param{"zip"} || $Param{"state"} && $Param{"city"} || defined $Param{"latitude"} && defined $Param{"longitude"}
!ll&&!rl&&r
300
$Param{'location'} || $Param{'zip'} || $Param{'state'} && $Param{'city'} || defined $Param{'latitude'} && defined $Param{'longitude'}
l!l&&r!l&&!r
000
$Param{"location"} or $Param{"zip"} or $Param{"state"} and $Param{"city"}
l!l&&r!l&&!r
000
$Param{"location"} or $Param{"zip"}
l!l&&r!l&&!r
000
$Param{"state"} and $Param{"city"}
!ll&&!rl&&r
000
defined $Param{"latitude"} and defined $Param{"longitude"}
!ll&&!rl&&r
000
MC/DC0%0/7
$SearchSpace eq "Local" and not $Param{"location"} || $Param{"zip"} || $Param{"state"} && $Param{"city"} || defined $Param{"latitude"} && defined $Param{"longitude"}
$SearchSpace eq "Local" $Param{"location"} $Param{"zip"} $Param{"state"} $Param{"city"} defined $Param{"latitude"} defined $Param{"longitude"}
Truth table10%1/10
A and not(B or C or D and E || F and G)
A= $SearchSpace eq "Local"B= $Param{"location"}C= $Param{"zip"}D= $Param{"state"}E= $Param{"city"}F= defined $Param{"latitude"}G= defined $Param{"longitude"}
ABCDEFGresult
0XXXXXX0
11XXXXX0
101XXXX0
10011XX0
1000X110
10010110
1000X0X1
1000X101
100100X1
10010101
977 and not
978 ## the following are the allowed parameter sets... if one is there,
979 ## we're okay
980 ($Param{location}
981 or
982 $Param{'zip'}
983 or
984 ($Param{'state'} and $Param{'city'})
985 or
986 (defined($Param{'latitude'}) and defined($Param{'longitude'}))
987 ))
988 {
989 ##
990 ## The diff between $Param{} references in the if() above, and
991 ## the arg names in the error below, is the %ArgToParam mapping
992 ##
993 0 return _carp_on_error("a 'Local' query must have at least Lat+Long, Location, PostalCode, or City+State");
994 }
995  
996 ##
997 ## Okay, we have everything we need to make a specific request object.
998 ## Make it and return.
999 ##
1000 3 return Yahoo::Search::Request->new(
1001 SearchEngine => $SearchEngine,
1002 Space => $SearchSpace,
1003 Action => $ActionUrl,
1004 Params => \%Param,
1005 %OtherRequestArgs,
1006 );
1007 }
1008  
1009 ##
1010 ## A way to bypass an explicit Request object, jumping from a SearchEngine
1011 ## (or nothing) directly to a Response object.
1012 ##
1013 sub Query
1014 3 ▶{
Subroutine Query: called CC=3 SCAR=11.8
Pod documented
1015 3 my $SearchEngine = shift;
1016 ##
1017 ## Can be called as a constructor -- if so, $SearchEngine will be the
1018 ## class name
1019 ##
1020 3 ▶ if (not ref $SearchEngine) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $SearchEngine) 3 0 3
1021 3 $SearchEngine = $SearchEngine->new();
1022 }
1023  
1024 3 ▶ if (my $Request = $SearchEngine->Request(@_)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $Request = $SearchEngine->Request(@_)) { } 3 0 3
1025 3 return $Request->Fetch();
1026 } else {
1027 # $@ already set
1028 0 return ();
1029 }
1030 }
1031  
1032  
1033 ##
1034 ## A way to bypass explicit Request and Response objects, jumping from a
1035 ## SearchEngine (or nothing) directly to a list of Result objects.
1036 ##
1037 sub Results
1038 3 ▶{
Subroutine Results: called CC=2 SCAR=7.6
Pod documented
1039 3 my $Response = Query(@_);
1040  
1041 3 ▶ if (not $Response) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Response) 3 0 3
1042 # $@ already set
1043 3 return ();
1044 }
1045 0 return $Response->Results;
1046 }
1047  
1048 ##
1049 ## A way to bypass explicit Request and Response objects, jumping from a
1050 ## SearchEngine (or nothing) directly to a list of links.
1051 ##
1052 sub Links
1053 0 ▶{
Subroutine Links: not called CC=1 SCAR=6.9
Pod documented
1054 0 return map { $_->Link } Results(@_);
1055 }
1056  
1057  
1058 ##
1059 ## A way to bypass explicit Request and Response objects, jumping from a
1060 ## SearchEngine (or nothing) directly to a bunch of html results.
1061 ##
1062 sub HtmlResults
1063 0 ▶{
Subroutine HtmlResults: not called CC=1 SCAR=6.9
Pod documented
1064 0 return map { $_->as_html } Results(@_);
1065 }
1066  
1067 ##
1068 ## A way to bypass explicit Request and Response objects, jumping from a
1069 ## SearchEngine (or nothing) directly to a list of terms
1070 ## (For Spell, Related, and Terms searches)
1071 ##
1072 sub Terms
1073 0 ▶{
Subroutine Terms: not called CC=1 SCAR=6.9
Pod documented
1074 0 return map { $_->Term } Results(@_);
1075 }
1076  
1077  
1078 sub MaxCount
1079 0 ▶{
Subroutine MaxCount: not called CC=4 SCAR=30.0
Pod documented
1080 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
1081 ##
1082 ## We'll use only the last arg -- it can be called as either
1083 ## Yahoo::Search::MaxCount($SearchSpace) or
1084 ## Yahoo:Search->MaxCount($SearchSpace) and we don't care which.
1085 ## In either case, the final arg is the search space.
1086 ##
1087 0 my $SearchSpace = $_[-1];
1088 0 ▶ if ($Config{$SearchSpace} and $Config{$SearchSpace}->{MaxCount}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Config{$SearchSpace} and $Config{$SearchSpace}{"MaxCount"}) 0 0 0
Condition0%0/3
$Config{$SearchSpace} and $Config{$SearchSpace}{"MaxCount"}
!ll&&!rl&&r
000
MC/DC0%0/2
$Config{$SearchSpace} and $Config{$SearchSpace}{"MaxCount"}
$Config{$SearchSpace} $Config{$SearchSpace}{"MaxCount"}
Truth table0%0/3
A and B
A= $Config{$SearchSpace}B= $Config{$SearchSpace}{"MaxCount"}
ABresult
0X0
100
111
1089 0 return $Config{$SearchSpace}->{MaxCount};
1090 }
1091 }
1092 0 return (); # bad/missing arg
1093 }
1094  
1095  
1096  
1097 2 1;
1098 __END__
« Search/Request.pm ↑ Summary Search/Response.pm »