Created
March 14, 2012 18:18
-
-
Save bkenny/2038381 to your computer and use it in GitHub Desktop.
Threading API Calls
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
# | |
# Short example on threaded API calls | |
# | |
# This code snippet is from spots.io and how it threads API calls to lookup whether a foursqaure location has | |
# Instagram photos or not. | |
# | |
# Create an sexual empty array | |
location_threads = [] | |
# Loop through each venue found for photos | |
@venues.each do |check| | |
# For each location - add a new API call to check if it's been listed in Instagram | |
location_threads << location_check(check.id) | |
end | |
# Join the results and throw them into @locations | |
location_threads.each{|thread| thread.join} | |
@locations = location_threads.map(&:value) | |
# Create another batch of threads to grab all of the thumbnail imagery for each authentic Instagram location | |
image_threads = [] | |
@locations.each do |images| | |
if images.data.count > 0 | |
image_threads << image_check(images.data.first.id) | |
end | |
end | |
# Output the goodness into @images | |
image_threads.each{|thread| thread.join} | |
@images = image_threads.map(&:value) | |
# location_check and image_check just run through the API's but here's for reference | |
def location_check(fs) | |
arg = "https://api.instagram.com/v1/locations/search?foursquare_v2_id=" + fs + "&client_id=CLIENT_ID" | |
Thread.new { | |
Hashie::Mash.new(HTTParty.get(arg)) | |
} | |
end | |
def image_check(id) | |
arg = "https://api.instagram.com/v1/locations/" + id + "/media/recent/?client_id=CLIENT_ID" | |
Thread.new { | |
Hashie::Mash.new(HTTParty.get(arg)) | |
} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment