Sha256: b721ddcd51d8032e91b5caf04db7e042925a97a8e749325a69862bb296c9dc95

Contents?: true

Size: 1.32 KB

Versions: 6

Compression:

Stored size: 1.32 KB

Contents

# frozen_string_literal: true

module PriceHubble
  module Client
    module Utils
      # Some helpers to prepare requests in a general way.
      #
      module Request
        extend ActiveSupport::Concern

        included do
          # A common HTTP content type to symbol
          # mapping for correct header settings.
          CONTENT_TYPE = {
            json: 'application/json',
            multipart: 'multipart/form-data',
            url_encoded: 'application/x-www-form-urlencoded'
          }.freeze

          # Use the configured identity to authenticate the given request.
          #
          # @param req [Faraday::Request] the request to manipulate
          def use_authentication(req)
            req.params.merge!(access_token: PriceHubble.identity.access_token)
          end

          # Use the default request context to identificate the request.
          #
          # @param action [String, Symbol] the used client action
          # @param req [Faraday::Request] the request to manipulate
          def use_default_context(req, action)
            req.options.context ||= {}
            req.options.context.merge!(client: self.class,
                                       action: action,
                                       request_id: SecureRandom.hex(3))
          end
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
pricehubble-0.4.2 lib/pricehubble/client/utils/request.rb
pricehubble-0.4.1 lib/pricehubble/client/utils/request.rb
pricehubble-0.4.0 lib/pricehubble/client/utils/request.rb
pricehubble-0.3.0 lib/pricehubble/client/utils/request.rb
pricehubble-0.2.0 lib/pricehubble/client/utils/request.rb
pricehubble-0.1.0 lib/pricehubble/client/utils/request.rb