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.

Returns:

  • (Integer)

    The current value of the throttle counter.



56
57
58
59
60
61
62
# File 'lib/rails_throttle/throttle.rb', line 56

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

  decrement = options[:decrement] || 1

  RailsThrottle.backend.decrement(key, decrement)
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 current value of the throttle counter.



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)

  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
  end

  value
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.



86
87
88
89
90
# File 'lib/rails_throttle/throttle.rb', line 86

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.



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/rails_throttle/throttle.rb', line 71

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