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, override_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
14 def download_file(file_name, skylink, override_options = {}, stream: true)
15   options = Helper::Download.default_options
16   options = Helper::Download.default_options.merge(override_options) unless override_options.empty?
17 
18   portal  = options[:portal_url]
19   skylink = Helper::Download.strip_prefix(skylink)
20   url = "#{portal}#{skylink}?attachment=#{options[:download]}"
21 
22   File.open(file_name, 'w') do |file|
23     HTTParty.get(url, follow_redirects: true, stream_body: stream) do |chunk|
24       file << chunk unless HTTP_REDIRECT_PERMANENT.include? chunk.code
25     end
26   end
27 
28   'Download successful!'
29 end