Skip to content

Instantly share code, notes, and snippets.

@mk-qi
Forked from Burgestrand/download-progress.rb
Created January 12, 2016 14:19
Show Gist options
  • Select an option

  • Save mk-qi/4274da00b32d557c7895 to your computer and use it in GitHub Desktop.

Select an option

Save mk-qi/4274da00b32d557c7895 to your computer and use it in GitHub Desktop.

Revisions

  1. @Burgestrand Burgestrand created this gist Jun 27, 2010.
    23 changes: 23 additions & 0 deletions download-progress.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    require 'net/http'
    require 'uri'

    def download(url)
    Thread.new do
    thread = Thread.current
    body = thread[:body] = []

    url = URI.parse url
    Net::HTTP.new(url.host, url.port).request_get(url.path) do |response|
    length = thread[:length] = response['Content-Length'].to_i

    response.read_body do |fragment|
    body << fragment
    thread[:done] = (thread[:done] || 0) + fragment.length
    thread[:progress] = thread[:done].quo(length) * 100
    end
    end
    end
    end

    thread = download 'http://caesar.acc.umu.se/mirror/ubuntu-releases/10.04/ubuntu-10.04-desktop-i386.iso'
    puts "%.2f%%" % thread[:progress].to_f until thread.join 1