Sha256: e79c7c329d92100272ad71ef3741b39d159bb38589ae48e65491f8cd3b86f342

Contents?: true

Size: 1.42 KB

Versions: 1

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true

require 'acfs/service/middleware/stack'

module Acfs
  class Service
    # Module providing all function to register middlewares
    # on services and process queued request through the
    # middleware stack.
    #
    module Middleware
      extend ActiveSupport::Concern

      # @api private
      # @return [Request]
      #
      def prepare(request)
        self.class.middleware.call request
      end

      module ClassMethods
        # @!method use(klass, *args, &block)
        #   @api public
        #
        #   Register a new middleware to be used for this service.
        #
        #   @example
        #     class MyService < Acfs::Service
        #       self.base_url = 'http://my.srv'
        #       use Acfs::Middleware::JSON
        #     end
        #
        #   @param [Class] klass Middleware class to append
        #   @param [Array<Object>] args Arguments passed to klass initialize
        #   @param [Proc] block Block passed to klass initialize
        #   @return [undefined]
        #
        def use(klass, ...)
          # Backward compatible behavior
          middleware.insert(0, klass, ...)
        end

        # @api private
        #
        # Return top most middleware.
        #
        # @return [#call]
        #
        def middleware
          @middleware ||= Stack.new
        end

        # @deprecated
        delegate :clear, to: :middleware
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
acfs-2.0.0 lib/acfs/service/middleware.rb