Skip to content

Instantly share code, notes, and snippets.

@tvladeck
Created July 17, 2012 14:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tvladeck/3129864 to your computer and use it in GitHub Desktop.
Save tvladeck/3129864 to your computer and use it in GitHub Desktop.
Touching the Yelp API
require 'oauth'
require 'json'
consumer_key = 'etdTVJFmBsqOjZITP_MqJQ'
consumer_secret = 'S1DUC0OqdRWrbvuhjVuF5QI3cEY'
token = 'm_fA4dzJ78ppRTrd2otSPFOTEx2Q_J4c'
token_secret = 'hL8scdaQ0gRMLPOj5ZCF7Wg3XvA'
api_host = 'api.yelp.com'
consumer = OAuth::Consumer.new(consumer_key, consumer_secret, {:site => "http://#{api_host}"})
access_token = OAuth::AccessToken.new(consumer, token, token_secret)
#--------This is an array that contains the category_filters that
#--------produce results in different verticals on yelp
@filters = ["galleries", #art galleries
"fashion", #clothing stores
"jewelry", #jewelry
]
#--------This produces a hash of arrays
#--------key: filter name
#--------value: array of objects with [name, lat, lng]
#--------returns k * 20 results for each filter
k = 2
store_array = {}
@filters.each do |filter_name|
j = 0
store_array[filter_name] = []
k.times do
offset = j*20
path = "/v2/search?location=san%20francisco&category_filter=#{filter_name}&offset=#{offset}"
result = access_token.get(path).body
businesses = JSON.parse(result)["businesses"]
store_array[filter_name] << businesses
j += 1
end
end
CSV.open("storelist.csv","w") do |csv|
csv << ["name","url","phone","categories","display phone","cross streets","city","display address","neighborhoods",
"postal_code","state_code"]
store_array.each do |filter, stores|
stores.flatten.each do |h|
loc = h["location"]
csv << [h["name"], h["url"], h["phone"], h["categories"], h["display_phone"], loc["cross_streets"],
loc["city"], loc["display_address"], loc["neighborhoods"], loc["postal_code"], loc["state_code"]]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment