Sha256: 5fa7eaec012072dc79b96bd352b3a8ec263c08abe04928f374f2ea39266e9cfa

Contents?: true

Size: 1.02 KB

Versions: 32

Compression:

Stored size: 1.02 KB

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 [201, 302, 303].include? response.status #success if it is a redirect or 201
      response.headers['Location']
    else
      raise 'FailedFileUpload'
    end
  end

  def confirm_file_upload(url)
    uri = URI(url)
    query = uri.query
    query.blank? ? get(uri.path) : get(uri.path, CGI::parse(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

32 entries across 32 versions & 1 rubygems

Version Path
bearcat-1.3.41 lib/bearcat/client/file_helper.rb
bearcat-1.3.40 lib/bearcat/client/file_helper.rb
bearcat-1.3.39 lib/bearcat/client/file_helper.rb
bearcat-1.3.38 lib/bearcat/client/file_helper.rb
bearcat-1.3.37 lib/bearcat/client/file_helper.rb
bearcat-1.3.36 lib/bearcat/client/file_helper.rb
bearcat-1.3.35 lib/bearcat/client/file_helper.rb
bearcat-1.3.34 lib/bearcat/client/file_helper.rb
bearcat-1.3.33 lib/bearcat/client/file_helper.rb
bearcat-1.3.32 lib/bearcat/client/file_helper.rb
bearcat-1.3.30 lib/bearcat/client/file_helper.rb
bearcat-1.3.28 lib/bearcat/client/file_helper.rb