Yelp.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.7
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary Yelp/Business.pm »
1 package WebService::Yelp;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 our $VERSION = '0.03';
7  
8 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 0 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=2.6
10 use Scalar::Util qw/looks_like_number/;
11  
12 use WebService::Yelp::Result;
13 use WebService::Yelp::Neighborhood;
14 use WebService::Yelp::Business;
15 use WebService::Yelp::Review;
16 use WebService::Yelp::Category;
17  
18 use WebService::Yelp::Message;
19  
20 use base qw/Class::Accessor/;
21 __PACKAGE__->mk_accessors(qw/ywsid base_url http_proxy
22 http_timeout http_agent/);
23  
24 use Data::Dumper;
25  
26 =head1 NAME
27  
28 WebService::Yelp - Yelp.com API Client Implementation
29  
30 =head1 SYNOPSIS
31  
32 use strict;
33 use WebService::Yelp;
34  
35 my $yelp = WebService::Yelp->new({ywsid => 'XXXXXXXXXXXX'});
36  
37 my $biz_res = $yelp->search_review_hood({
38 term => 'cream puffs',
39 location => 'San Francisco',
40 };
41 for my $biz (@{$biz_res->businesses()}) {
42  
43 my @reviews = $biz->reviews();
44 my @hoods = $biz->neighborhoods();
45  
46 }
47  
48 =head1 DESCRIPTION
49  
50 This module implements a programmatic interface to yelp.com's public REST API.
51  
52 =head1 REQUIREMENTS
53  
54 Before you can use this module, you'll need to obtain your very own
55 "Yelp Web Service ID" (ywsid) from
56 http://www.yelp.com/developers. While you are there, I'd also urge you to read
57 Yelp's Terms of Use and documentation.
58  
59 Much more documentation is available on Yelp's site:
60  
61 http://www.yelp.com/developers/documentation
62  
63 Most of the functions here map directly to API commands, so it would
64 be a good idea to familiarize yourself with their documentation first
65 and then take a look at the methods and parameters here.
66  
67 =cut
68  
69  
70 my $base_url = 'http://api.yelp.com';
71  
72 =head1 METHODS
73  
74 =head2 new
75
76 my $yelp = WebService::Yelp->new($hash_ref);
77  
78 =head3 Required Parameters
79  
80 =over 4
81  
82 * ywsid - your Yelp Web Service ID.
83  
84 =back
85  
86 =head3 Optional Parameters
87  
88 =over 4
89  
90 * http_proxy - specify a proxy server to use
91  
92 * http_timeout - specify a timeout (Default is 10 seconds)
93  
94 =back
95  
96 =head3 Return Value
97  
98 =over 4
99  
100 * A new WebService::Yelp object, or undef if there were problems.
101  
102 =back
103  
104 =cut
105  
106 sub new {
107 my $self = shift->SUPER::new(@_);
108  
109 unless($self->ywsid()) {
110 warn "ywsid is a required init parameter";
111 return undef;
112 }
113  
114 # set up the user agent
115 my $ua = LWP::UserAgent->new();
116 $ua->agent($self->http_agent() || __PACKAGE__ . '/' . $VERSION);
117  
118 $ua->proxy('http', $self->http_proxy()) if $self->http_proxy();
119 $ua->timeout($self->http_timeout()) if $self->http_timeout();
120  
121 $self->{_ua} = $ua;
122  
123 # set up our uri
124 my $uri = URI->new($self->base_url() || $base_url);
125 $self->{_uri} = $uri;
126  
127 # set up the json parser
128 my $json = JSON->new();
129 $self->{_json} = $json;
130 return $self;
131 }
132  
133  
134 my %cmap = (
135 # review search
136 'search.review.bb' => {
137 req => {
138 tl_lat => 'double',
139 tl_long => 'double',
140 br_lat => 'double',
141 br_long => 'double',
142 },
143 opt => {
144 term => 'string',
145 num_biz_requested => 'int',
146 output => 'string',
147 category => 'string',
148 },
149 path => 'business_review_search',
150 returns => 'businesses',
151 },
152 'search.review.gpr' => {
153 req => {
154 lat => 'double',
155 long => 'double',
156 },
157 opt => {
158 term => 'string',
159 num_biz_requested => 'int',
160 radius => 'double',
161 output => 'string',
162 category => 'string',
163 },
164 path => 'business_review_search',
165 returns => 'businesses',
166 },
167 'search.review.hood' => {
168 req => {
169 location => 'string',
170 },
171 opt => {
172 term => 'string',
173 num_biz_requested => 'int',
174 radius => 'double',
175 output => 'string',
176 },
177 path => 'business_review_search',
178 returns => 'businesses',
179 },
180 # phone search
181 'search.phone' => {
182 req => {
183 phone => 'int',
184 },
185 opt => {
186 output => 'string',
187 },
188 path => 'phone_search',
189 returns => 'businesses',
190 },
191 # neighborhood search
192 'search.neighborhood.geocode' => {
193 req => {
194 lat => 'double',
195 long => 'double',
196 },
197 opt => {
198 output => 'string',
199 },
200 path => 'neighborhood_search',
201 returns => 'neighborhoods',
202 },
203 'search.neighborhood.location' => {
204 req => {
205 location => 'string',
206 },
207 opt => {
208 output => 'string',
209 },
210 path => 'neighborhood_search',
211 returns => 'neighborhoods',
212 },
213 );
214  
215 =head2 Review Search Functions
216  
217 This set of functions searches for and returns review and business
218 information. The following two parameters are common across these
219 functions. Note that output determines the return values for all
220 search functions as well.
221  
222 =over 4
223  
224 * output - Result output type.
225  
226 =back
227  
228 This controls what is returned by the API. The default is
229 WebService::Yelp::* objects. Other options are 'php', 'json', and
230 'pickle'. If one of these three options is specified, the raw data
231 will be returned by the search function as a scalar value. Otherwise,
232 a B<WebService::Yelp::Result> object will be returned. If there was a
233 transport level error, search functions simply return undef and print
234 the requests status line to STDERR.
235  
236 Your first step should be to check the B<WebService::Yelp::Message>
237 object available by calling the B<WebService::Yelp::Result>->message()
238 method and checking the value of code(). See:
239  
240 http://www.yelp.com/developers/documentation/search_api#rCode
241  
242 for more information on the return codes, messages, and error descriptions.
243  
244 =over 4
245  
246 * category - Narrow results to specific categories.
247  
248 =back
249  
250 By selecting only specific categories, search results can be confined
251 to businesses that match those categories. Multiple categories can be
252 speficied by combining them with a plus sign, i.e. bars+poolhalls
253  
254 =head2 search_review_bb - Searching by Map Bounding Box
255  
256 Limit the search to an area within a specific geographical box
257 specified by four geographic points.
258  
259 =head3 Required Parameters
260  
261 =over 4
262  
263 * tl_lat - Top Left latitude of bounding box
264  
265 * tl_long - Top Left longitude of bounding box
266  
267 * br_lat - Bottom right latitude of bounding box
268  
269 * br_long - Bottom right longitude of bounding box
270  
271 =back
272  
273 =head3 Optional Parameters
274  
275 =over 4
276  
277 * term - Business name or search term.
278  
279 * num_biz_requested - Number of businesses to return (1-20, Default 10).
280  
281 =back
282  
283 =cut
284  
285 sub search_review_bb {
286 return shift->call('search.review.bb', @_);
287 }
288  
289 =head2 search_review_gpr - Search by Geo-Point and Radius
290  
291 Given a point and an outward radius, search for businesses within the area.
292  
293 =head3 Required Parameters
294  
295 =over 4
296  
297 * lat - The latitude of the point.
298  
299 * long - The longitude of the point.
300  
301 =back
302  
303 =head3 Optional Parameters
304  
305 =over 4
306  
307 * radius - The outward radius from the two points above, Max is 25.
308  
309 * term - Business name or search term.
310  
311 * num_biz_requested - Number of businesses to return (1-20, Default 10).
312  
313 =back
314  
315 =cut
316  
317 sub search_review_gpr {
318 return shift->call('search.review.gpr', @_);
319 }
320  
321 =head2 search_review_hood - Search by Neighborhood, Address or City
322  
323 Given a general or specific location, search for matching businesses.
324  
325 =head3 Required Parameters
326  
327 =over 4
328  
329 * location - An address, neighborhood, city, state, or zip code.
330  
331 =back
332  
333 =head3 Optional Parameters
334  
335 =over 4
336  
337 * radius - The outward radius from the two points above, Max is 25.
338  
339 * term - Business name or search term.
340  
341 * num_biz_requested - Number of businesses to return (1-20, Default 10).
342  
343 =back
344  
345 =cut
346  
347 sub search_review_hood {
348 return shift->call('search.review.hood', @_);
349 }
350  
351 =head2 search_phone - Search by Phone Number
352  
353 Find a specific business by phone number.
354  
355 =head3 Required Parameter
356  
357 =over 4
358  
359 * phone - An all digit phone number (like 1-234-567-8901)
360  
361 =back
362  
363 =cut
364  
365 sub search_phone {
366 return shift->call('search.phone', @_);
367 }
368  
369 =head2 Neighborhood Search Functions
370  
371 The following functions return neighborhood data. The output parameter
372 as described above is valid for these as well.
373  
374 =head2 search_neighborhood_geocode - Search for Neighborhoods by Geo-Ppoint
375  
376 Given a latitude and longitude, return neighborhood information for
377 the location.
378  
379 =head3 Required Parameters
380  
381 =over 4
382  
383 * lat - The latitude.
384  
385 * long - The longitude.
386  
387 =back
388  
389 =cut
390  
391 sub search_neighborhood_geocode {
392 return shift->call('search.neighborhood.geocode', @_);
393 }
394  
395 =head2 search_neighborhood_location - Search by Address, City, State or Zip
396  
397 Given a general or specific location, search for matching neighborhoods.
398  
399 =head3 Required Parameters
400  
401 =over 4
402  
403 * location - An address, neighborhood, city, state, or zip code.
404  
405 =back
406  
407 =cut
408  
409 sub search_neighborhood_location {
410 return shift->call('search.neighborhood.location', @_);
411 }
412  
413 =head2 Other Functions
414  
415 =head2 call - The actual search function caller
416  
417 Call is the actual search implementation. The other functions call it by specifying the search function first, so if you'd prefer you can use it directly. Search functions names are separated by periods (in the same way the methods above use underscores) i.e.
418  
419 $yelp->search_review_gpr({ ....
420  
421 is the same as
422  
423 $yelp->call('search.review.gpr', { ....
424  
425 =cut
426  
427 sub call {
428 my ($self, $func, $params, $opts) = @_;
429  
430 return undef unless _validate($func, $params);
431  
432 $params->{ywsid} = $self->ywsid();
433 $self->{_uri}->path($cmap{$func}->{path});
434 $self->{_uri}->query_form($params);
435  
436 my $req = HTTP::Request->new(GET => $self->{_uri});
437 my $res = $self->{_ua}->request($req);
438  
439 unless($res->is_success()) {
440 warn $res->status_line();
441 return undef;
442 }
443  
444 # if the caller specifies a particular output type, return
445 # the raw output here, otherwise give them big fat objects
446  
447 if(defined($params->{output}) &&
448 ($params->{output} eq 'json' ||
449 $params->{output} eq 'pickle' ||
450 $params->{output} eq 'php')) {
451  
452 return $res->content();
453 }
454  
455 my $content = $self->{_json}->decode($res->content());
456 my $msg = WebService::Yelp::Message->new($content->{message});
457 my ($result, @results);
458  
459 PREPARE: {
460 if($cmap{$func}->{returns} eq 'businesses') {
461  
462 for my $b (@{$content->{businesses}} ) {
463  
464 my @reviews;
465 while(my $r = shift @{$b->{reviews}}) {
466 push @reviews, WebService::Yelp::Review->new($r);
467 }
468 $b->{reviews} = \@reviews;
469  
470 my @neighborhoods;
471 while(my $n = shift @{$b->{neighborhoods}}) {
472 push @neighborhoods, WebService::Yelp::Neighborhood->new($n);
473 }
474 $b->{neighborhoods} = \@neighborhoods;
475  
476 my @categories;
477 while(my $c = shift @{$b->{categories}}) {
478 push @categories, WebService::Yelp::Category->new($c);
479 }
480 $b->{categories} = \@categories;
481  
482 my $biz = WebService::Yelp::Business->new($b);
483  
484 push @results, $biz;
485 }
486 $result = WebService::Yelp::Result->new({businesses => \@results,
487 message => $msg});
488 last PREPARE;
489 }
490
491 if($cmap{$func}->{returns} eq 'neighborhoods') {
492 for my $n (@{$content->{neighborhoods}}) {
493 my $hood = WebService::Yelp::Neighborhood->new($n);
494 push @results, $hood;
495 }
496 $result = WebService::Yelp::Result->new({neighborhoods => \@results,
497 message => $msg});
498
499 last PREPARE;
500 }
501 }
502 return $result;
503 }
504  
505 # this could have probably been done with something from CPAN .. oh well, it
506 # was a fun wheel to reinvent
507 sub _validate {
508 my ($func, $params) = @_;
509  
510 # verify that we know about this function.
511 unless($cmap{$func}) {
512 warn "Unrecognized function '$func'";
513 return 0;
514 }
515  
516 # verify that we have the required params
517 for my $req (keys %{$cmap{$func}->{req}}) {
518 unless($params->{$req}) {
519 warn "$func requires the '$req' parameter";
520 return 0;
521 }
522 return 0 unless _check_type($cmap{$func}->{req}->{$req},
523 $params->{$req});
524 }
525  
526 # and do some type checking on the optional ones too
527 for my $opt (keys %{$cmap{$func}->{opt}}) {
528
529 if(defined($params->{$opt})) {
530 return 0 unless(_check_type($cmap{$func}->{opt}->{$opt},
531 $params->{$opt}));
532 }
533 }
534 return 1;
535 }
536
537 sub _check_type {
538 my ($type, $value) = @_;
539  
540 return 0 unless $value;
541 # (very) basic type validation
542 if($type eq 'int' ||
543 $type eq 'double') {
544  
545 unless(looks_like_number($value)) {
546  
547 return 0;
548 }
549 }
550 elsif($type eq 'string') {
551  
552 }
553 return 1;
554 }
555  
556  
557 =head1 SEE ALSO
558  
559 L<LWP::UserAgent>
560  
561 This module's source and other documentation is hosted at
562 http://code.google.com/p/perl-www-yelp-api/
563  
564 =head1 AUTHOR
565  
566 josh rotenberg, E<lt>joshrotenberg@gmail.comE<gt>
567  
568 =head1 COPYRIGHT AND LICENSE
569  
570 Copyright (C) 2008 by josh rotenberg
571  
572 This library is free software; you can redistribute it and/or modify
573 it under the same terms as Perl itself, either Perl version 5.8.8 or,
574 at your option, any later version of Perl 5 you may have available.
575  
576 Note that this software is not maintained, endorsed, sponsored,
577 supported, or otherwise managed by Yelp. All inquiries related to this
578 software should be directed to the author.
579  
580 =cut
581  
582 1;
↑ Summary Yelp/Business.pm »