Sha256: 8c9054010d2b407c18f779b52d7ada9b220f71ffb3dc7ecbc0b4507633590a13

Contents?: true

Size: 1.23 KB

Versions: 7

Compression:

Stored size: 1.23 KB

Contents

require 'footrest/client'

module Catalogcat
  class Client < Footrest::Client
    require 'catalogcat/api_array' # monkey patch

    Dir[File.join(__dir__, 'client', '*.rb')].each do |mod|
      mname = File.basename(mod, '.*').camelize
      require mod
      include "Catalogcat::Client::#{mname}".constantize
    end

    # Override Footrest connection to use Token authorization
    # rubocop:disable Naming/AccessorMethodName
    def set_connection(config)
      super
      connection.builder.insert(Footrest::RaiseFootrestErrors, ExtendedRaiseFootrestErrors)
      connection.builder.delete(Footrest::RaiseFootrestErrors)
      connection.headers[:authorization].sub! 'Bearer', 'Token'
    end
    # rubocop:enable Naming/AccessorMethodName

    # Override Footrest request for ApiArray support
    def request(method, &block)
      response = connection.send(method, &block)
      raise Footrest::HttpError::ErrorBase, response if response.status >= 400

      Catalogcat::ApiArray.process_response(response, self)
    end
  end

  class ExtendedRaiseFootrestErrors < Footrest::RaiseFootrestErrors
    def on_complete(response)
      super
      key = response[:status].to_i
      raise ERROR_MAP[key.floor(-2)], response if key >= 400
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
bearcat-1.5.3 lib/catalogcat/client.rb
bearcat-1.5.2 lib/catalogcat/client.rb
bearcat-1.5.0 lib/catalogcat/client.rb
bearcat-1.5.0.beta4 lib/catalogcat/client.rb
bearcat-1.5.0.beta3 lib/catalogcat/client.rb
bearcat-1.5.0.beta2 lib/catalogcat/client.rb
bearcat-1.5.0.beta1 lib/catalogcat/client.rb