Class: Rack::Throttle::Limiter
- Inherits:
-
Object
- Object
- Rack::Throttle::Limiter
- Defined in:
- lib/rack/throttle/limiter.rb
Overview
This is the base class for rate limiter implementations.
Direct Known Subclasses
Constant Summary collapse
- CODE =
429
- MESSAGE =
"Rate Limit Exceeded"
Instance Attribute Summary collapse
-
#app ⇒ Object
readonly
Returns the value of attribute app.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#allowed?(request) ⇒ Boolean
Returns ‘false` if the rate limit has been exceeded for the given `request`, or `true` otherwise.
-
#blacklisted?(request) ⇒ Boolean
abstract
Returns ‘true` if the originator of the given `request` is blacklisted (not honoring rate limits, and thus permanently forbidden access without the need to maintain further rate limit counters).
- #call(env) ⇒ Array(Integer, Hash, #each)
-
#initialize(app, options = {}) ⇒ Limiter
constructor
A new instance of Limiter.
-
#whitelisted?(request) ⇒ Boolean
abstract
Returns ‘true` if the originator of the given `request` is whitelisted (not subject to further rate limits).
Constructor Details
#initialize(app, options = {}) ⇒ Limiter
Returns a new instance of Limiter.
28 29 30 |
# File 'lib/rack/throttle/limiter.rb', line 28 def initialize(app, = {}) @app, @options = app, end |
Instance Attribute Details
#app ⇒ Object (readonly)
Returns the value of attribute app.
14 15 16 |
# File 'lib/rack/throttle/limiter.rb', line 14 def app @app end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
15 16 17 |
# File 'lib/rack/throttle/limiter.rb', line 15 def @options end |
Instance Method Details
#allowed?(request) ⇒ Boolean
Returns ‘false` if the rate limit has been exceeded for the given `request`, or `true` otherwise.
Override this method in subclasses that implement custom rate limiter strategies.
50 51 52 53 54 55 56 |
# File 'lib/rack/throttle/limiter.rb', line 50 def allowed?(request) case when whitelisted?(request) then true when blacklisted?(request) then false else true # override in subclasses end end |
#blacklisted?(request) ⇒ Boolean
Returns ‘true` if the originator of the given `request` is blacklisted (not honoring rate limits, and thus permanently forbidden access without the need to maintain further rate limit counters).
The default implementation always returns ‘false`. Override this method in a subclass to implement custom blacklisting logic.
83 84 85 |
# File 'lib/rack/throttle/limiter.rb', line 83 def blacklisted?(request) false end |
#call(env) ⇒ Array(Integer, Hash, #each)
36 37 38 39 |
# File 'lib/rack/throttle/limiter.rb', line 36 def call(env) request = Rack::Request.new(env) allowed?(request) ? app.call(env) : rate_limit_exceeded(request) end |
#whitelisted?(request) ⇒ Boolean
Returns ‘true` if the originator of the given `request` is whitelisted (not subject to further rate limits).
The default implementation always returns ‘false`. Override this method in a subclass to implement custom whitelisting logic.
68 69 70 |
# File 'lib/rack/throttle/limiter.rb', line 68 def whitelisted?(request) false end |