Skip to content

Instantly share code, notes, and snippets.

@brunopgalvao
Created September 11, 2014 22:39
Show Gist options
  • Save brunopgalvao/7e1f33923abc3f495752 to your computer and use it in GitHub Desktop.
Save brunopgalvao/7e1f33923abc3f495752 to your computer and use it in GitHub Desktop.
Query foursquare for businesses.
namespace :populate do
desc "Populate data from Foursqaure API"
task :foursquare => :environment do
client = Foursquare2::Client.new(:client_id => 'CLIENT_ID', :client_secret => 'CLIENT_SECRET', :api_version => '20140904')
Cities.data_path = "lib/tasks/cities"
cities = Cities.cities_in_country('US')
limit = 5000 # Foursqaure rate limit up to 5,000 userless requests per hour to venues/* endpoints.
cities.each do |key, city|
break unless limit > 0
venues = client.search_venues(:ll => "#{city.latitude},#{city.longitude}", :query => 'Restaurants')["venues"]
venues.each do |venue|
Business.find_or_create_by_business_name(venue["name"]) do |business|
Location.find_or_create_by_id(business.location_id) do |location|
location.address = venue["location"]["address"]
location.city = venue["location"]["city"]
location.state = venue["location"]["state"]
location.country_code = venue["location"]["cc"]
location.zipcode = venue["location"]["postalCode"]
end
end
limit = limit -1
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment