Class: Raidis::Throttle

Inherits:
Object
  • Object
show all
Defined in:
lib/raidis/throttle.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(seconds = 3) ⇒ Throttle

Returns a new instance of Throttle.



6
7
8
# File 'lib/raidis/throttle.rb', line 6

def initialize(seconds = 3)
  @interval = seconds.to_i
end

Instance Attribute Details

#intervalObject (readonly)

Returns the value of attribute interval.



4
5
6
# File 'lib/raidis/throttle.rb', line 4

def interval
  @interval
end

#last_actionObject (readonly)

Returns the value of attribute last_action.



4
5
6
# File 'lib/raidis/throttle.rb', line 4

def last_action
  @last_action
end

Instance Method Details

#action!Object



17
18
19
# File 'lib/raidis/throttle.rb', line 17

def action!
  @last_action = Time.now
end

#sleep_if_neededObject



10
11
12
13
14
15
# File 'lib/raidis/throttle.rb', line 10

def sleep_if_needed
  return unless last_action
  duration = last_action + interval - Time.now
  return unless duration.to_i > 0
  sleep duration
end