Skip to content

Instantly share code, notes, and snippets.

Last active September 17, 2015 06:47
What would you like to do?
How many gyms/yoga/pilates in Boston/New York/LA? Ruby script to output a spreadsheet of the number of a given category in a given city according to Yelp.
require 'oauth'
require 'json'
require 'pp'
require 'csv'
consumer_key = 'NUM'
consumer_secret = 'NUM'
token = 'NUM'
token_secret = 'NUM'
api_host = ''
consumer =, consumer_secret, {:site => "http://#{api_host}"})
access_token =, token, token_secret)
cities = [
"New+York", "Los+Angeles", "Chicago", "Houston", "Philadelphia", "Pheonix", "San+Antonio", "San+Diego", "Dallas", "San+Jose", "Austin", "Jacksonville",
"Indianapolis", "San+Francisco", "Columbus", "Fort+Worth", "Charlotte", "Detroit", "El+Paso", "Memphis", "Boston", "Seattle", "Denver", "Washington",
"Nashville", "Baltimore", "Louisville", "Portland", "Oklahoma+City", "Milwaukee", "Las+Vegas", "Albuquerque", "Tucson", "Fresno", "Sacramento",
"Long+Beach", "Kansas+City", "Mesa", "Virginia+Beach", "Atlanta","Colorado+Springs", "Raleigh", "Omaha", "Miami", "Oakland", "Tulsa", "Minneapolis",
"Cleveland", "Wichita", "Arlington", "New+Orleans", "Bakersfield", "Tampa", "Honolulu", "Anaheim", "Aurora", "Santa+Ana", "St.+Louis,MA", "Riverside",
"Corpus+Christi", "Pittsburgh"
segments = [
"fitness+class", "bootcamp", "crossfit+gym", "yoga+class", "pilates+class", "martial+arts+class", "spin+class",
"biking+class", "boxing", "ballet+class", "barre+class"
]"yelp.csv", "wb") do |csv|
row =
row << "City"
for y in 0..(segments.length-1)
row << segments[y]
csv << row
for z in 0..(cities.length-1)
city = cities[z]
row =
row << city
pp city
for i in 0..(segments.length-1)
item = segments[i]
path = "/v2/search?term=#{item}&location=#{city}"
result = JSON.parse(access_token.get(path).body)
count = result["total"]
pp count
row << count
csv << row
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment