Sha256: 4833d526309917298d5334c8d737f8a4f4d4318a12a1ee3c0c011bc3f537e022

Contents?: true

Size: 983 Bytes

Versions: 10

Compression:

Stored size: 983 Bytes

Contents

module FileHelper

  def file_params(file_path)
    {
      size: File.open(file_path).size,
      name: File.basename(file_path)
    }
  end

  def declare_file(api_path, params)
    post(api_path, params)
  end

  def post_file(url, params, file_path)
    params['Filename'] = File.basename(file_path)
    params['file'] = Faraday::UploadIO.new(file_path, params['content-type'])
    response = upload_connection.post(url, params)
    if [302, 303].include? response.status #success if it is a redirect
      response.headers['Location']
    else
      raise 'FailedFileUpload'
    end
  end

  def confirm_file_upload(url)
    uri = URI(url)
    post(uri.path, CGI::parse(uri.query))
  end

  def upload_connection
    Faraday.new do |f|
      f.options[:open_timeout] = ENV.fetch('FARADAY_OPEN_TIMEOUT', 60).to_i
      f.options[:timeout] = ENV.fetch('FARADAY_TIMEOUT', 60).to_i
      f.request :multipart
      f.request :url_encoded
      f.adapter :net_http
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
bearcat-1.3.27 lib/bearcat/client/file_helper.rb
bearcat-1.3.26 lib/bearcat/client/file_helper.rb
bearcat-1.3.25 lib/bearcat/client/file_helper.rb
bearcat-1.3.24 lib/bearcat/client/file_helper.rb
bearcat-1.3.23 lib/bearcat/client/file_helper.rb
bearcat-1.3.22 lib/bearcat/client/file_helper.rb
bearcat-1.3.21 lib/bearcat/client/file_helper.rb
bearcat-1.3.20 lib/bearcat/client/file_helper.rb
bearcat-1.3.19 lib/bearcat/client/file_helper.rb
bearcat-1.3.18 lib/bearcat/client/file_helper.rb