Class: WebShield::ThrottleShield

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

Constant Summary

Constants inherited from Shield

Shield::OPTION_KEYS

Instance Attribute Summary

Attributes inherited from Shield

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

Instance Method Summary collapse

Methods inherited from Shield

#dictatorial?, #initialize

Constructor Details

This class inherits a constructor from WebShield::Shield

Instance Method Details

#filter(request) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/web_shield/throttle_shield.rb', line 6

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)
  store_keys = [id.to_s, user.to_s]
  if options[:path_sensitive]
    route = [request.request_method, req_path]
  else
    route = (options[:method] ? [options[:method].to_s.upcase, shield_path] : [shield_path])
  end
  store_keys << Digest::MD5.hexdigest(route.join('-'))

  if @config.store.incr(store_keys.join('-'), options[:period]) <= options[:limit]
    :pass
  else
    config.logger.info("[#{id}] Block '#{user}' #{request.request_method} #{req_path}")
    :block
  end
end