class WebShield::ThrottleShield

Constants

OPTION_KEYS

Public Class Methods

new(id, shield_path, options, config) click to toggle source

Params:

path:
options:
  period: required
  limit: required
  method: optional
  path_sensitive: optional, defualt false
Calls superclass method WebShield::Shield::new
# File lib/web_shield/throttle_shield.rb, line 14
def initialize(id, shield_path, options, config)
  super

  check_options(@options)
end

Public Instance Methods

filter(request) click to toggle source
# File lib/web_shield/throttle_shield.rb, line 20
def filter(request)
  req_path = request.path
  return unless path_matcher.match(req_path)
  return :block if options[:limit] <= 0
  return if options[:method] && options[:method].to_s.upcase != request.request_method

  user = config.user_parser.call(request)

  if @config.store.incr(get_store_key(request, user), options[:period]) <= options[:limit]
    write_log(:debug, "Pass '#{user}' #{request.request_method} #{req_path}")
    :pass
  else
    write_log(:info, "Block '#{user}' #{request.request_method} #{req_path}")
    :block
  end
end

Private Instance Methods

check_options(options) click to toggle source
# File lib/web_shield/throttle_shield.rb, line 51
def check_options(options)
  options.each do |key, val|
    raise Error, "Invalid shield option '#{key}'" unless OPTION_KEYS.include?(key)
  end
end
get_store_key(request, user) click to toggle source
# File lib/web_shield/throttle_shield.rb, line 40
def get_store_key(request, user)
  keys = ['web_shield', id.to_s, user.to_s]
  route = if options[:path_sensitive]
    [request.request_method, request.path]
  else
    (options[:method] ? [options[:method].to_s.upcase, shield_path] : [shield_path])
  end
  keys << Digest::MD5.hexdigest(route.join('-'))
  keys.join('/')
end