Sha256: 9bf0681d206d747c3d3b63912091ad65ba2cefa4e1bc77de09d95d7465379ba4

Contents?: true

Size: 840 Bytes

Versions: 2

Compression:

Stored size: 840 Bytes

Contents

module Rack
  module Congestion
    class Limiter
      attr_accessor :app, :options
      attr_reader :env

      def initialize(app, options = { })
        self.app = app
        self.options = options
      end

      def call(env)
        dup._call env
      end

      def _call(env)
        @env = env
        request.allowed? ? app.call(env) : rejected_response
      end

      def request
        @request ||= Rack::Congestion::Request.new env, key, options
      end

      def key
        ->{ 'global' }
      end

      def rejected_response
        [
          429,
          {
            'Content-Type' => 'text/plain; charset=utf-8',
            'Retry-After' => backoff
          },
          ['Rate Limit Exceeded']
        ]
      end

      def backoff
        request.backoff.to_s rescue -1
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

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