Class: Rack::Attack::RateLimit
- Inherits:
-
Object
- Object
- Rack::Attack::RateLimit
- Defined in:
- lib/rack/attack/rate-limit.rb,
lib/rack/attack/rate-limit/version.rb
Constant Summary collapse
- RACK_ATTACK_KEY =
'rack.attack.throttle_data'
- VERSION =
'1.0.0'
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
-
#add_rate_limit_headers!(headers, env) ⇒ Object
Return hash of headers with Rate Limiting data.
- #call(env) ⇒ Object
-
#default_options ⇒ Object
Default options to configure Rack::RateLimit.
-
#initialize(app, options = {}) ⇒ RateLimit
constructor
A new instance of RateLimit.
-
#rack_attack_key ⇒ Object
Returns env key used by Rack::Attack to namespace data.
- #throttle ⇒ Object
Constructor Details
#initialize(app, options = {}) ⇒ RateLimit
Returns a new instance of RateLimit.
15 16 17 18 |
# File 'lib/rack/attack/rate-limit.rb', line 15 def initialize(app, = {}) @app = app @options = .merge() end |
Instance Attribute Details
#app ⇒ Object (readonly)
Returns the value of attribute app.
13 14 15 |
# File 'lib/rack/attack/rate-limit.rb', line 13 def app @app end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
13 14 15 |
# File 'lib/rack/attack/rate-limit.rb', line 13 def @options end |
Instance Method Details
#add_rate_limit_headers!(headers, env) ⇒ Object
Return hash of headers with Rate Limiting data
headers - Hash of headers
Returns hash
52 53 54 55 56 |
# File 'lib/rack/attack/rate-limit.rb', line 52 def add_rate_limit_headers!(headers, env) headers['X-RateLimit-Limit'] = rate_limit_limit(env).to_s headers['X-RateLimit-Remaining'] = rate_limit_remaining(env).to_s headers end |
#call(env) ⇒ Object
20 21 22 23 24 25 26 27 |
# File 'lib/rack/attack/rate-limit.rb', line 20 def call(env) # If env does not have necessary data to extract rate limit data for the provider, then app.call return app.call(env) unless rate_limit_available?(env) # Otherwise, add rate limit headers status, headers, body = app.call(env) add_rate_limit_headers!(headers, env) [status, headers, body] end |
#default_options ⇒ Object
Default options to configure Rack::RateLimit
Returns hash
39 40 41 |
# File 'lib/rack/attack/rate-limit.rb', line 39 def { throttle: 'throttle' } end |
#rack_attack_key ⇒ Object
Returns env key used by Rack::Attack to namespace data
Returns string
32 33 34 |
# File 'lib/rack/attack/rate-limit.rb', line 32 def rack_attack_key RACK_ATTACK_KEY end |
#throttle ⇒ Object
43 44 45 |
# File 'lib/rack/attack/rate-limit.rb', line 43 def throttle [:throttle] || '' end |