class ParamSanitizer::RequestSanitizer
Attributes
strategized_routes[R]
Public Class Methods
new(app, *args)
click to toggle source
# File lib/param_sanitizer/request_sanitizer.rb, line 7 def initialize(app, *args) @app = app @strategized_routes = args.last.is_a?(Hash) ? args.last : {} emit_warning if @strategized_routes.empty? end
Public Instance Methods
build(strategy)
click to toggle source
# File lib/param_sanitizer/request_sanitizer.rb, line 41 def build(strategy) if strategy.respond_to?(:call) then strategy elsif strategy.respond_to?(:new) then strategy.new elsif strategy.is_a?(Symbol) then ParamSanitizer::Strategies.const_get("#{strategy}Strategy").new else raise ArgumentError.new "#{strategy.to_s} does not support 'call'!" end end
call(env)
click to toggle source
# File lib/param_sanitizer/request_sanitizer.rb, line 13 def call(env) request = Rack::Request.new(env) request = execute_strategies(request) if has_strategy?(request.path) env["QUERY_STRING"] = encode_to_query_string(request.params) @app.call(env) end
emit_warning()
click to toggle source
# File lib/param_sanitizer/request_sanitizer.rb, line 33 def emit_warning puts "ParamSanitizer::RequestSanitizer initialized without sanitization strategies. Middleware is now a no-op" end
encode_to_query_string(params)
click to toggle source
# File lib/param_sanitizer/request_sanitizer.rb, line 37 def encode_to_query_string(params) URI.encode(params.map{|k,v| "#{k}=#{v}"}.join('&')) end
execute_strategies(request)
click to toggle source
# File lib/param_sanitizer/request_sanitizer.rb, line 20 def execute_strategies(request) strategies = @strategized_routes[request.path] strategies.each { |strategy| instance = build(strategy) instance.call(request) if instance.respond_to? :call } request end
has_strategy?(route)
click to toggle source
# File lib/param_sanitizer/request_sanitizer.rb, line 29 def has_strategy?(route) @strategized_routes.has_key?(route) end