Class: LeakyBucket::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/leaky_bucket/middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

Returns a new instance of Middleware.



5
6
7
# File 'lib/leaky_bucket/middleware.rb', line 5

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/leaky_bucket/middleware.rb', line 9

def call(env)
  begin
    @app.call(env)
  rescue LeakyBucket::TooManyRequests => e
    body = "Limit exceeded"
    headers = { "Content-Type" => "text/plain", "Content-Length" => 14, "Retry-After" => "#{e.retry_in}" }
    [ 429, headers, [ body ]]
  end
end