Class: WebShield::ThrottleShield

Inherits:
Shield
  • Object
show all
Defined in:
lib/web_shield/throttle_shield.rb

Constant Summary collapse

OPTION_KEYS =
[:period, :limit, :method, :path_sensitive, :dictatorial]

Instance Attribute Summary

Attributes inherited from Shield

#config, #id, #options, #path_matcher, #shield_path

Instance Method Summary collapse

Methods inherited from Shield

#dictatorial?, #shield_name, #write_log

Constructor Details

#initialize(id, shield_path, options, config) ⇒ ThrottleShield

Params:

path:
options:
  period: required
  limit: required
  method: optional
  path_sensitive: optional, defualt false


14
15
16
17
18
# File 'lib/web_shield/throttle_shield.rb', line 14

def initialize(id, shield_path, options, config)
  super

  check_options(@options)
end

Instance Method Details

#filter(request) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 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