Advertisement
Guest User

Untitled

a guest
May 28th, 2015
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. require 'open-uri'
  2. require 'fileutils'
  3. require 'ruby-progressbar'
  4.  
  5. progress_bar = ProgressBar.create(
  6. starting_at: 0,
  7. total: nil,
  8. format: "%a %B %p%% %r KB/sec",
  9. rate_scale: lambda { |rate| rate / 1024 }
  10. )
  11.  
  12. content_length_proc = Proc.new { |content_length|
  13. progress_bar.total = content_length
  14. }
  15.  
  16. progress_proc = Proc.new { |bytes_transferred|
  17. if progress_bar.total && progress_bar.total < bytes_transferred
  18. progress_bar.total = nil
  19. end
  20. progress_bar.progress = bytes_transferred
  21. }
  22.  
  23. open(link, "rb", content_length_proc: content_length_proc, progress_proc: progress_proc) do |page|
  24. File.open(file_path, "wb") do |file|
  25. file.write(page.read)
  26. end
  27. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement