Class: Rack::NimbleThrottling

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/nimble_throttling.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ NimbleThrottling

Returns a new instance of NimbleThrottling.



3
4
5
# File 'lib/rack/nimble_throttling.rb', line 3

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/rack/nimble_throttling.rb', line 7

def call(env)
  req = Request.new(env)

  if NimbleThrottler.endpoints.include?(req.path)
    NimbleThrottler.throttle_for(req)
    if NimbleThrottler.exceed_limit?(req)
      message = "Rate limit exceeded. Try again in #{NimbleThrottler.expires_in(req)} seconds"
      [429, { 'Content-Type' => 'text/html', 'Content-Length' => message.size.to_s }, [message]]
    else
      normal_response(env)
    end
  else
    normal_response(env)
  end
end

#normal_response(env) ⇒ Object



23
24
25
26
# File 'lib/rack/nimble_throttling.rb', line 23

def normal_response(env)
  status, headers, body = @app.call(env)
  [status, headers, body]
end