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



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

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} (#{SizeFormatter.format(content_length)}) %.2f%%" %
              (size.to_f / content_length * 100)
    end
  ) { |f| File.open(dest, "wb") { |out| out.write f.read } }
  puts "\r"
end