Created
April 19, 2015 14:29
-
-
Save hieuk09/09d485626911bdfd7c79 to your computer and use it in GitHub Desktop.
Download with progress bar
This file contains hidden or 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 'open-uri' | |
require 'fileutils' | |
require 'ruby-progressbar' | |
progress_bar = ProgressBar.create( | |
starting_at: 0, | |
total: nil, | |
format: "%a %B %p%% %r KB/sec", | |
rate_scale: lambda { |rate| rate / 1024 } | |
) | |
content_length_proc = Proc.new { |content_length| | |
progress_bar.total = content_length | |
} | |
progress_proc = Proc.new { |bytes_transferred| | |
if progress_bar.total && progress_bar.total < bytes_transferred | |
progress_bar.total = nil | |
end | |
progress_bar.progress = bytes_transferred | |
} | |
open(link, "rb", content_length_proc: content_length_proc, progress_proc: progress_proc) do |page| | |
File.open(file_path, "wb") do |file| | |
file.write(page.read) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment