Class: Rack::NimbleThrottling
- Inherits:
-
Object
- Object
- Rack::NimbleThrottling
- Defined in:
- lib/rack/nimble_throttling.rb
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app) ⇒ NimbleThrottling
constructor
A new instance of NimbleThrottling.
- #normal_response(env) ⇒ Object
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) = "Rate limit exceeded. Try again in #{NimbleThrottler.expires_in(req)} seconds" [429, { 'Content-Type' => 'text/html', 'Content-Length' => .size.to_s }, []] 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 |