Skip to content

Instantly share code, notes, and snippets.

@atomgiant
Created March 17, 2013 13:14
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 atomgiant/5181455 to your computer and use it in GitHub Desktop.
Save atomgiant/5181455 to your computer and use it in GitHub Desktop.
Curl url downloader as an alternative to curb
# 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