Skip to content

Instantly share code, notes, and snippets.

@airblade
Created February 5, 2013 10:53
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 airblade/4713705 to your computer and use it in GitHub Desktop.
Save airblade/4713705 to your computer and use it in GitHub Desktop.
Typhoeus example.
require 'typhoeus'
class ImageDownloader
# Returns an array of images at the given `urls`.
# Each element is either a StringIO representation of an image
# or nil if the image could not be downloaded for any reason.
#
# Params:
# urls - an array of URLs pointing to images
def get(urls, options = {})
options = defaults.merge options
results = Array.new urls.length
hydra = Typhoeus::Hydra.new
urls.each_with_index do |url, index|
request = Typhoeus::Request.new url, options
request.on_complete do |response|
results[index] = response.success? ? StringIO.new(response.body) : nil
end
hydra.queue request
end
hydra.run
results
end
private
def defaults
{timeout: 10000} # 10s
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment