Class: RubyWasm::Downloader

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_wasm/build/downloader.rb

Instance Method Summary collapse

Instance Method Details

#download(url, dest, message) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ruby_wasm/build/downloader.rb', line 13

def download(url, dest, message)
  require "open-uri"
  content_length = 0
  uri = URI.parse(url)
  OpenURI.open_uri(
    uri,
    content_length_proc: ->(len) { content_length = len },
    progress_proc: ->(size) do
      print "\r#{message} (#{format_size(content_length)}) %.2f%%" %
              (size.to_f / content_length * 100)
    end
  ) { |f| File.open(dest, "wb") { |out| out.write f.read } }
  puts "\r"
end

#format_size(size) ⇒ Object



3
4
5
6
7
8
9
10
11
# File 'lib/ruby_wasm/build/downloader.rb', line 3

def format_size(size)
  units = %w[B KB MB GB TB]
  unit = 0
  while size > 1024 and unit < units.size - 1
    size /= 1024.0
    unit += 1
  end
  "%s #{units[unit]}" % size.round(2)
end