Skip to content

Instantly share code, notes, and snippets.

@arunthampi
Last active March 23, 2017 23:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arunthampi/4f37721b70ffdb5354ea to your computer and use it in GitHub Desktop.
Save arunthampi/4f37721b70ffdb5354ea to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'excon'
def download_file(url, source_path = nil)
error = nil
if source_path.nil?
source_path = File.join("/tmp", "output")
end
file_handle = File.open(source_path, 'wb')
streamer = lambda do |chunk, remaining_bytes, total_bytes|
file_handle.write(chunk)
end
puts "about to start connection to '#{url}'"
connection = Excon.new(url, connect_timeout: 360, debug: true, omit_default_port: true)
begin
connection.request(expects: 200,
method: :get,
idempotent: true,
retry_limit: 6,
read_timeout: 360,
response_block: streamer)
rescue Excon::Errors::Error => e
puts e
file_handle.close
file_handle = nil
ensure
file_handle.close if file_handle
end
return source_path, error
end
download_file('http://mclov.in/')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment