Sha256: 5cc60f7affa96d7c4b62040772fad245a660c809c619320f3c8edb0133e23eaf

Contents?: true

Size: 948 Bytes

Versions: 14

Compression:

Stored size: 948 Bytes

Contents

module Prop

  # Convenience middleware that conveys the message configured on a Prop handle as well
  # as time left before the current window has passed in a Retry-After header.
  class Middleware

    # Default error handler
    class DefaultErrorHandler
      def self.call(env, error)
        body    = error.description || "This action has been rate limited"
        headers = { "Content-Type" => "text/plain", "Content-Length" => "#{body.size}", "Retry-After" => "#{error.retry_after}" }

        [ 429, headers, [ body ]]
      end
    end

    def initialize(app, options = {})
      @app     = app
      @options = options
      @handler = options[:error_handler] || DefaultErrorHandler
    end

    def call(env)
      begin
        @app.call(env)
      rescue Prop::RateLimited => e
        render_response(env, e)
      end
    end

    protected

    def render_response(env, error)
      @handler.call(env, error)
    end
  end

end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
prop-2.1.1 lib/prop/middleware.rb
prop-2.1.0 lib/prop/middleware.rb
prop-2.0.4 lib/prop/middleware.rb
prop-2.0.3 lib/prop/middleware.rb
prop-2.0.2 lib/prop/middleware.rb
prop-2.0.1 lib/prop/middleware.rb
prop-2.0.0 lib/prop/middleware.rb
prop-1.2.0 lib/prop/middleware.rb
prop-1.1.0 lib/prop/middleware.rb
prop-1.0.2 lib/prop/middleware.rb
prop-1.0.1 lib/prop/middleware.rb
prop-1.0.0 lib/prop/middleware.rb
prop-0.7.8 lib/prop/middleware.rb
prop-0.7.7 lib/prop/middleware.rb