module Download

Module for handling inbound requests

Constants

HTTP_REDIRECT_PERMANENT

Array with http redirect & http permanent code Used to determine if the GET request should start downloading or follow redirect

Public Instance Methods

download_file(file_name, skylink, options = {}, stream = true) click to toggle source

Download file from the skynet portal file_name & skylink is required, the rest is optional since they come with default values

# File lib/skynet/download.rb, line 15
def download_file(file_name, skylink, options = {}, stream = true)
  options = Helper::Download.default_options
  options = Helper::Download.default_options.merge(options) unless options.empty?

  portal  = options[:portal_url]
  skylink = Skynet.strip_prefix(skylink)
  url = "#{portal}#{skylink}?attachment=#{options[:download]}"

  File.open(file_name, 'w') do |file|
    HTTParty.get(url, follow_redirects: true, stream_body: stream) do |chunk|
      file << chunk unless HTTP_REDIRECT_PERMANENT.include? chunk.code
    end
  end

  'Download successful!'
end