Class: RailsThrottle::Throttle

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

Class Method Summary collapse

Class Method Details

.decrement(key, options = {}) ⇒ Integer

Decrements the counter for a given key.

Parameters:

  • key (String)

    The key that uniquely identifies this throttle operation.

  • options (Hash) (defaults to: {})

    The options to this increment.

Options Hash (options):

  • :decrement (Integer)

    The amount to decrement the counter by, defaults to 1 if not provided.

  • :period (Integer)

    The period of time (in seconds) which this throttle applies, the throttle will expire after this number of seconds.

Returns:

  • (Integer)

    The current value of the throttle counter.



58
59
60
61
62
63
64
65
66
67
# File 'lib/rails_throttle/throttle.rb', line 58

def self.decrement(key, options = {})
  raise "Key cannot be blank." if key.blank?

  decrement = options[:decrement] || 1
  period = options[:period]

  raise "Must specify :period in the parameters" if period.nil?

  RailsThrottle.backend.decrement(key, decrement, expires_in: period)
end

.increment(key, options = {}) {|key, value| ... } ⇒ Integer

Increments the counter for a given key.

Parameters:

  • key (String)

    The key that uniquely identifies this throttle operation.

  • options (Hash) (defaults to: {})

    The options to this increment.

Options Hash (options):

  • :increment (Integer)

    The amount to increment the counter by, defaults to 1 if not provided.

  • :limit (Integer)

    The maximum value the key can take before it is throttled (i.e. this key will be throttled once exceeded this limit).

  • :period (Integer)

    The period of time (in seconds) which this throttle applies, the throttle will expire after this number of seconds.

Yields:

  • (key, value)

    Yields up to two parameters (you can choose whether you want zero, one, or two of these parameters).

Yield Parameters:

  • key (String)

    The key that was passed in.

  • value (Integer)

    The current value of the throttle counter.

Returns:

  • (Integer)

    The value of the block, or the current value of the throttle counter if no block is given.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rails_throttle/throttle.rb', line 17

def self.increment(key, options = {}, &block)
  raise "Key cannot be blank" if key.blank?

  increment = options[:increment] || 1
  limit = options[:limit]
  period = options[:period]

  raise "Must specify :limit in the parameters" if limit.nil?
  raise "Must specify :period in the parameters" if period.nil?

  value = RailsThrottle.backend.increment(key, increment, expires_in: period)

  if value.nil?
    RailsThrottle.backend.write(key, increment, expires_in: period)

    value = increment
  elsif value > limit
    raise RailsThrottle::ThrottleError, "Limit exceeded for key `#{key}` with limit of #{limit}"
  end

  unless block.nil?
    if block.arity.zero?
      block.call
    elsif block.arity == 1
      block.call(key)
    else
      block.call(key, value)
    end
  else
    value
  end
end

.reset(key) ⇒ Object

Resets a key so that it is no longer throttled.

Parameters:

  • key (String)

    The key that uniquely identifies this throttle operation.



91
92
93
94
95
# File 'lib/rails_throttle/throttle.rb', line 91

def self.reset(key)
  raise "Key cannot be blank." if key.blank?

  RailsThrottle.backend.delete(key)
end

.throttled?(key, options = {}) ⇒ Boolean

Returns true if the key is throttled, false otherwise.

Parameters:

  • key (String)

    The key that uniquely identifies this throttle operation.

  • options (Hash) (defaults to: {})

    The options to this increment.

Options Hash (options):

  • :limit (Integer)

    The maximum value the key can take before it is throttled (i.e. this key will be throttled once exceeded this limit).

Returns:

  • (Boolean)

    True if the key is throttled, false otherwise.



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/rails_throttle/throttle.rb', line 76

def self.throttled?(key, options = {})
  raise "Key cannot be blank." if key.blank?

  limit = options[:limit]

  raise "Must specify :limit in the parameters" if limit.nil?

  value = RailsThrottle.backend.read(key) || 0

  value >= limit
end