class RateLimiter::Throttle

Limits the rate at which new records of an ActiveRecord model can be saved. Checks to see if RateLimiter is enabled and, if all conditions are satisfied and if any other records have been created within the time interval for rate limiting.

Constants

DEFAULT_INTERVAL
DEFAULT_ON_FIELD
DEFAULT_TIMESTAMP_FIELD

Public Class Methods

new(record, options = {}) click to toggle source
# File lib/rate_limiter/throttle.rb, line 13
def initialize(record, options = {})
  @record = record
  @options = options
end

Public Instance Methods

exceeded?() click to toggle source

Has the rate limit been exceeded?

# File lib/rate_limiter/throttle.rb, line 19
def exceeded?
  enabled? && rate_limit? && others_for_rate_limiting.present?
end

Private Instance Methods

enabled?() click to toggle source

Is RateLimiter enabled for this particular record?

# File lib/rate_limiter/throttle.rb, line 26
def enabled?
  RateLimiter.enabled? &&
    RateLimiter.request.enabled? &&
    RateLimiter.request.enabled_for_model?(@record.class)
end
if_condition() click to toggle source

Proc that if present must return true for RateLimiter to be enabled for this particular record.

# File lib/rate_limiter/throttle.rb, line 34
def if_condition
  @options[:if_condition]
end
interval() click to toggle source

The time interval (in seconds)

# File lib/rate_limiter/throttle.rb, line 39
def interval
  @options[:interval] || DEFAULT_INTERVAL
end
interval_query_params() click to toggle source

Arel query for finding records within the interval.

# File lib/rate_limiter/throttle.rb, line 44
def interval_query_params
  @record.class.arel_table[timestamp_field].gteq(Time.current - interval)
end
on() click to toggle source

An Array of model attribute names to match against.

# File lib/rate_limiter/throttle.rb, line 49
def on
  Array(@options[:on] || DEFAULT_ON_FIELD)
end
on_query_params() click to toggle source

Arel query for finding records that match the on attributes.

# File lib/rate_limiter/throttle.rb, line 54
def on_query_params
  on.map { |attr| @record.class.arel_table[attr].eq(@record.send(attr)) }.inject(&:or)
end
others_for_rate_limiting() click to toggle source

Other records that fall within the time interval.

# File lib/rate_limiter/throttle.rb, line 59
def others_for_rate_limiting
  @record.class.where(on_query_params).where(interval_query_params)
end
rate_limit?() click to toggle source

Are all of the conditions for rate limiting met for this record?

# File lib/rate_limiter/throttle.rb, line 64
def rate_limit?
  (if_condition.blank? || if_condition.call(@record)) && !unless_condition.try(:call, @record)
end
timestamp_field() click to toggle source

Timestamp field to test the interval against.

# File lib/rate_limiter/throttle.rb, line 69
def timestamp_field
  @options[:timestamp_field] || DEFAULT_TIMESTAMP_FIELD
end
unless_condition() click to toggle source

Proc that if present must return false for RateLimiter to be enabled for this particular record.

# File lib/rate_limiter/throttle.rb, line 75
def unless_condition
  @options[:unless_condition]
end