Class: FastSubmissionProtection::SubmissionTimer

Inherits:
Object
  • Object
show all
Defined in:
lib/fast_submission_protection/submission_timer.rb

Instance Method Summary collapse

Constructor Details

#initialize(storage, key, delay = nil, clock = nil) ⇒ SubmissionTimer

Returns a new instance of SubmissionTimer.



7
8
9
10
11
# File 'lib/fast_submission_protection/submission_timer.rb', line 7

def initialize storage, key, delay = nil, clock = nil
  @storage, @key = storage, key
  @delay = delay || self.class.delay
  @clock = clock || self.class.clock
end

Instance Method Details

#clearObject



26
27
28
# File 'lib/fast_submission_protection/submission_timer.rb', line 26

def clear
  @storage.delete @key
end

#restartObject



22
23
24
# File 'lib/fast_submission_protection/submission_timer.rb', line 22

def restart
  @storage[@key] = @clock.now
end

#startObject



18
19
20
# File 'lib/fast_submission_protection/submission_timer.rb', line 18

def start
  @storage[@key] ||= @clock.now
end

#too_fast?Boolean

Returns:

  • (Boolean)


13
14
15
16
# File 'lib/fast_submission_protection/submission_timer.rb', line 13

def too_fast?
  started = @storage[@key]
  !started || (started + @delay > @clock.now)
end