blib/lib/WWW/SearchResult.pm

filter: Statementstmt 95.0% 77 / 81 Branchbran 87.5% 7 / 8 Conditioncond 50.0% 2 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 92.8% 39 / 42 Podpod 100% 36 / 36 totaltotal 93.0% 161 / 173 CC 7 SCAR 4.0
CC
7
Cov
92%
_elem_array
4.0
as_HTML
3.1
url
2.0
CRAP 1.5
« blib/lib/WWW/Search/Null/Count.pm ↑ Summary blib/lib/WWW/Search/Null/Empty.pm »
1 # SearchResult.pm
2 # by John Heidemann
3 # Copyright (C) 1996 by USC/ISI
4 # $Id: SearchResult.pm,v 2.78 2008-07-21 01:20:30 Martin Exp $
5 #
6 # Copyright (c) 1996 University of Southern California.
7 # All rights reserved.
8 #
9 # Redistribution and use in source and binary forms are permitted
10 # provided that the above copyright notice and this paragraph are
11 # duplicated in all such forms and that any documentation, advertising
12 # materials, and other materials related to such distribution and use
13 # acknowledge that the software was developed by the University of
14 # Southern California, Information Sciences Institute. The name of the
15 # University may not be used to endorse or promote products derived from
16 # this software without specific prior written permission.
17 #
18 # THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
19 # WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
20 # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21  
22  
23 =head1 NAME
24  
25 WWW::SearchResult - class for results returned from WWW::Search
26  
27 =head1 SYNOPSIS
28  
29 require WWW::Search;
30 require WWW::SearchResult;
31 $search = new WWW::Search;
32 $search->native_query(WWW::Search::escape_query($query));
33 # Get first result:
34 $result = $search->next_result();
35  
36 =head1 DESCRIPTION
37  
38 A framework for returning the results of C<WWW::Search>.
39  
40 =head1 SEE ALSO
41  
42 L<WWW::Search>
43  
44 =head1 REQUIRED RESULTS
45  
46 The particular fields returned in a result are backend- (search
47 engine-) dependent. However, all search engines are required to
48 return a url and title. (This list may grow in the future.)
49  
50 =head1 METHODS AND FUNCTIONS
51  
52 =cut
53  
54 #####################################################################
55  
56 package WWW::SearchResult;
57  
58 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61 3 ▶use CGI;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 3 ▶use base 'LWP::MemberMixin';
Subroutine BEGIN: called CC=1 SCAR=0.0
63 our
64 3 $VERSION = do{ my @r = (q$Revision: 2.78 $ =~ /\d+/g); sprintf "%d."."%03d" x $#r, @r};
65  
66 =head2 new
67  
68 To create a new WWW::SearchResult, call
69  
70 $result = new WWW::SearchResult();
71  
72 =cut
73  
74 sub new
75 {
76 54 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
77 54 my $self = bless { }, $class;
78 54 $self->{urls} = ();
79 54 return $self;
80 } # new
81  
82 =head2 url
83  
84 Returns the primary URL. Note that there may be a list of urls, see
85 also methods C<urls> and C<add_url>. Nothing special is guaranteed
86 about the primary URL other than that it is the first one returned by
87 the back end.
88  
89 Every result is required to have at least one URL.
90  
91 =cut
92  
93 sub url
94 {
95 56 ▶ my $self = shift;
Subroutine url: called CC=2 SCAR=6.9
Pod documented
96 56 ▶ if (@_ < 1)
Branch: 1 branch
BranchTrueFalseTotal
if (@_ < 1) 3 53 56
97 {
98 # No arguments, just return the current value:
99 3 return ${$self->{urls}}[0];
100 } # if no args
101 53 unshift @{$self->{urls}}, $_[0];
102 53 return $self->{urls}->[0];
103 } # url
104  
105 sub _elem_array
106 {
107 85 ▶ my $self = shift;
Subroutine _elem_array: called CC=4 SCAR=13.9
108 85 my $elem = shift;
109 85 ▶ if (@_ < 1)
Branch: 1 branch
BranchTrueFalseTotal
if (@_ < 1) 4 81 85
110 {
111 # No arguments
112 4 ▶ return wantarray ? @{$self->{$elem}} : $self->{$elem};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 4 4
113 } # if
114 81 ▶ if (ref($_[0]))
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) { } 27 54 81
115 {
116 # Trying to assign an arrayref:
117 27 $self->{$elem} = $_[0];
118 }
119 else
120 {
121 # Trying to set to a scalar (or list of scalars); make sure it's
122 # an array even if they give one element:
123 54 $self->{$elem} = undef;
124 54 push @{$self->{$elem}}, @_;
125 }
126 # Always return array refrence
127 81 return $self->{$elem};
128 } # _elem_array
129  
130 sub _add_elem_array
131 {
132 2959 ▶ my $self = shift;
Subroutine _add_elem_array: called CC=1 SCAR=0.0
133 2959 my $elem = shift;
134 # No matter how many they're adding:
135 2959 push(@{$self->{$elem}}, @_);
136 } # _add_elem_array
137  
138  
139 =head2 add_url
140  
141 Add a URL to the list.
142  
143 =cut
144  
145 987 ▶sub add_url { return shift->_add_elem_array('urls', @_); }
Subroutine add_url: called CC=1 SCAR=0.0
Pod documented
146  
147 =head2 urls
148  
149 Return a reference to the list of urls.
150 There is also a primary URL (C<url>).
151  
152 =cut
153  
154 29 ▶sub urls { return shift->_elem_array('urls', @_); }
Subroutine urls: called CC=1 SCAR=0.0
Pod documented
155  
156 =head2 add_related_url
157  
158 Add a URL to the related_url list.
159  
160 =cut
161  
162 986 ▶sub add_related_url { return shift->_add_elem_array('related_urls', @_); }
Subroutine add_related_url: called CC=1 SCAR=0.0
Pod documented
163  
164 =head2 related_urls
165  
166 Return a reference to the list of related urls.
167  
168 =cut
169  
170 28 ▶sub related_urls { return shift->_elem_array('related_urls', @_); }
Subroutine related_urls: called CC=1 SCAR=0.0
Pod documented
171  
172 =head2 add_related_title
173  
174 Add a title to the list or related titles.
175  
176 =cut
177  
178 986 ▶sub add_related_title { return shift->_add_elem_array('related_titles', @_); }
Subroutine add_related_title: called CC=1 SCAR=0.0
Pod documented
179  
180 =head2 related_titles
181  
182 Return a reference to the list of related titles.
183  
184 =cut
185  
186 28 ▶sub related_titles { return shift->_elem_array('related_titles', @_); }
Subroutine related_titles: called CC=1 SCAR=0.0
Pod documented
187  
188  
189 =head2 title, description, score, change_date, index_date, size, raw
190  
191 Set or get attributes of the result.
192  
193 None of these attributes is guaranteed to be provided by
194 a given backend. If an attribute is not provided
195 its method will return C<undef>.
196  
197 Typical contents of these attributes:
198  
199 =over 8
200  
201 =item title
202  
203 The title of the hit result (typically that provided by the 'TITLE'
204 HTML tag).
205  
206 =cut
207  
208 56 ▶sub title { return shift->_elem('title', @_); }
Subroutine title: called CC=1 SCAR=0.0
Pod documented
209  
210 =item description
211  
212 A brief description of the result, as provided (or not) by the search engine.
213 Often the first few sentences of the document.
214  
215 =cut
216  
217 56 ▶sub description { return shift->_elem('description', @_); }
Subroutine description: called CC=1 SCAR=0.0
Pod documented
218  
219 =item source
220  
221 Source is either the base url for this result (as listed on the search
222 engine's results page) or another copy of the full url path of the
223 result. It might also indicate the source site name or address whence
224 the result came, for example, 'CNN' or 'http://www.cnn.com' if the
225 search result page said "found at CNN.com".
226  
227 This value is backend-specific; in fact very few backends set this
228 value.
229  
230 =cut
231  
232 54 ▶sub source { return shift->_elem('source', @_); }
Subroutine source: called CC=1 SCAR=0.0
Pod documented
233  
234 =item add_sources
235  
236 Same meaning as source above, for adding sources in case there are
237 potentially multiple sources.
238  
239 =cut
240  
241 0 ▶sub add_sources { return shift->_add_elem_array('sources', @_); }
Subroutine add_sources: not called CC=1 SCAR=6.9
Pod documented
242  
243 =item sources
244  
245 Returns a reference to the list of sources.
246  
247 =cut
248  
249 0 ▶sub sources { return shift->_elem_array('sources', @_); }
Subroutine sources: not called CC=1 SCAR=6.9
Pod documented
250  
251 =item score
252  
253 A backend specific, numeric score of the search result.
254 The exact range of scores is search-engine specific.
255 Usually larger scores are better, but this is no longer required.
256 See normalized_score for a backend independent score.
257  
258 =cut
259  
260 54 ▶sub score { return shift->_elem('score', @_); }
Subroutine score: called CC=1 SCAR=0.0
Pod documented
261  
262 =item normalized_score
263  
264 This is intended to be a backend-independent score of the search
265 result. The range of this score is between 0 and 1000. Higher values
266 indicate better quality results.
267  
268 This is not really implemented since no one has created an
269 backend-independent ranking algorithm.
270  
271 =cut
272  
273 54 ▶sub normalized_score { return shift->_elem('normalized_score', @_); }
Subroutine normalized_score: called CC=1 SCAR=0.0
Pod documented
274  
275 =item change_date
276  
277 When the result was last changed. Typically this is the modification
278 time of the destination web page.
279  
280 =cut
281  
282 54 ▶sub change_date { return shift->_elem('change_date', @_); }
Subroutine change_date: called CC=1 SCAR=0.0
Pod documented
283  
284 =item index_date
285  
286 When the search engine indexed the result.
287  
288 =cut
289  
290 54 ▶sub index_date { return shift->_elem('index_date', @_); }
Subroutine index_date: called CC=1 SCAR=0.0
Pod documented
291  
292 =item size
293  
294 The approximate size of the result, in bytes. This is only an
295 approximation because search backends often report the size as
296 "18.4K"; the best we can do with that number is return it as the value
297 of 18.4 * 1024.
298  
299 =cut
300  
301 54 ▶sub size { return shift->_elem('size', @_); }
Subroutine size: called CC=1 SCAR=0.0
Pod documented
302  
303 =item raw
304  
305 The raw HTML for the entire result. Raw should be exactly the raw
306 HTML for one entry. It should not include list or table setup
307 commands (like ul or table tags), but it may include list item or
308 table data commands (like li, tr, or td). Whether raw contains a list
309 entry, table row, br-separated lines, or plain text is search-engine
310 dependent. In fact, many backends do not even return it at all.
311  
312 =cut
313  
314 54 ▶sub raw { return shift->_elem('raw', @_); }
Subroutine raw: called CC=1 SCAR=0.0
Pod documented
315  
316 =item as_HTML
317  
318 Convert the search result to a human-readable form,
319 decorated with HTML for pretty-printing.
320  
321 =cut
322  
323 sub as_HTML
324 {
325 1 ▶ my $self = shift;
Subroutine as_HTML: called CC=3 SCAR=11.2
Pod documented
326 1 my $cgi = new CGI;
327 1 ▶ my $s = $cgi->a({
Condition50%1/2
$self->title || 'title unknown'
l!l
10
MC/DC0%0/1
$self->title || 'title unknown'
$self->title
Truth table50%1/2
A
A= $self->title
Aresult
11
00
328 href => $self->url,
329 },
330 $self->title || 'title unknown',
331 );
332 1 $s .= $cgi->br;
333 1 ▶ $s .= $self->description || 'no description available';
Condition50%1/2
$self->description || 'no description available'
l!l
10
MC/DC0%0/1
$self->description || 'no description available'
$self->description
Truth table50%1/2
A
A= $self->description
Aresult
11
00
334 1 return $s;
335 } # as_HTML
336  
337 =back
338  
339 =head2 Others
340  
341 More attributes of the result. Backend-specific.
342 Refer to the documentation of each backend for details.
343  
344 =over
345  
346 =item bid_amount
347  
348 =cut
349  
350 1 ▶sub bid_amount { return shift->_elem('bid', @_); }
Subroutine bid_amount: called CC=1 SCAR=0.0
Pod documented
351  
352 =item bid_count
353  
354 =cut
355  
356 1 ▶sub bid_count { return shift->_elem('bids', @_); }
Subroutine bid_count: called CC=1 SCAR=0.0
Pod documented
357  
358 =item bidder
359  
360 =cut
361  
362 1 ▶sub bidder { return shift->_elem('bidder', @_); }
Subroutine bidder: called CC=1 SCAR=0.0
Pod documented
363  
364 =item category
365  
366 =cut
367  
368 1 ▶sub category { return shift->_elem('category', @_); }
Subroutine category: called CC=1 SCAR=0.0
Pod documented
369  
370 =item company
371  
372 =cut
373  
374 54 ▶sub company { return shift->_elem('company', @_); }
Subroutine company: called CC=1 SCAR=0.0
Pod documented
375  
376 =item end_date
377  
378 =cut
379  
380 1 ▶sub end_date { return shift->_elem('end_date', @_); }
Subroutine end_date: called CC=1 SCAR=0.0
Pod documented
381  
382 =item image_url
383  
384 =cut
385  
386 1 ▶sub image_url { return shift->_elem('image_url', @_); }
Subroutine image_url: called CC=1 SCAR=0.0
Pod documented
387  
388 =item item_number
389  
390 =cut
391  
392 1 ▶sub item_number { return shift->_elem('item_number', @_); }
Subroutine item_number: called CC=1 SCAR=0.0
Pod documented
393  
394 =item location
395  
396 =cut
397  
398 54 ▶sub location { return shift->_elem('location', @_); }
Subroutine location: called CC=1 SCAR=0.0
Pod documented
399  
400 =item question_count
401  
402 =cut
403  
404 1 ▶sub question_count { return shift->_elem('question_count', @_); }
Subroutine question_count: called CC=1 SCAR=0.0
Pod documented
405  
406 =item seller
407  
408 =cut
409  
410 1 ▶sub seller { return shift->_elem('seller', @_); }
Subroutine seller: called CC=1 SCAR=0.0
Pod documented
411  
412 =item shipping
413  
414 =cut
415  
416 1 ▶sub shipping { return shift->_elem('shipping', @_); }
Subroutine shipping: called CC=1 SCAR=0.0
Pod documented
417  
418 =item sold
419  
420 =cut
421  
422 0 ▶sub sold { return shift->_elem('sold', @_); }
Subroutine sold: not called CC=1 SCAR=6.9
Pod documented
423  
424 =item start_date
425  
426 =cut
427  
428 1 ▶sub start_date { return shift->_elem('start_date', @_); }
Subroutine start_date: called CC=1 SCAR=0.0
Pod documented
429  
430 =item thumb_url
431  
432 =cut
433  
434 1 ▶sub thumb_url { return shift->_elem('thumb_url', @_); }
Subroutine thumb_url: called CC=1 SCAR=0.0
Pod documented
435  
436 =item watcher_count
437  
438 =cut
439  
440 1 ▶sub watcher_count { return shift->_elem('seller', @_); }
Subroutine watcher_count: called CC=1 SCAR=0.0
Pod documented
441  
442 =back
443  
444 =head1 AUTHOR
445  
446 WWW::SearchResult was written by John Heidemann.
447 WWW::SearchResult is maintained by Martin Thurn.
448  
449 =cut
450  
451 3 1;
452  
453 __END__
« blib/lib/WWW/Search/Null/Count.pm ↑ Summary blib/lib/WWW/Search/Null/Empty.pm »