yelp 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,36 @@
1
+ require 'yelp/record'
2
+
3
+ class Yelp
4
+ # Describes the available response formats when querying the Yelp web
5
+ # service for results.
6
+ #
7
+ class ResponseFormat < Record
8
+ # the name of the response format
9
+ attr_reader :name
10
+
11
+ # whether this response format returns serialized data (and, so, data that
12
+ # we probably don't want to print to a log file and suchlike)
13
+ attr_reader :serialized
14
+
15
+ # the format specifier to retrieve results as straight JSON content
16
+ JSON = Yelp::ResponseFormat.new(:name => 'json')
17
+
18
+ # the format specifier to retrieve results as Ruby objects converted
19
+ # from the returned JSON content via the +json+ rubygem.
20
+ JSON_TO_RUBY = Yelp::ResponseFormat.new(:name => 'json_to_ruby')
21
+
22
+ # the format specifier to retrieve results as a serialized Python
23
+ # response. We're not quite sure why you'd want to use this if
24
+ # you're calling it from Ruby code, but we like to see completeness
25
+ # in an API.
26
+ PICKLE = Yelp::ResponseFormat.new(:name => 'pickle', :serialized => true)
27
+
28
+ # the format specifier to retrieve results as a serialized PHP
29
+ # response. We're not quite sure why you'd want to use this if
30
+ # you're calling it from Ruby code, but we like to see completeness
31
+ # in an API.
32
+ PHP = Yelp::ResponseFormat.new(:name => 'php', :serialized => true)
33
+
34
+ alias :serialized? :serialized
35
+ end
36
+ end
@@ -0,0 +1,26 @@
1
+ require 'yelp/request'
2
+
3
+ class Yelp
4
+ module Review
5
+ module Request
6
+ class Base < Yelp::Request
7
+ # specifies the number of businesses to return in the result set.
8
+ # default is 10. minimum value is 1 and maximum value is 20.
9
+ attr_reader :business_count
10
+
11
+ # string representing the name of business or search term being
12
+ # requested.
13
+ attr_reader :term
14
+
15
+ def base_url
16
+ 'http://api.yelp.com/business_review_search'
17
+ end
18
+
19
+ def to_yelp_params
20
+ super.merge(:term => term,
21
+ :num_biz_requested => business_count)
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,37 @@
1
+ require 'yelp/review/request/base'
2
+
3
+ class Yelp
4
+ module Review
5
+ module Request
6
+ # Describes a request to search for business reviews for businesses
7
+ # within a geo-point-specific bounding box and radius around
8
+ # that box.
9
+ #
10
+ class BoundingBox < Yelp::Review::Request::Base
11
+ # bottom right latitude of bounding box
12
+ attr_reader :bottom_right_latitude
13
+
14
+ # bottom right longitude of bounding box
15
+ attr_reader :bottom_right_longitude
16
+
17
+ # radius to use while searching around specified geo-point.
18
+ # default value is 1, maximum value is 25.
19
+ attr_reader :radius
20
+
21
+ # top left latitude of bounding box
22
+ attr_reader :top_left_latitude
23
+
24
+ # top left longitude of bounding box
25
+ attr_reader :top_left_longitude
26
+
27
+ def to_yelp_params
28
+ super.merge(:tl_lat => top_left_latitude,
29
+ :tl_long => top_left_longitude,
30
+ :br_lat => bottom_right_latitude,
31
+ :br_long => bottom_right_longitude,
32
+ :radius => radius)
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,28 @@
1
+ require 'yelp/review/request/base'
2
+
3
+ class Yelp
4
+ module Review
5
+ module Request
6
+ # Describes a request to search for business reviews for businesses near
7
+ # a specific geo-point and radius around that point.
8
+ #
9
+ class GeoPoint < Yelp::Review::Request::Base
10
+ # latitude of geo-point to search near
11
+ attr_reader :latitude
12
+
13
+ # longitude of geo-point to search near
14
+ attr_reader :longitude
15
+
16
+ # radius to use while searching around specified geo-point.
17
+ # default value is 1, maximum value is 25.
18
+ attr_reader :radius
19
+
20
+ def to_yelp_params
21
+ super.merge(:lat => latitude,
22
+ :long => longitude,
23
+ :radius => radius)
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,63 @@
1
+ require 'yelp/review/request/base'
2
+
3
+ class Yelp
4
+ module Review
5
+ module Request
6
+ # Describes a request to search for business reviews near a specific
7
+ # address/location. You do not need to specify all of the address
8
+ # attributes -- some subset of the core +address+, +city+,
9
+ # +neighborhood+, +state+ and +zipcode+ will suffice.
10
+ #
11
+ class Location < Yelp::Review::Request::Base
12
+ # the street address of the location sought
13
+ attr_reader :address
14
+
15
+ # the city of the location sought
16
+ attr_reader :city
17
+
18
+ # the neighborhood of the location sought
19
+ attr_reader :neighborhood
20
+
21
+ # radius to use while searching around specified geo-point.
22
+ # default value is 1, maximum value is 25.
23
+ attr_reader :radius
24
+
25
+ # the state of the location sought
26
+ attr_reader :state
27
+
28
+ # the zipcode of the location sought
29
+ attr_reader :zipcode
30
+
31
+ def initialize (params)
32
+ # we explicitly initialize the location fields since we reference
33
+ # them later when building a full location string and we want
34
+ # to know they were initialized properly (and avoid warnings)
35
+ super({
36
+ :address => nil,
37
+ :city => nil,
38
+ :neighborhood => nil,
39
+ :state => nil,
40
+ :zipcode => nil
41
+ }.merge(params))
42
+ end
43
+
44
+ def to_yelp_params
45
+ super.merge(:location => build_location_string,
46
+ :radius => radius)
47
+ end
48
+
49
+ protected
50
+
51
+ # Returns the Yelp-compatible concatenated string with the various
52
+ # possible bits of an address-oriented location.
53
+ #
54
+ def build_location_string
55
+ # per the Yelp documentation, the location string is to be built
56
+ # as some combination of "address, neighborhood, city, state, or
57
+ # zip".
58
+ [ @address, @neighborhood, @city, @state, @zipcode ].compact.join(" ")
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,11 @@
1
+ require 'test/unit'
2
+ require 'yelp'
3
+
4
+ class TestClient < Test::Unit::TestCase
5
+ def test_validation
6
+ # make sure we can do basic client instantiation
7
+ assert_nothing_raised do
8
+ @client = Yelp::Client.new
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,41 @@
1
+ require 'pp'
2
+ require 'rubygems'
3
+ require 'test/unit'
4
+ require 'yelp'
5
+ require File.dirname(__FILE__) + '/yelp_helper'
6
+
7
+ class TestNeighborhoodSearch < Test::Unit::TestCase
8
+ include YelpHelper
9
+
10
+ def setup
11
+ @client = Yelp::Client.new
12
+ @yws_id = ENV['YWSID']
13
+ # @client.debug = true
14
+ end
15
+
16
+ GORDO_LAT = 37.782093
17
+ GORDO_LON = -122.483230
18
+ GORDO_NEIGHBORHOOD = 'Outer Richmond'
19
+
20
+ def test_geo_point_search
21
+ request = Yelp::Neighborhood::Request::GeoPoint.new(:latitude => GORDO_LAT,
22
+ :longitude => GORDO_LON,
23
+ :yws_id => @yws_id)
24
+ response = @client.search(request)
25
+ validate_json_to_ruby_response(response)
26
+ assert_equal response['neighborhoods'].first, GORDO_NEIGHBORHOOD
27
+ end
28
+
29
+ GORDO_ADDRESS = '2252 Clement Street'
30
+
31
+ def test_location_search
32
+ request = Yelp::Neighborhood::Request::Location.new(:address => GORDO_ADDRESS,
33
+ :city => 'San Francisco',
34
+ :state => 'CA',
35
+ :zipcode => 94121,
36
+ :yws_id => @yws_id)
37
+ response = @client.search(request)
38
+ validate_json_to_ruby_response(response)
39
+ assert_equal response['neighborhoods'].first, GORDO_NEIGHBORHOOD
40
+ end
41
+ end
@@ -0,0 +1,23 @@
1
+ require 'pp'
2
+ require 'rubygems'
3
+ require 'test/unit'
4
+ require 'yelp'
5
+ require File.dirname(__FILE__) + '/yelp_helper'
6
+
7
+ class TestPhoneSearch < Test::Unit::TestCase
8
+ include YelpHelper
9
+
10
+ def setup
11
+ @client = Yelp::Client.new
12
+ @yws_id = ENV['YWSID']
13
+ # @client.debug = true
14
+ end
15
+
16
+ GORDO_PHONE_NUMBER = '4155666011'
17
+
18
+ def test_phone_search
19
+ request = Yelp::Phone::Request::Number.new(:phone_number => GORDO_PHONE_NUMBER, :yws_id => @yws_id)
20
+ response = @client.search(request)
21
+ validate_json_to_ruby_response(response)
22
+ end
23
+ end
@@ -0,0 +1,102 @@
1
+ require 'pp'
2
+ require 'rubygems'
3
+ require 'test/unit'
4
+ require 'yelp'
5
+ require File.dirname(__FILE__) + '/yelp_helper'
6
+
7
+ class TestReviewSearch < Test::Unit::TestCase
8
+ include YelpHelper
9
+
10
+ def setup
11
+ @client = Yelp::Client.new
12
+ @yws_id = ENV['YWSID']
13
+ # @client.debug = true
14
+ end
15
+
16
+ def test_bounding_box
17
+ request = Yelp::Review::Request::BoundingBox.new(
18
+ :bottom_right_latitude => 37.788022,
19
+ :bottom_right_longitude => -122.399797,
20
+ :top_left_latitude => 37.9,
21
+ :top_left_longitude => -122.5,
22
+ # :radius => 1,
23
+ # :business_count => 3,
24
+ :term => 'yelp',
25
+ :yws_id => @yws_id)
26
+ response = @client.search(request)
27
+ validate_json_to_ruby_response(response)
28
+ end
29
+
30
+ def test_geo_point
31
+ request = Yelp::Review::Request::GeoPoint.new(
32
+ :latitude => 37.78022,
33
+ :longitude => -122.399797,
34
+ :radius => 2,
35
+ # :business_count => 5,
36
+ :term => 'yelp',
37
+ :yws_id => @yws_id)
38
+ response = @client.search(request)
39
+ validate_json_to_ruby_response(response)
40
+ end
41
+
42
+ def test_location
43
+ request = Yelp::Review::Request::Location.new(
44
+ :address => '650 Mission St',
45
+ :city => 'San Francisco',
46
+ :state => 'CA',
47
+ :radius => 2,
48
+ # :business_count => 5,
49
+ :term => 'cream puffs',
50
+ :yws_id => @yws_id)
51
+ response = @client.search(request)
52
+ validate_json_to_ruby_response(response)
53
+ end
54
+
55
+ def test_json_response_format
56
+ request = basic_request(:response_format => Yelp::ResponseFormat::JSON)
57
+ response = @client.search(request)
58
+ validate_json_response(response)
59
+ end
60
+
61
+ def test_json_to_ruby_response_format
62
+ request = basic_request(:response_format => Yelp::ResponseFormat::JSON_TO_RUBY)
63
+ response = @client.search(request)
64
+ validate_json_to_ruby_response(response)
65
+ end
66
+
67
+ def test_pickle_response_format
68
+ request = basic_request(:response_format => Yelp::ResponseFormat::PICKLE)
69
+ @client.search(request)
70
+ # TODO: validation
71
+ end
72
+
73
+ def test_php_response_format
74
+ request = basic_request(:response_format => Yelp::ResponseFormat::PHP)
75
+ response = @client.search(request)
76
+ # TODO: validation
77
+ end
78
+
79
+ def test_compressed_response
80
+ request = basic_request(:compress_response => true)
81
+ response = @client.search(request)
82
+ validate_json_to_ruby_response(response)
83
+ end
84
+
85
+ def test_uncompressed_response
86
+ request = basic_request(:compress_response => false)
87
+ response = @client.search(request)
88
+ validate_json_to_ruby_response(response)
89
+ end
90
+
91
+ protected
92
+
93
+ def basic_request (params = nil)
94
+ default_params = {
95
+ :city => 'San Francisco',
96
+ :state => 'CA',
97
+ :term => 'gordo',
98
+ :yws_id => @yws_id
99
+ }
100
+ Yelp::Review::Request::Location.new(default_params.merge(params))
101
+ end
102
+ end
@@ -0,0 +1,35 @@
1
+ module YelpHelper
2
+ def validate_json_response (response)
3
+ assert_not_nil response
4
+ assert_instance_of String, response
5
+ assert_match(/^\{"message": \{"text": "OK", "code": 0, "version": 1\.0\}, "(businesses|neighborhoods)": .*?\}$/, response)
6
+ end
7
+
8
+ def validate_json_to_ruby_response (response)
9
+ assert_not_nil response
10
+ assert_instance_of Hash, response
11
+ assert_not_nil response['message']
12
+ assert(response['message']['code'] == 0)
13
+ assert(response['message']['text'] == 'OK')
14
+ assert_not_nil((response['businesses'] || response['neighborhoods']))
15
+ if response['businesses']
16
+ response['businesses'].each { |b| validate_json_to_ruby_business(b) }
17
+ end
18
+ end
19
+
20
+ YELP_BIZ_ID_LENGTH = 22
21
+
22
+ def validate_json_to_ruby_business (business)
23
+ # rudimentary checks to make sure it looks like a typical yelp business
24
+ # result converted to a ruby hash
25
+ assert business['id'].length == YELP_BIZ_ID_LENGTH
26
+ assert_valid_url business['url']
27
+ end
28
+
29
+ # just a rudimentary check will serve us fine
30
+ VALID_URL_REGEXP = /^https?:\/\/[a-z0-9]/i
31
+
32
+ def assert_valid_url (url)
33
+ assert_match VALID_URL_REGEXP, url
34
+ end
35
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.4
3
+ specification_version: 1
4
+ name: yelp
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.1.1
7
+ date: 2007-08-06 00:00:00 -07:00
8
+ summary: An object-oriented interface to the Yelp Developer API.
9
+ require_paths:
10
+ - lib
11
+ email: shaper@wgks.org
12
+ homepage: http://www.samskivert.com/shaper/yelp
13
+ rubyforge_project: yelp
14
+ description: An object-oriented interface to the Yelp Developer API.
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Walter Korman
31
+ files:
32
+ - History.txt
33
+ - LICENSE.txt
34
+ - Manifest.txt
35
+ - README.txt
36
+ - Rakefile
37
+ - TODO.txt
38
+ - lib/yelp.rb
39
+ - lib/yelp/client.rb
40
+ - lib/yelp/record.rb
41
+ - lib/yelp/response_format.rb
42
+ - lib/yelp/request.rb
43
+ - lib/yelp/neighborhood/request/base.rb
44
+ - lib/yelp/neighborhood/request/geo_point.rb
45
+ - lib/yelp/neighborhood/request/location.rb
46
+ - lib/yelp/phone/request/number.rb
47
+ - lib/yelp/review/request/base.rb
48
+ - lib/yelp/review/request/bounding_box.rb
49
+ - lib/yelp/review/request/geo_point.rb
50
+ - lib/yelp/review/request/location.rb
51
+ - test/test_client.rb
52
+ - test/test_neighborhood_search.rb
53
+ - test/test_phone_search.rb
54
+ - test/test_review_search.rb
55
+ - test/yelp_helper.rb
56
+ test_files:
57
+ - test/test_client.rb
58
+ - test/test_neighborhood_search.rb
59
+ - test/test_phone_search.rb
60
+ - test/test_review_search.rb
61
+ rdoc_options:
62
+ - --main
63
+ - README.txt
64
+ extra_rdoc_files:
65
+ - History.txt
66
+ - LICENSE.txt
67
+ - Manifest.txt
68
+ - README.txt
69
+ - TODO.txt
70
+ executables: []
71
+
72
+ extensions: []
73
+
74
+ requirements: []
75
+
76
+ dependencies:
77
+ - !ruby/object:Gem::Dependency
78
+ name: json
79
+ version_requirement:
80
+ version_requirements: !ruby/object:Gem::Version::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: 1.1.1
85
+ version:
86
+ - !ruby/object:Gem::Dependency
87
+ name: hoe
88
+ version_requirement:
89
+ version_requirements: !ruby/object:Gem::Version::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: 1.2.2
94
+ version: