Sha256: 9dce8c7c0d5bfb33bbd6c74b0eae3cd971e7b8d647e36ffafb153ed6f51ce293

Contents?: true

Size: 844 Bytes

Versions: 2

Compression:

Stored size: 844 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
        @handler.call(env, e)
      end
    end
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
prop-0.7.3 lib/prop/middleware.rb
prop-0.7.2 lib/prop/middleware.rb