File Coverage

blib/lib/WebService/Yahoo/BOSS.pm
Criterion Covered Total %
statement 27 54 50.0
branch 0 6 0.0
condition 0 6 0.0
subroutine 9 15 60.0
pod 2 3 66.6
total 38 84 45.2


line stmt bran cond sub pod time code
1             package WebService::Yahoo::BOSS;
2              
3             =head1 NAME
4              
5             WebService::Yahoo::BOSS - Interface to the Yahoo BOSS Search API
6              
7             =head1 SYNOPSIS
8              
9             use WebService::Yahoo::BOSS;
10              
11             $Boss = WebService::Yahoo::BOSS->new( ckey => $ckey, csecret => $csecret );
12              
13             $response = $Boss->Web( q => 'microbrew award winner 2010', ... );
14              
15             $response = $Boss->PlaceFinder( q => 'Fleet Street, London', ... );
16              
17              
18             =head1 DESCRIPTION
19              
20             Provides an interface to the Yahoo BOSS (Build Your Own Search) web service API.
21              
22             Mad props to Yahoo for putting out a premium search api which encourages
23             innovative use.
24              
25             This is a work in progress, so patches welcome!
26              
27             =head2 Interaction
28              
29             Each service has a corresponding method call. The call takes the same
30             parameters as described in the Yahoo BOSS documentation.
31              
32             Each method returns a L object that has the
33             following methods:
34              
35             $response->totalresults; # total number of available results
36             $response->count; # number of results in this set
37             $response->start; # typically same as start argument in request
38             $response->results; # reference to array of result objects
39              
40             The result objects accessed via the C methods are instances of
41             a C class that corresponds to the method
42             called.
43              
44             =head1 METHODS
45              
46             =cut
47              
48 3     3   32503 use Moo;
  3         66949  
  3         16  
49              
50 3     3   7765 use Any::URI::Escape;
  3         9700  
  3         205  
51 3     3   3014 use LWP::UserAgent;
  3         180183  
  3         135  
52 3     3   40 use URI;
  3         7  
  3         123  
53 3     3   3852 use Net::OAuth;
  3         2538  
  3         93  
54 3     3   3714 use Data::Dumper;
  3         21508  
  3         270  
55 3     3   2932 use Data::UUID;
  3         2520  
  3         234  
56 3     3   22 use Carp qw(croak);
  3         6  
  3         120  
57              
58 3     3   2050 use WebService::Yahoo::BOSS::Response;
  3         13  
  3         1854  
59              
60              
61             our $VERSION = '1.02';
62              
63             my $Ug = Data::UUID->new;
64              
65             $Net::OAuth::PROTOCOL_VERSION = Net::OAuth::PROTOCOL_VERSION_1_0A;
66              
67              
68             =head2 new
69              
70             $boss = WebService::Yahoo::BOSS->new(
71              
72             # required
73             ckey => $ckey,
74             csecret => $csecret,
75              
76             # optional
77             url => 'http://yboss.yahooapis.com',
78             ua => LWP::UserAgent->new(...),
79             );
80              
81             =cut
82              
83             has 'ckey' => ( is => 'ro', required => 1 );
84             has 'csecret' => ( is => 'ro', required => 1 );
85              
86             has 'url' => (
87             is => 'ro',
88             default => "http://yboss.yahooapis.com",
89             );
90              
91             has 'ua' => (
92             is => 'ro',
93             default => sub {
94             LWP::UserAgent->new(
95             agent => __PACKAGE__ . '_' . $VERSION,
96             keep_alive => 1, # cache connection
97             );
98             }
99             );
100              
101             # last HTTP::Response e.g. to enable introspection of error details
102             has 'http_response' => (
103             is => 'rw'
104             );
105              
106              
107             sub _create_boss_request {
108 0     0     my ($self, $api_path, $args) = @_;
109              
110             # Create request
111 0           my $request = Net::OAuth->request("request token")->new(
112             consumer_key => $self->ckey,
113             consumer_secret => $self->csecret,
114             request_url => $self->url . $api_path,
115             request_method => 'GET',
116             signature_method => 'HMAC-SHA1',
117             timestamp => time,
118             nonce => $Ug->to_string( $Ug->create ),
119             extra_params => $args,
120             callback => '',
121             );
122              
123 0           $request->sign;
124              
125 0           return $request;
126             }
127              
128              
129             sub _perform_boss_request {
130 0     0     my ($self, $request) = @_;
131              
132 0           my $res = $self->ua->get( $request->to_url );
133 0           $self->http_response($res);
134 0 0         unless ( $res->is_success ) {
135 0           die sprintf "%s requesting %s: %s",
136             $res->status_line, $request->to_url, Dumper($res);
137             }
138 0           return $res->decoded_content;
139             }
140              
141              
142             sub _parse_boss_response {
143 0     0     my ($self, $response_content, $result_class) = @_;
144 0           return WebService::Yahoo::BOSS::Response->parse( $response_content, $result_class );
145             }
146              
147              
148             sub ask_boss {
149 0     0 0   my ($self, $api_path, $args, $result_class) = @_;
150              
151 0           my $request = $self->_create_boss_request($api_path, $args);
152 0           my $response_content = $self->_perform_boss_request($request);
153 0           my $response = $self->_parse_boss_response($response_content, $result_class);
154              
155 0           return $response;
156             }
157              
158             =head2 Web
159              
160             $response = $Boss->Web( q => 'microbrew award winner 2010',
161             start => 0,
162             exclude => 'pilsner', );
163              
164             For more information about the arguments and result attributes see
165             L
166              
167             The results are L objects.
168              
169             =cut
170              
171             sub Web {
172 0     0 1   my ( $self, %args ) = @_;
173              
174 0 0         croak "q parameter not defined"
175             unless defined $args{q};
176              
177 0   0       $args{count} ||= 10;
178 0   0       $args{filter} ||= '-porn';
179 0   0       $args{format} ||= 'json';
180 0 0         croak 'only json format supported'
181             unless $args{format} eq 'json';
182              
183 0           return $self->ask_boss('/ysearch/web', \%args, 'WebService::Yahoo::BOSS::Response::Web');
184             }
185              
186              
187             =head2 PlaceFinder
188              
189             $response = $boss->PlaceFinder(
190             q => '701 First Ave., Sunnyvale, CA 94089',
191             );
192              
193             For more information about the arguments and result attributes see
194             L
195              
196             The results are L objects.
197              
198             =cut
199              
200             sub PlaceFinder {
201 0     0 1   my ( $self, %args ) = @_;
202              
203 0           $args{flags} .= "J"; # JSON
204              
205 0           return $self->ask_boss('/geo/placefinder', \%args, 'WebService::Yahoo::BOSS::Response::PlaceFinder');
206             }
207              
208              
209             1;
210              
211             =head1 SEE ALSO
212              
213             L
214              
215             L
216              
217             =head1 AUTHOR
218              
219             "Fred Moyer", Efred@slwifi.comE
220              
221             The PlaceFinder service, and general refactoring and optimization, by Tim Bunce.
222              
223             =head1 COPYRIGHT AND LICENSE
224              
225             Copyright (C) 2011 by Silver Lining Networks
226              
227             This library is free software; you can redistribute it and/or modify
228             it under the same terms as Perl itself, either Perl version 5.10.1 or,
229             at your option, any later version of Perl 5 you may have available.
230              
231              
232             =cut