Class: Aspera::TimerLimiter

Inherits:
Object
  • Object
show all
Defined in:
lib/aspera/timer_limiter.rb

Overview

trigger returns true only if the delay has passed since the last trigger

Instance Method Summary collapse

Constructor Details

#initialize(delay) ⇒ TimerLimiter

Returns a new instance of TimerLimiter.

Parameters:

  • delay

    in seconds (float)



7
8
9
10
11
# File 'lib/aspera/timer_limiter.rb', line 7

def initialize(delay)
  @delay = delay
  @last_trigger_time = nil
  @count = 0
end

Instance Method Details

#trigger?Boolean

Check if the trigger condition is met

Returns:

  • (Boolean)

    true if the trigger condition is met, false otherwise



15
16
17
18
19
20
21
22
23
24
# File 'lib/aspera/timer_limiter.rb', line 15

def trigger?
  current_time = Time.now.to_f
  @count += 1
  if @last_trigger_time.nil? || ((current_time - @last_trigger_time) > @delay)
    @last_trigger_time = current_time
    @count = 0
    return true
  end
  return false
end