class Falkor::Download

Attributes

file_name[R]
url[R]

Public Class Methods

new(url, file_name) click to toggle source
# File lib/falkor/download.rb, line 9
def initialize(url, file_name)
  @url = url
  @file_name = file_name
end

Public Instance Methods

download() click to toggle source
# File lib/falkor/download.rb, line 14
def download
  return destination if File.exist?(destination)

  request do |response, file_size|
    next unless success?(response)

    report_progress(:write_chunks, file_size, response, &Proc.new)
    destination
  end
end

Private Instance Methods

destination() click to toggle source
# File lib/falkor/download.rb, line 66
def destination
  File.join(Dir.pwd, "tmp", file_name)
end
https?() click to toggle source
# File lib/falkor/download.rb, line 45
def https?
  uri.scheme == "https"
end
request() { |response, to_i| ... } click to toggle source
# File lib/falkor/download.rb, line 29
def request
  return_value = nil

  Net::HTTP.start(uri.host, uri.port, use_ssl: https?) do |http|
    http.request(Net::HTTP::Get.new(uri)) do |response|
      return_value = yield response, response["content-length"].to_i
    end
  end

  return_value
end
success?(response) click to toggle source
# File lib/falkor/download.rb, line 62
def success?(response)
  (200..299).cover?(response.code.to_i)
end
uri() click to toggle source
# File lib/falkor/download.rb, line 41
def uri
  @uri ||= URI(url)
end
write_chunks(response) { |size| ... } click to toggle source
# File lib/falkor/download.rb, line 49
def write_chunks(response)
  FileUtils.mkdir_p "tmp"
  FileUtils.rm_rf destination

  File.open(destination, "wb") do |file|
    response.read_body do |chunk|
      file.write chunk

      yield(chunk.size)
    end
  end
end