Last active
March 23, 2017 23:00
-
-
Save arunthampi/4f37721b70ffdb5354ea to your computer and use it in GitHub Desktop.
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 '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