Sha256: 0af5b6dc34ad9b501db39486dcd8747f0755a655b2e3e6f03ab5f195c2b87324

Contents?: true

Size: 929 Bytes

Versions: 2

Compression:

Stored size: 929 Bytes

Contents

module Rack
  module EncodingGuard
    class Middleware
      attr_reader :app, :strategy

      def initialize(app, strategy = nil, options = {})
        @app = app
        @strategy = resolve_strategy_object(strategy, options)
      end

      def call(env)
        strategy.process(env) do
          app.call(env)
        end
      end

      private

      def resolve_strategy_object(strategy, options)
        case strategy
        when nil then RejectStrategy.new(options)
        when Class then strategy.new(options)
        when String then strategy.constantize.new(options)
        when Symbol
          class_name = "#{strategy.to_s.camelize}Strategy"
          EncodingGuard.const_get(class_name).new(options)
        else
          unless strategy.respond_to?(:process)
            fail ArgumentError, "Invalid strategy: #{strategy.inspect}"
          end
          strategy
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rack-encoding_guard-0.1.1 lib/rack/encoding_guard/middleware.rb
rack-encoding_guard-0.1.0 lib/rack/encoding_guard/middleware.rb