Class: Raidis::Throttle
- Inherits:
-
Object
- Object
- Raidis::Throttle
- Defined in:
- lib/raidis/throttle.rb
Instance Attribute Summary collapse
-
#interval ⇒ Object
readonly
Returns the value of attribute interval.
-
#last_action ⇒ Object
readonly
Returns the value of attribute last_action.
Instance Method Summary collapse
- #action! ⇒ Object
-
#initialize(seconds = 3) ⇒ Throttle
constructor
A new instance of Throttle.
- #sleep_if_needed ⇒ Object
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
#interval ⇒ Object (readonly)
Returns the value of attribute interval.
4 5 6 |
# File 'lib/raidis/throttle.rb', line 4 def interval @interval end |
#last_action ⇒ Object (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_needed ⇒ Object
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 |