Class: ActionThrottler::Actions

Inherits:
Object
  • Object
show all
Defined in:
lib/action_throttler/actions.rb

Class Method Summary collapse

Class Method Details

.add(action = nil, &block) ⇒ Object

Adds an action to the stack

Parameters:

  • action (Symbol) (defaults to: nil)

    the name of the action, must be unique

  • block (Block)

    the configuration block



8
9
10
11
12
13
14
15
16
17
# File 'lib/action_throttler/actions.rb', line 8

def add(action = nil, &block)
  @actions ||= {}
  
  raise "A name to the ActionThrottler action is required!" if action.nil?
  
  config = ActionThrottler::Config.new
  block.call(config)

  @actions[action] = config.to_hash
end

.can_be_run?(action, ref = "") ⇒ Boolean

Checks the database to see if an action can be run

Parameters:

  • action (Symbol)

    the name of the action to be run

  • ref (Mixed) (defaults to: "")

    (optional) the reference object

Returns:

  • (Boolean)

    returns true or false depending on the outcome



24
25
26
27
28
29
30
31
# File 'lib/action_throttler/actions.rb', line 24

def can_be_run?(action, ref = "")
  ::ActionThrottlerLog.all(:conditions => [
    "scope = ? AND reference = ? AND created_at >= ?",
    action.to_s,
    normalise_ref(ref),
    @actions[action][:duration].ago
  ]).size < @actions[action][:limit] ? true : false
end

.can_run(action, ref = "") ⇒ Object

See Also:

  • selfself::run


55
56
57
# File 'lib/action_throttler/actions.rb', line 55

def can_run(action, ref = "")
  run(action, ref)
end

.cannot_be_run?(action, ref = "") ⇒ Boolean

Returns:

  • (Boolean)

See Also:

  • selfself::can_be_run?


34
35
36
# File 'lib/action_throttler/actions.rb', line 34

def cannot_be_run?(action, ref = "")
  not can_be_run?(action, ref)
end

.cannot_run(action, ref = "") ⇒ Object

See Also:

  • selfself::run


60
61
62
# File 'lib/action_throttler/actions.rb', line 60

def cannot_run(action, ref = "")
  ! run(action, ref)
end

.run(action, ref = "") ⇒ Boolean

Runs an action and registers it in the database

Parameters:

  • action (Symbol)

    the name of the action to be run

  • ref (Mixed) (defaults to: "")

    (optional) the reference object

Returns:

  • (Boolean)

    returns true or false depending on the outcome



43
44
45
46
47
48
49
50
51
52
# File 'lib/action_throttler/actions.rb', line 43

def run(action, ref = "")
  if can_be_run?(action, ref)
    ::ActionThrottlerLog.create({
      :scope     => action.to_s,
      :reference => normalise_ref(ref)
    })
  else
    false
  end
end