Class: Coppertone::RequestCountLimiter
- Inherits:
-
Object
- Object
- Coppertone::RequestCountLimiter
- Defined in:
- lib/coppertone/request_count_limiter.rb
Overview
A utility class that encapsulates counter and limit behavior. Primarily used to track and limit the number of DNS queries of various types.
Instance Attribute Summary collapse
-
#count ⇒ Object
Returns the value of attribute count.
-
#counter_description ⇒ Object
Returns the value of attribute counter_description.
-
#limit ⇒ Object
Returns the value of attribute limit.
Instance Method Summary collapse
- #check_if_limit_exceeded ⇒ Object
- #exceeded? ⇒ Boolean
- #exception_message ⇒ Object
- #increment!(num = 1) ⇒ Object
-
#initialize(limit = nil, counter_description = nil) ⇒ RequestCountLimiter
constructor
A new instance of RequestCountLimiter.
- #limited? ⇒ Boolean
Constructor Details
#initialize(limit = nil, counter_description = nil) ⇒ RequestCountLimiter
Returns a new instance of RequestCountLimiter.
7 8 9 10 11 |
# File 'lib/coppertone/request_count_limiter.rb', line 7 def initialize(limit = nil, counter_description = nil) self.limit = limit self.counter_description = counter_description self.count = 0 end |
Instance Attribute Details
#count ⇒ Object
Returns the value of attribute count.
5 6 7 |
# File 'lib/coppertone/request_count_limiter.rb', line 5 def count @count end |
#counter_description ⇒ Object
Returns the value of attribute counter_description.
5 6 7 |
# File 'lib/coppertone/request_count_limiter.rb', line 5 def counter_description @counter_description end |
#limit ⇒ Object
Returns the value of attribute limit.
5 6 7 |
# File 'lib/coppertone/request_count_limiter.rb', line 5 def limit @limit end |
Instance Method Details
#check_if_limit_exceeded ⇒ Object
19 20 21 22 |
# File 'lib/coppertone/request_count_limiter.rb', line 19 def check_if_limit_exceeded return if limit.nil? raise Coppertone::LimitExceededError, if exceeded? end |
#exceeded? ⇒ Boolean
28 29 30 31 32 |
# File 'lib/coppertone/request_count_limiter.rb', line 28 def exceeded? return false unless limited? count > limit end |
#exception_message ⇒ Object
24 25 26 |
# File 'lib/coppertone/request_count_limiter.rb', line 24 def "Maximum #{counter_description} limit of #{limit} exceeded." end |
#increment!(num = 1) ⇒ Object
13 14 15 16 17 |
# File 'lib/coppertone/request_count_limiter.rb', line 13 def increment!(num = 1) self.count += num check_if_limit_exceeded count end |
#limited? ⇒ Boolean
34 35 36 |
# File 'lib/coppertone/request_count_limiter.rb', line 34 def limited? !limit.nil? end |