Class: DontRepeatFor

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(time, key) ⇒ DontRepeatFor

Returns a new instance of DontRepeatFor.

Parameters:

  • time (Integer)

    The amount of seconds that we don’t want to repeat the process for

  • key (String)

    A unique key to identify the process that we don’t want to repeat for the given time



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

#keyObject

Returns the value of attribute key.



16
17
18
# File 'lib/dont_repeat_for.rb', line 16

def key
  @key
end

#timeObject

Returns the value of attribute time.



16
17
18
# File 'lib/dont_repeat_for.rb', line 16

def time
  @time
end