class Rack::Defense::Config

Constants

BANNED_RESPONSE
THROTTLED_RESPONSE

Attributes

ban_callbacks[R]
banned_response[RW]
bans[R]
throttle_callbacks[R]
throttled_response[RW]
throttles[R]

Public Class Methods

new() click to toggle source
# File lib/rack/defense.rb, line 20
def initialize
  self.banned_response = BANNED_RESPONSE
  self.throttled_response = THROTTLED_RESPONSE
  @throttles, @bans = {}, {}
  @ban_callbacks, @throttle_callbacks = [], []
 end

Public Instance Methods

after_ban(&block) click to toggle source
# File lib/rack/defense.rb, line 41
def after_ban(&block)
  ban_callbacks << block
end
after_throttle(&block) click to toggle source
# File lib/rack/defense.rb, line 45
def after_throttle(&block)
  throttle_callbacks << block
end
ban(rule_name, &block) click to toggle source
# File lib/rack/defense.rb, line 36
def ban(rule_name, &block)
  raise ArgumentError, 'rule name should not be nil' unless rule_name
  bans[rule_name] = block
end
store() click to toggle source
# File lib/rack/defense.rb, line 58
def store
  # Redis.new uses REDIS_URL environment variable by default as URL.
  # See https://github.com/redis/redis-rb
  @store ||= SimpleDelegator.new(Redis.new)
end
store=(value) click to toggle source
# File lib/rack/defense.rb, line 49
def store=(value)
  value = Redis.new(url: value) if value.is_a?(String)
  if @store
    @store.__setobj__(value)
  else
    @store = SimpleDelegator.new(value)
  end
end
throttle(rule_name, max_requests, period, &block) click to toggle source
# File lib/rack/defense.rb, line 27
def throttle(rule_name, max_requests, period, &block)
  raise ArgumentError, 'rule name should not be nil' unless rule_name
  counter = ThrottleCounter.new(rule_name, max_requests, period, store)
  throttles[rule_name] = lambda do |req|
    key = block.call(req)
    key if key && counter.throttle?(key)
  end
end