Sha256: 2be515078da7ee8e62aeaf7aeee1a9e81f0c9d87785ab1484314a2cad2561c84

Contents?: true

Size: 857 Bytes

Versions: 2

Compression:

Stored size: 857 Bytes

Contents

module Rack
  module Congestion
    class PathLimiter < Limiter
      attr_accessor :path, :path_matcher

      def initialize(app, options = { })
        self.path = normalize_path_from options
        self.path_matcher = normalize_matcher_from options
        super
      end

      def _call(env)
        @env = env
        ignored? ? app.call(env) : super(env)
      end

      def key
        ->{ path }
      end

      def ignored?
        request.path !~ path_matcher
      end

      protected

      def normalize_path_from(options)
        self.path = options.delete :path
        path.sub(/^\/*/, '/').sub /\/+$/, ''
      end

      def normalize_matcher_from(options)
        self.path_matcher = options.delete :path_matcher
        return path_matcher if path_matcher
        Regexp.new path.sub(/^\^?/, '^'), 'i'
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rack-congestion-0.0.3 lib/rack/congestion/path_limiter.rb
rack-congestion-0.0.2 lib/rack/congestion/path_limiter.rb