Skip to content

Instantly share code, notes, and snippets.

@hieuk09
Created April 19, 2015 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hieuk09/09d485626911bdfd7c79 to your computer and use it in GitHub Desktop.
Save hieuk09/09d485626911bdfd7c79 to your computer and use it in GitHub Desktop.
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