class Rack::Berater::Handler

Public Class Methods

new(app, options = {}) click to toggle source
# File lib/rack/berater/handler.rb, line 4
def initialize(app, options = {})
  @app = app
  @options = {
    status_code: options.fetch(:status_code, 429),
    headers: {
      Rack::CONTENT_TYPE => "text/plain",
    }.update(options.fetch(:headers, {})),
    body: options.fetch(:body, true),
  }
end

Public Instance Methods

call(env) click to toggle source
# File lib/rack/berater/handler.rb, line 15
def call(env)
  @app.call(env)
rescue ::Berater::Overloaded => e
  code = @options[:status_code]

  body = case @options[:body]
    when true
      Rack::Utils::HTTP_STATUS_CODES[code]
    when nil, false
      nil
    when String
      @options[:body]
    when Proc
      @options[:body].call(env, e)
    else
      raise ArgumentError, "invalid :body option: #{@options[:body]}"
    end

  headers = body ? @options[:headers] : {}

  [
    code,
    headers,
    [ body ].compact,
  ]
end