Class: Coppertone::RequestCountLimiter

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#countObject

Returns the value of attribute count.



5
6
7
# File 'lib/coppertone/request_count_limiter.rb', line 5

def count
  @count
end

#counter_descriptionObject

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

#limitObject

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_exceededObject



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, exception_message if exceeded?
end

#exceeded?Boolean

Returns:

  • (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_messageObject



24
25
26
# File 'lib/coppertone/request_count_limiter.rb', line 24

def exception_message
  "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

Returns:

  • (Boolean)


34
35
36
# File 'lib/coppertone/request_count_limiter.rb', line 34

def limited?
  !limit.nil?
end