Module: Download

Extended by:
Helper::Download
Included in:
Skynet
Defined in:
lib/skynet/download.rb

Overview

Module for handling inbound requests

Constant Summary collapse

HTTP_REDIRECT_PERMANENT =

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

[301, 302].freeze

Instance Method Summary collapse

Methods included from Helper::Download

default_options, strip_prefix

Instance Method Details

#download_file(file_name, skylink, override_options = {}, stream: true) ⇒ Object

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



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/skynet/download.rb', line 14

def download_file(file_name, skylink, override_options = {}, stream: true)
  options = Helper::Download.default_options
  options = Helper::Download.default_options.merge(override_options) unless override_options.empty?

  portal  = options[:portal_url]
  skylink = Helper::Download.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