Created
March 17, 2013 13:14
-
-
Save atomgiant/5181455 to your computer and use it in GitHub Desktop.
Curl url downloader as an alternative to curb
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
# Helper class to save urls to a file | |
class UrlLoader | |
# Download a url and optionally specify a path to save it to a file. | |
def self.get(url, path=nil) | |
# set curl to timeout after 30 minutes for any calls | |
cmd = "curl --fail --location --silent --show-error --max-redirs 10 --connect-timeout 60 --max-time 1800" | |
cmd += " --output #{path}" unless path.nil? | |
cmd += " '#{url}'" | |
rsp = `#{cmd}` | |
raise "Received non-success response code for url: '#{url}'" if (path.nil? ? rsp.empty? : (!File.exists?(path) || File.size(path) == 0)) | |
path.nil? ? rsp : File.new(path) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment