Sha256: c048a7fac297b275026a77f8e13a855361c091715905c9712b0a2ea2c9b382fb

Contents?: true

Size: 1.17 KB

Versions: 1

Compression:

Stored size: 1.17 KB

Contents

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

      # Queue a new request. The request will travel through
      # all registered middleware.
      #
      def queue(req)
        super self.class.middleware.call req
      end

      module ClassMethods

        # Register a new middleware to be used for this service.
        #
        # class MyService < Acfs::Service
        #   self.base_url = 'http://my.srv'
        #   use Acfs::Middleware::JsonDecoder
        # end
        #
        def use(klass, options = {})
          @middlewares ||= []

          return false if @middlewares.include? klass

          @middlewares << klass
          @middleware = klass.new(middleware, options)
        end

        # Return top most middleware.
        #
        def middleware
          @middleware ||= proc { |request| request}
        end

        # Clear all registered middlewares.
        #
        def clear
          @middleware  = nil
          @middlewares = nil
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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