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 =
"0.1.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.
9 10 11 12 |
# File 'lib/rack/attack/rate-limit.rb', line 9 def initialize(app, = {}) @app = app @options = .merge() end |
Instance Attribute Details
#app ⇒ Object (readonly)
Returns the value of attribute app.
7 8 9 |
# File 'lib/rack/attack/rate-limit.rb', line 7 def app @app end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
7 8 9 |
# File 'lib/rack/attack/rate-limit.rb', line 7 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
46 47 48 49 50 |
# File 'lib/rack/attack/rate-limit.rb', line 46 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
14 15 16 17 18 19 20 21 |
# File 'lib/rack/attack/rate-limit.rb', line 14 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
33 34 35 |
# File 'lib/rack/attack/rate-limit.rb', line 33 def { throttle: 'throttle' } end |
#rack_attack_key ⇒ Object
Returns env key used by Rack::Attack to namespace data
Returns string
26 27 28 |
# File 'lib/rack/attack/rate-limit.rb', line 26 def rack_attack_key RACK_ATTACK_KEY end |
#throttle ⇒ Object
37 38 39 |
# File 'lib/rack/attack/rate-limit.rb', line 37 def throttle [:throttle] || '' end |