Sha256: d66dc1323f0312aac4a375298581926623a0b50a9116641d951d4133f41bec71
Contents?: true
Size: 981 Bytes
Versions: 9
Compression:
Stored size: 981 Bytes
Contents
# frozen_string_literal: true require 'open-uri' require 'net/http' require 'tempfile' require 'fileutils' module Reaver extend self def download(url, name) dest = name url = URI(url) raise Error, "url was invalid" if !url.respond_to?(:open) options = {} options['User-Agent'] = 'MyApp/1.2.3' downloaded_file = '' Whirly.start do Whirly.status = "downloading #{dest}" downloaded_file = URI.open(url, options) # open-uri will return a StringIO instead of a Tempfile if the filesize # is less than 10 KB, so we patch this behaviour by converting it into # a Tempfile. if downloaded_file.is_a?(StringIO) tempfile = Tempfile.new('open-uri', binmode: true) IO.copy_stream(downloaded_file, tempfile.path) downloaded_file = tempfile FileUtils.mv downloaded_file.path, dest else IO.copy_stream(downloaded_file, dest) end downloaded_file end end end
Version data entries
9 entries across 9 versions & 1 rubygems