Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Download with progress bar
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