Sha256: 27bcea76d5ded073eeaa815deb2aeb60cf40ebb1c9b8159be2754f3bc0916130

Contents?: true

Size: 1.46 KB

Versions: 8

Compression:

Stored size: 1.46 KB

Contents

=begin

Conditionally use Rack Middleware.

Usage:

condition_proc = ->(env) { env['PATH_INFO'] == '/match' }
use_when condition_proc, SomeMiddleware, options

I feel sure there must be something like this officially supported somewhere, but I can't find it.

=end

module Rack
  module PactBroker
    module UseWhen
      class ConditionallyUseMiddleware
        def initialize(app, condition_proc, middleware, *args, **kwargs, &block)
          @app_without_middleware = app
          @condition_proc = condition_proc
          @middleware = middleware
          @args = args
          @kwargs = kwargs
          @block = block
        end

        def call(env)
          if condition_proc.call(env)
            app_with_middleware.call(env)
          else
            app_without_middleware.call(env)
          end
        end

        private

        attr_reader :app_without_middleware, :condition_proc, :middleware, :args, :kwargs, :block

        def app_with_middleware
          @app_with_middleware ||= begin
            rack_builder = ::Rack::Builder.new
            rack_builder.use middleware, *args, **kwargs, &block
            rack_builder.run app_without_middleware
            rack_builder.to_app
          end
        end
      end

      refine Rack::Builder do
        def use_when(condition_proc, middleware, *args, **kwargs, &block)
          use(ConditionallyUseMiddleware, condition_proc, middleware, *args, **kwargs, &block)
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
pact_broker-2.112.0 lib/rack/pact_broker/use_when.rb
pact_broker-2.111.0 lib/rack/pact_broker/use_when.rb
pact_broker-2.109.1 lib/rack/pact_broker/use_when.rb
pact_broker-2.109.0 lib/rack/pact_broker/use_when.rb
pact_broker-2.108.0 lib/rack/pact_broker/use_when.rb
pact_broker-2.107.1 lib/rack/pact_broker/use_when.rb
pact_broker-2.107.0 lib/rack/pact_broker/use_when.rb
pact_broker-2.107.0.beta.1 lib/rack/pact_broker/use_when.rb