Class: SimpleRateLimiter::Domain::Violations

Inherits:
Object
  • Object
show all
Defined in:
lib/domain/violations.rb

Instance Method Summary collapse

Constructor Details

#initialize(punishment_factor, limit_period) ⇒ Violations

Returns a new instance of Violations.



4
5
6
7
# File 'lib/domain/violations.rb', line 4

def initialize(punishment_factor, limit_period)
  @punishment_factor = punishment_factor
  @limit_period = limit_period
end

Instance Method Details

#apply?(violation_records, time = Time.now.to_i) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
18
19
20
21
# File 'lib/domain/violations.rb', line 15

def apply?(violation_records,  time = Time.now.to_i)
  violation_period = get_violation_period(violation_records)
  time_elapsed = get_time_elapsed(violation_records, time)
  return true if time_elapsed < violation_period

  false
end

#exist?(records) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
12
13
# File 'lib/domain/violations.rb', line 9

def exist?(records)
  return true unless records.empty?

  false
end

#expired?(violation_records, time = Time.now.to_i) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
29
30
# File 'lib/domain/violations.rb', line 23

def expired?(violation_records, time = Time.now.to_i)
  time_elapsed = get_time_elapsed(violation_records, time)
  violation_period = get_violation_period(violation_records)
  post_violation_period = violation_period * 2
  return false if time_elapsed < post_violation_period

  true
end

#get_time_elapsed(violation_records, time) ⇒ Object



36
37
38
# File 'lib/domain/violations.rb', line 36

def get_time_elapsed(violation_records, time)
  time - violation_records.last.to_i
end

#get_violation_period(violation_records) ⇒ Object



32
33
34
# File 'lib/domain/violations.rb', line 32

def get_violation_period(violation_records)
  (@punishment_factor**(violation_records.length - 1)) * @limit_period
end