Class: Bottleneck::Core
- Inherits:
-
Object
- Object
- Bottleneck::Core
- Defined in:
- lib/bottleneck/core.rb
Instance Method Summary collapse
-
#initialize(ip) ⇒ Core
constructor
Create a Core object.
-
#run ⇒ Hash
Run main logic initialization, set and read Redis keys and validation requests count and time limit.
Constructor Details
#initialize(ip) ⇒ Core
Create a Core object.
10 11 12 13 14 |
# File 'lib/bottleneck/core.rb', line 10 def initialize(ip) @ip = ip.to_s @storage = Bottleneck.storage @limits = Bottleneck.config["limits"] end |
Instance Method Details
#run ⇒ Hash
Run main logic initialization, set and read Redis keys and validation requests count and time limit.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/bottleneck/core.rb', line 19 def run client_ip = @ip key = "request_count:#{client_ip}" result = { status: Constants::SUCCESS_STATUS, message: Constants::OK_MESSAGE } requests_count = @storage.get(key) unless requests_count @storage.set(key, 0) @storage.expire(key, @limits["time_period_seconds"]) end if requests_count.to_i >= @limits["max_requests_count"] result[:status] = Constants::EXPIRED_STATUS result[:message] = (period(key)) else @storage.incr(key) end result end |