class WebShield::Shield

Attributes

config[R]
id[R]
options[R]
path_matcher[R]
shield_path[R]

Public Class Methods

new(id, shield_path, options, config) click to toggle source
# File lib/web_shield/shield.rb, line 7
def initialize(id, shield_path, options, config)
  @id = id
  @shield_path = shield_path
  @path_matcher = build_path_matcher(shield_path)
  @options = hash_to_symbol_keys(options)
  @config = config
end

Public Instance Methods

dictatorial?() click to toggle source
# File lib/web_shield/shield.rb, line 20
def dictatorial?
  @options[:dictatorial]
end
filter(request) click to toggle source

Returns: :pass, :block, [:response, rack_response], nil

# File lib/web_shield/shield.rb, line 16
def filter(request)
  raise Error, "implement me"
end
shield_name() click to toggle source
# File lib/web_shield/shield.rb, line 33
def shield_name
  self.class.name.split('::', 2).last + "\##{id}"
end
write_log(severity, msg) click to toggle source
# File lib/web_shield/shield.rb, line 24
def write_log(severity, msg)
  case svrt = severity.to_sym
  when :debug, :info, :warn, :error
    config.logger.send(svrt, "#{shield_name} #{msg}")
  else
    raise "Invalid log severity '#{svrt}'"
  end
end

Private Instance Methods

build_path_matcher(path) click to toggle source

Support symbols: :name, (), *

# File lib/web_shield/shield.rb, line 40
def build_path_matcher(path)
  regexp_str = Regexp.escape(path)
  regexp_str.gsub!(/\\\((.+)\\\)/, '(\1)?')
  regexp_str.gsub!(/:[^\/\)]+(\)|\/)?/) {|str| "[^/]+#{$1 ? $1 : nil}" }
  regexp_str.gsub!(/\/$/, '/?')
  regexp_str.gsub!("\\*", '.*')
  Regexp.new("\\A#{regexp_str}\\z", 'i')
end
hash_to_symbol_keys(hash) click to toggle source
# File lib/web_shield/shield.rb, line 49
def hash_to_symbol_keys(hash)
  hash.each_with_object({}) do |kv, result|
    key, val = kv[0].to_sym, kv[1]
    result[key] = val
  end
end