Module: ConcernsOnRails::Controllers::Throttleable::ClassMethods

Defined in:
lib/concerns_on_rails/controllers/throttleable.rb

Instance Method Summary collapse

Instance Method Details

#throttle_by(limit:, period:, by: nil, only: nil, except: nil, name: nil) ⇒ Object

Declare a rate-limit rule. ‘limit` requests per `period` (a Duration or seconds), bucketed by `by:` (a callable, default per-IP). `only:`/ `except:` scope it to a subset of actions (mutually exclusive). `name:` disambiguates the counter key when several rules share a discriminator.



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/concerns_on_rails/controllers/throttleable.rb', line 44

def throttle_by(limit:, period:, by: nil, only: nil, except: nil, name: nil)
  validate_throttle!(limit: limit, period: period, by: by, only: only, except: except)

  rule = {
    limit: limit,
    period: period.to_i,
    by: by || DEFAULT_DISCRIMINATOR,
    only: only && Array(only).map(&:to_s),
    except: except && Array(except).map(&:to_s),
    name: (name || "rule#{throttleable_rules.size}").to_s
  }
  self.throttleable_rules = throttleable_rules + [rule]
end