Sha256: 807208d3b97248ce8f9223d74357b9d51689052474a4393c3a653e6e0b755945

Contents?: true

Size: 834 Bytes

Versions: 1

Compression:

Stored size: 834 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(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(e)
      end
    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
prop-0.7.1 lib/prop/middleware.rb