Created
September 11, 2014 22:39
-
-
Save brunopgalvao/7e1f33923abc3f495752 to your computer and use it in GitHub Desktop.
Query foursquare for businesses.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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