class Paquet::Utils
Constants
- HTTPS_SCHEME
- REDIRECTION_LIMIT
Public Class Methods
download_file(source, destination, counter = REDIRECTION_LIMIT)
click to toggle source
# File lib/paquet/utils.rb, line 10 def self.download_file(source, destination, counter = REDIRECTION_LIMIT) raise "Too many redirection" if counter == 0 begin f = File.open(destination, "wb") uri = URI.parse(source) http = Net::HTTP.new(uri.host, uri.port, ) http.use_ssl = uri.scheme == HTTPS_SCHEME response = http.get(uri.path) case response when Net::HTTPSuccess f.write(response.body) when Net::HTTPRedirection counter -= 1 download_file(response['location'], destination, counter) else raise "Response not handled: #{response.class}, path: #{uri.path}" end f.path rescue => e FileUtils.rm_rf(f.path) rescue nil raise e ensure f.close end end