Sha256: b96950af7898ecf1c4a315717c8122d10cc922b92d8459653b78b3ed49281c33

Contents?: true

Size: 1.24 KB

Versions: 1

Compression:

Stored size: 1.24 KB

Contents

module Bearcat
  class Client < Footrest::Client
    module FileHelper
      def file_params(file_path)
        {
          size: File.open(file_path).size,
          name: File.basename(file_path)
        }.with_indifferent_access
      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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bearcat-1.4.13 lib/bearcat/client/file_helper.rb