Created
February 5, 2013 10:53
-
-
Save airblade/4713705 to your computer and use it in GitHub Desktop.
Typhoeus example.
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
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