Sha256: 0e92dfe11c4c2213c9ae1f5ead2139b7cd409031e15739e198b2c3c9bdadd151

Contents?: true

Size: 1.21 KB

Versions: 37

Compression:

Stored size: 1.21 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)
        uri.scheme = uri.host = uri.port = nil
        get(uri.to_s)
      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

37 entries across 37 versions & 1 rubygems

Version Path
bearcat-1.5.37 lib/bearcat/client/file_helper.rb
bearcat-1.5.36 lib/bearcat/client/file_helper.rb
bearcat-1.5.35 lib/bearcat/client/file_helper.rb
bearcat-1.5.34 lib/bearcat/client/file_helper.rb
bearcat-1.5.33 lib/bearcat/client/file_helper.rb
bearcat-1.5.32 lib/bearcat/client/file_helper.rb
bearcat-1.5.31 lib/bearcat/client/file_helper.rb
bearcat-1.5.30 lib/bearcat/client/file_helper.rb
bearcat-1.5.29 lib/bearcat/client/file_helper.rb
bearcat-1.5.28 lib/bearcat/client/file_helper.rb
bearcat-1.5.28.beta1 lib/bearcat/client/file_helper.rb
bearcat-1.5.26 lib/bearcat/client/file_helper.rb
bearcat-1.5.24 lib/bearcat/client/file_helper.rb
bearcat-1.5.23 lib/bearcat/client/file_helper.rb
bearcat-1.5.20 lib/bearcat/client/file_helper.rb
bearcat-1.5.19 lib/bearcat/client/file_helper.rb
bearcat-1.5.17 lib/bearcat/client/file_helper.rb
bearcat-1.5.16 lib/bearcat/client/file_helper.rb
bearcat-1.5.15 lib/bearcat/client/file_helper.rb
bearcat-1.5.14 lib/bearcat/client/file_helper.rb