File Coverage

blib/lib/Google/Ranker.pm
Criterion Covered Total %
statement 15 36 41.6
branch 0 18 0.0
condition 0 6 0.0
subroutine 5 7 71.4
pod 1 1 100.0
total 21 68 30.8


line stmt bran cond sub pod time code
1             package Google::Ranker;
2              
3 2     2   41337 use warnings;
  2         5  
  2         62  
4 2     2   11 use strict;
  2         5  
  2         92  
5              
6             =head1 NAME
7              
8             Google::Ranker - Find the ranking of a site/result against a search
9              
10             =head1 VERSION
11              
12             Version 0.02
13              
14             =cut
15              
16             our $VERSION = '0.02';
17              
18              
19             =head1 SYNOPSIS
20              
21             use Google::Ranker;
22            
23             my $rank = Google::Ranker->rank("search.cpan.org", { q => "perl network", key => ..., referer => ... });
24             # (Make sure to get a valid key and referer from http://code.google.com/apis/ajaxsearch/signup.html first)
25              
26             # Or pass in a prepared search:
27            
28             my $search = Google::Search->Local(...);
29             my $rank = Google::Ranker->rank("example.com", $search);
30              
31             # You can also rank against different criteria:
32            
33             my $search = Google::Search->Video(q => "tay zonday", ...);
34             my $rank = Google::Ranker->rank(sub { $_[0]->titleNoFormatting =~ m/Chocolate Rain/i }, $search);
35              
36             =head1 DESCRIPTION
37              
38             Google::Ranker will determine the rank of a result matching some criteria within a search. The search
39             can be done on any of Google's search services, including web, local, news, blogs, images, videos, and books.
40              
41             This connects to Google's AJAX Search API (L) and is built upon
42             L
43              
44             =cut
45              
46 2     2   1747 use Google::Search;
  2         374020  
  2         99  
47 2     2   22 use Scalar::Util qw/blessed/;
  2         4  
  2         186  
48 2     2   11 use Carp;
  2         4  
  2         734  
49              
50             =head1 METHODS
51              
52             =head2 Google::Rank->rank( , )
53              
54             Returns the numeric rank for in
55              
56             Returns undef if is not found (very possible, since the Google AJAX API only returns a limited
57             number of results at this time)
58              
59             The first result from Google is ranked at 1
60              
61             The parameter can either be a string converted into a regular expression,
62             a regular expression to be matched against the uri of each result, or a code reference
63             passed each result (in turn) as the first parameter.
64              
65             The parameter should be a L or a hash reference to be passed to
66             Google::Search->new(...)
67              
68             At minimum you must pass in a C, a C, and a C (the actual query)
69              
70             =cut
71              
72             sub rank {
73 0     0 1   my $class = shift;
74 0           my $matcher = shift;
75 0           my $search = shift;
76              
77 0 0 0       if (defined $search && ref $search eq "") {
78 0           warn "\n# ", __FILE__, ":", __LINE__, "\n", <<_END_;
79             # Running a search without a valid API key/referer
80             # Pass in a key => ... and referer => ... to disable this warning
81             # You can get both at http://code.google.com/apis/ajaxsearch/signup.html
82              
83             _END_
84 0           $search = { q => $search };
85             }
86 0 0         if (ref $search eq "HASH") {
87 0           $search = Google::Search->Web(%$search);
88             }
89              
90 0 0         croak "Don't have a search to rank with" unless $search;
91 0 0 0       croak "Don't understand search \"$search\"" unless blessed $search && $search->isa("Google::Search");
92 0 0         croak "Don't have a matcher to find ranking position with" unless $matcher;
93              
94 0 0         $matcher = qr/$matcher/ if ref $matcher eq "";
95 0 0         if (ref $matcher eq "Regexp") {
96 0           my $re = $matcher;
97             $matcher = sub {
98 0     0     return $_[0]->uri->as_string =~ $re;
99 0           };
100             }
101 0 0         unless (ref $matcher eq "CODE") {
102 0           croak "Don't understand matcher \"$matcher\"";
103             }
104              
105 0           my $result = $search->first_match($matcher);
106              
107 0 0         return undef unless defined $result;
108              
109 0           return $result->number + 1;
110             }
111              
112             =head1 AUTHOR
113              
114             Robert Krimen, C<< >>
115              
116             =head1 SEE ALSO
117              
118             L
119              
120             =head1 BUGS
121              
122             Please report any bugs or feature requests to C, or through
123             the web interface at L. I will be notified, and then you'll
124             automatically be notified of progress on your bug as I make changes.
125              
126              
127              
128              
129             =head1 SUPPORT
130              
131             You can find documentation for this module with the perldoc command.
132              
133             perldoc Google::Ranker
134              
135              
136             You can also look for information at:
137              
138             =over 4
139              
140             =item * RT: CPAN's request tracker
141              
142             L
143              
144             =item * AnnoCPAN: Annotated CPAN documentation
145              
146             L
147              
148             =item * CPAN Ratings
149              
150             L
151              
152             =item * Search CPAN
153              
154             L
155              
156             =back
157              
158              
159             =head1 ACKNOWLEDGEMENTS
160              
161              
162             =head1 COPYRIGHT & LICENSE
163              
164             Copyright 2008 Robert Krimen, all rights reserved.
165              
166             This program is free software; you can redistribute it and/or modify it
167             under the same terms as Perl itself.
168              
169              
170             =cut
171              
172             1; # End of Google::Ranker