tcocca-yelp4r 1.0.2

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.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Tom Cocca
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,41 @@
1
+ yelp4r
2
+ ======
3
+
4
+ Yelp4r is a Ruby wrapper for the Yelp API. It utilizes the amazing HTTParty gem by John Nunemaker.
5
+ If you need to build an API wrapper that gem is an enourmous help, refer to the following:
6
+
7
+ http://railstips.org/2008/7/29/it-s-an-httparty-and-everyone-is-invited
8
+
9
+
10
+ Yelp API Docs
11
+ =========
12
+
13
+ http://www.yelp.com/developers/documentation
14
+
15
+ You will need to register for a Yelp API Key.
16
+
17
+
18
+ USAGE
19
+ ==========
20
+
21
+ See http://github.com/tcocca/yelp4r/tree/master/examples
22
+
23
+
24
+ INSTALATION
25
+ ==========
26
+
27
+ sudo gem install tcocca-yelp4r -s http://gems.github.com
28
+
29
+
30
+ QUESTION/CONCERNS/COMMENTS
31
+ ==========
32
+
33
+ Send me an email through github or at tom dot cocca at gmail dot com
34
+ Feel free to fork and submit changes
35
+ There is a test suite built on rspec.
36
+
37
+
38
+ COPYRIGHT
39
+ =========
40
+
41
+ Copyright (c) 2008 Tom Cocca. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,56 @@
1
+ require 'rake'
2
+ require 'rake/rdoctask'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |s|
7
+ s.name = "yelp4r"
8
+ s.summary = %Q{Yelp API wrapper in Ruby}
9
+ s.email = "tom.cocca@gmail.com"
10
+ s.homepage = "http://github.com/tcocca/yelp4r"
11
+ s.description = "Simple Ruby wrapper for the Yelp API built on HTTParty with parsers for available Neighborhoods and Categories"
12
+ s.files = [
13
+ "README",
14
+ "LICENSE",
15
+ "Rakefile",
16
+ "VERSION.yml",
17
+ "lib/yelp4r.rb",
18
+ "lib/yelp4r/categories.rb",
19
+ "lib/yelp4r/client.rb",
20
+ "lib/yelp4r/neighborhood_search.rb",
21
+ "lib/yelp4r/neighborhoods.rb",
22
+ "lib/yelp4r/phone_search.rb",
23
+ "lib/yelp4r/response.rb",
24
+ "lib/yelp4r/review_search.rb",
25
+ "spec/rcov.opts",
26
+ "spec/spec.opts",
27
+ "spec/spec_helper.rb",
28
+ "spec/fixtures/categories.html",
29
+ "spec/fixtures/neighborhoods.html",
30
+ "spec/yelp4r/categories_spec.rb",
31
+ "spec/yelp4r/client_spec.rb",
32
+ "spec/yelp4r/neighborhood_search_spec.rb",
33
+ "spec/yelp4r/neighborhoods_spec.rb",
34
+ "spec/yelp4r/phone_search_spec.rb",
35
+ "spec/yelp4r/review_search_spec.rb",
36
+ "examples/yelp.rb"
37
+ ]
38
+ s.authors = ["Tom Cocca"]
39
+ s.add_dependency 'httparty'
40
+ s.add_dependency 'hpricot'
41
+ end
42
+ rescue LoadError
43
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
44
+ end
45
+
46
+ Rake::RDocTask.new do |rdoc|
47
+ rdoc.rdoc_dir = 'rdoc'
48
+ rdoc.title = 'yelp4r'
49
+ rdoc.options << '--line-numbers' << '--inline-source'
50
+ rdoc.rdoc_files.include('README*')
51
+ rdoc.rdoc_files.include('lib/**/*.rb')
52
+ end
53
+
54
+ Dir['tasks/**/*.rake'].each { |t| load t }
55
+
56
+ task :default => :spec
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :patch: 2
3
+ :major: 1
4
+ :minor: 0
data/examples/yelp.rb ADDED
@@ -0,0 +1,164 @@
1
+ require File.dirname(__FILE__) + '/../lib/yelp4r'
2
+
3
+ # Read the Yelp API Documentation
4
+ # http://www.yelp.com/developers/documentation
5
+
6
+ # Initialize a new Yelp Client
7
+ client = Yelp4r::Client.new('your_ywsid_key')
8
+
9
+
10
+ # Initialize a new Phone Search object
11
+ phone_search = Yelp4r::PhoneSearch.new(client)
12
+
13
+ # Phone search only does one thing, looks up a business by a phone number
14
+ results = phone_search.search_by_phone_number('1234567890')
15
+
16
+ # A response object is returned
17
+ # The following response methods are available to all Yelp searches in the 3 search classes
18
+ # PhoneSearch, ReveiwSearch, NeighborhoodSearch
19
+ if results.success?
20
+ puts results.data # Pure data of the repsonse
21
+ puts results.body # Entire body of the response
22
+ else
23
+ puts results.reponse_code # Yelp repsonse code
24
+ puts results.error_message # The text of the error message
25
+ end
26
+
27
+
28
+ # Initialize a Neighborhood Search Object
29
+ neigh_search = Yelp4r::NeighborhoodSearch.new(client)
30
+
31
+ # Search by a geocode point
32
+ # Pass a Lat and Long respectively
33
+ results = neigh_search.search_by_geocode(37.788022, -122.399797)
34
+
35
+ # Search by a location (with optional country code)
36
+ results = neigh_search.search_by_location('Boston, MA')
37
+ # or
38
+ results = neigh_search.search_by_location('Boston, MA', 'US')
39
+
40
+ # Again the response methods above apply here as well
41
+
42
+
43
+ # Initialize a Review Search Object
44
+ review_search = Yelp4r::ReviewSearch.net(client)
45
+
46
+ # Each search method for the review_search object takes the following optional parameters
47
+ # :category => this is a Yelp category term (see the categories list below)
48
+ # :term => this is a search term
49
+ # :num_biz_requested => a number 1 through 20 (default is 10)
50
+
51
+ # Search by a bouding box
52
+ # Pass a top left lat/long and bottom right lat/long
53
+ results = review_search.search_by_bounding_box(38, -122.6, 37.788022, -122.399797)
54
+ results = review_search.search_by_bounding_box(38, -122.6, 37.788022, -122.399797, :term => "bars", :num_biz_requested => 15)
55
+
56
+ # Search by geocode and radius
57
+ # This method accepts and optional param of
58
+ # :radius => max of 25
59
+ results = review_search.search_by_geocode_and_radius(37.788022, -122.399797)
60
+ results = review_search.search_by_geocode_and_radius(37.788022, -122.399797, :radius => .5)
61
+ results = review_search.search_by_geocode_and_radius(37.788022, -122.399797, :category => "bars", :radius => 1)
62
+
63
+ # Search by location
64
+ # This method accepts and optional params of:
65
+ # :radius => max of 25
66
+ # :cc => country code
67
+ results = review_search.search_by_location('Boston, MA')
68
+ results = review_search.search_by_location('Boston, MA', :num_biz_requested => 10)
69
+ results = review_search.search_by_location('Boston, MA', :radius => 5)
70
+ results = review_search.search_by_location('Boston, MA', :term => "doctors")
71
+
72
+ # Again the response methods above apply here as well
73
+
74
+
75
+ # Yelp4r provides a couple of html parsed lists powered by Hpricot
76
+ # This first is the list of neighborhoods
77
+ # see the page here: http://www.yelp.com/developers/documentation/neighborhood_list
78
+
79
+ # Create a new Neighborhoods object
80
+ neighborhoods = Yelp4r::Neighborhoods.new
81
+
82
+ # Get the list
83
+ # This list is designed to keep the parent and its children in a hash with the parent being the key and the children in an array
84
+ # which is generated by a recursive function
85
+ # see the spec_helper method yelp4r_test_neighs_list for an example.
86
+
87
+ puts neighborhoods.list
88
+
89
+ # There is also a method to return a list of <option> tags to be used in a select tag in HTML
90
+ # The method takes an optional string for selected or an array if used with a multiple select
91
+ # The value of the options return the full string of the location
92
+
93
+ puts neighborhoods.options_from_list
94
+ # or
95
+ puts neighborhoods.options_from_list('Deep Cove, Vancouver, BC, Canada')
96
+ # or
97
+ puts neighborhoods.options_from_list(['Vancouver, BC, Canada', 'Deep Cove, Vancouver, BC, Canada'])
98
+
99
+ # To use this in a form see the following example:
100
+
101
+ =begin
102
+ #controller
103
+
104
+ def index
105
+ @neighs = Yelp4r::Neighborhoods.new
106
+ end
107
+
108
+ #view - index.html.erb
109
+
110
+ <% form_tag "/yelp" do %>
111
+ <%= select_tag :neighs, @neighs.options_from_list(params[:neighs] || []) %>
112
+ <br />
113
+ <%= select_tag :mult_neighs, @neighs.options_from_list(params[:mult_neighs] || []), :multiple => true, :size => 10 %>
114
+ <br />
115
+ <%= submit_tag "submit" %>
116
+ <% end %>
117
+
118
+ =end
119
+
120
+
121
+ # The second parser is the list of categories.
122
+ # See the page here: http://www.yelp.com/developers/documentation/category_list
123
+
124
+ # Create a Categories object
125
+ categories = Yelp4r::Categories.new
126
+
127
+ # Get the list
128
+ # As above the list keeps parents and children
129
+ # However this list is different as there is a "display" value and the "input" or "parameter" value
130
+ # In the hash the key is the "input" value, there is a :display value and if the element has children their is a :children value that is an array
131
+ # see the spec_helper method yelp4r_test_cats_list
132
+
133
+ puts categories.list
134
+
135
+ # As with Neighborhoods there is also a method to return a list of <option> tags to be used in a select tag in HTML for Categories
136
+ # The method takes an optional string for selected or an array if used with a multiple select
137
+ # This works exactly the same as Neighborhoods
138
+
139
+ puts categories.options_from_list
140
+ # or
141
+ puts categories.options_from_list("beaches")
142
+ # or
143
+ puts categories.options_from_list(["diving", "dancestudio", "golf"])
144
+
145
+ # To use this in a form see the following example: (basically the same exact thing as neighborhoods)
146
+
147
+ =begin
148
+ #controller
149
+
150
+ def index
151
+ @cats = Yelp4r::Categories.new
152
+ end
153
+
154
+ #view - index.html.erb
155
+
156
+ <% form_tag "/yelp" do %>
157
+ <%= select_tag :cats, @cats.options_from_list(params[:cats] || []) %>
158
+ <br />
159
+ <%= select_tag :mult_cats, @cats.options_from_list(params[:mult_cats] || []), :multiple => true, :size => 10 %>
160
+ <br />
161
+ <%= submit_tag "submit" %>
162
+ <% end %>
163
+
164
+ =end
data/lib/yelp4r.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'httparty'
3
+
4
+ require 'yelp4r/client'
5
+ require 'yelp4r/phone_search'
6
+ require 'yelp4r/neighborhood_search'
7
+ require 'yelp4r/review_search'
8
+ require 'yelp4r/response'
9
+ require 'yelp4r/neighborhoods'
10
+ require 'yelp4r/categories'
@@ -0,0 +1,73 @@
1
+ module Yelp4r
2
+ class Categories
3
+
4
+ attr_accessor :parse_url
5
+
6
+ def initialize
7
+ @parse_url = "http://www.yelp.com/developers/documentation/category_list"
8
+ end
9
+
10
+ def list
11
+ require 'hpricot'
12
+ require 'open-uri'
13
+ doc = open(parse_url) {|f| Hpricot(f)}
14
+ list = doc.search("/html/body/div[3]/div/div[2]/ul")
15
+ neighborhoods = process_list(list.first)
16
+ return neighborhoods
17
+ end
18
+
19
+ def options_from_list(selected = [])
20
+ selected_opts = selected.collect {|s| s.strip}
21
+ process_options(list, selected_opts)
22
+ end
23
+
24
+ private
25
+
26
+ def process_list(item)
27
+ item.children_of_type("li").map do |child|
28
+ unless child.inner_text.nil?
29
+ if child.next_sibling && child.next_sibling.name == "ul"
30
+ str_vals = decode_string(child.inner_text)
31
+ {str_vals[2] => {:display => str_vals[1], :children => process_list(child.next_sibling)}}
32
+ elsif child.name == "li"
33
+ str_vals = decode_string(child.inner_text)
34
+ {str_vals[2] => {:display => str_vals[1]}}
35
+ end
36
+ end
37
+ end
38
+ end
39
+
40
+ def decode_string(str)
41
+ /(.*)\s\(([^\)]*)/.match(str)
42
+ end
43
+
44
+ def process_options(item, selected, depth = 0, options = [])
45
+ if item.is_a?(Hash)
46
+ depth += 1
47
+ item.each do |key, val|
48
+ opt_selected = selected.include?(key) ? ' selected="selected"' : ''
49
+ options << %(<option value="#{key}"#{opt_selected}>#{option_prefix(depth)}#{val[:display]}</option>)
50
+ process_options(val[:children], selected, depth, options) if val[:children]
51
+ end
52
+ depth -= 1
53
+ elsif item.is_a?(Array)
54
+ item.each do |i|
55
+ process_options(i, selected, depth, options)
56
+ end
57
+ end
58
+ return options
59
+ end
60
+
61
+ def option_prefix(depth)
62
+ s = ""
63
+ if depth > 1
64
+ (depth - 1).times do
65
+ s += "&nbsp;-"
66
+ end
67
+ s += "&nbsp;"
68
+ end
69
+ return s
70
+ end
71
+
72
+ end
73
+ end
@@ -0,0 +1,13 @@
1
+ module Yelp4r
2
+ class Client
3
+
4
+ include HTTParty
5
+ base_uri 'api.yelp.com'
6
+ format :json
7
+
8
+ def initialize(ywsid)
9
+ self.class.default_params :ywsid => ywsid
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,28 @@
1
+ module Yelp4r
2
+ class NeighborhoodSearch
3
+
4
+ attr_accessor :client
5
+
6
+ def initialize(client)
7
+ @client = client
8
+ end
9
+
10
+ def search_by_geocode(lat, long)
11
+ options = {:lat => lat, :long => long}
12
+ process(options)
13
+ end
14
+
15
+ def search_by_location(location, cc = "")
16
+ options = {:location => location}
17
+ options.merge!({:cc => cc}) unless cc.blank?
18
+ process(options)
19
+ end
20
+
21
+ private
22
+
23
+ def process(options)
24
+ Response.new(@client.class.get('/neighborhood_search', :query => options))
25
+ end
26
+
27
+ end
28
+ end
@@ -0,0 +1,82 @@
1
+ module Yelp4r
2
+ class Neighborhoods
3
+
4
+ attr_accessor :parse_url
5
+
6
+ def initialize
7
+ @parse_url = "http://www.yelp.com/developers/documentation/neighborhood_list"
8
+ end
9
+
10
+ def list
11
+ require 'hpricot'
12
+ require 'open-uri'
13
+ doc = open(parse_url) {|f| Hpricot(f)}
14
+ list = doc.search("/html/body/div[3]/div/div[2]/ul")
15
+ neighborhoods = process_list(list.first)
16
+ return neighborhoods
17
+ end
18
+
19
+ def options_from_list(selected = [])
20
+ selected_opts = selected.collect {|s| s.strip}
21
+ process_options(list, selected_opts)
22
+ end
23
+
24
+ private
25
+
26
+ def process_list(item)
27
+ item.children_of_type("li").map do |child|
28
+ unless child.inner_text.nil?
29
+ if child.next_sibling && child.next_sibling.name == "ul"
30
+ {child.inner_text => process_list(child.next_sibling)}
31
+ elsif child.name == "li"
32
+ child.inner_text
33
+ end
34
+ end
35
+ end
36
+ end
37
+
38
+ def process_options(item, selected, depth = 0, options = [], parent = "")
39
+ if item.is_a?(Hash)
40
+ depth += 1
41
+ item.each do |key, val|
42
+ unless parent.blank?
43
+ opt = "#{key}, #{parent}"
44
+ else
45
+ opt = key
46
+ end
47
+ opt_selected = selected.include?(opt) ? ' selected="selected"' : ''
48
+ options << %(<option value="#{opt}"#{opt_selected}>#{option_prefix(depth)}#{key}</option>)
49
+ process_options(val, selected, depth, options, opt)
50
+ end
51
+ depth -= 1
52
+ elsif item.is_a?(Array)
53
+ item.each do |i|
54
+ process_options(i, selected, depth, options, parent)
55
+ end
56
+ else
57
+ depth += 1
58
+ unless parent.blank?
59
+ opt = "#{item}, #{parent}"
60
+ else
61
+ opt = item
62
+ end
63
+ opt_selected = selected.include?(opt) ? ' selected="selected"' : ''
64
+ options << %(<option value="#{opt}"#{opt_selected}>#{option_prefix(depth)}#{item}</option>)
65
+ depth -= 1
66
+ end
67
+ return options
68
+ end
69
+
70
+ def option_prefix(depth)
71
+ s = ""
72
+ if depth > 1
73
+ (depth - 1).times do
74
+ s += "&nbsp;-"
75
+ end
76
+ s += "&nbsp;"
77
+ end
78
+ return s
79
+ end
80
+
81
+ end
82
+ end