Class: DontRepeatFor
- Inherits:
-
Object
- Object
- DontRepeatFor
- Defined in:
- lib/dont_repeat_for.rb,
lib/dont_repeat_for/version.rb
Overview
Runs a block only once in the given time frame Uses redis to know if we’ve run the block before. Example 1: “‘ruby
20.times do
DontRepeatFor.new("test_dont_repeat/test", 5.minutes) { puts "This message will only be displayed once every 5 minutes.";
sleep(1.minute)
end
“‘
@hint: You can include an ID in the key to enforce rate limiting for a specific store.
Constant Summary collapse
- VERSION =
'1.0.0.0'
Instance Attribute Summary collapse
-
#key ⇒ Object
Returns the value of attribute key.
-
#time ⇒ Object
Returns the value of attribute time.
Instance Method Summary collapse
-
#initialize(time, key) ⇒ DontRepeatFor
constructor
A new instance of DontRepeatFor.
Constructor Details
#initialize(time, key) ⇒ DontRepeatFor
Returns a new instance of DontRepeatFor.
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/dont_repeat_for.rb', line 21 def initialize(time, key) @key = key @time = time.to_i return nil if ran_recently? remember_not_to_repeat! yield end |
Instance Attribute Details
#key ⇒ Object
Returns the value of attribute key.
16 17 18 |
# File 'lib/dont_repeat_for.rb', line 16 def key @key end |
#time ⇒ Object
Returns the value of attribute time.
16 17 18 |
# File 'lib/dont_repeat_for.rb', line 16 def time @time end |