Skip to content

Instantly share code, notes, and snippets.

@eddanger
Created October 12, 2009 19:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save eddanger/208686 to your computer and use it in GitHub Desktop.
Save eddanger/208686 to your computer and use it in GitHub Desktop.
module Rack
class Censor
WORDS = [ 'shit', 'fuck', 'cock', 'cunt', 'cameltoe', 'mooseknuckle' ].map { |w| Regexp.new(w, Regexp::IGNORECASE) }.freeze
attr_reader :options, :request
def initialize(app, options={})
@app, @options = app, {
:replacement => '*****'
}.merge(options)
end
def call(env)
@request = Rack::Request.new(env)
censor_input
@app.call(env)
end
private
def censor_input
form_input = request.env['rack.request.form_hash']
form_input.each do |name,value|
WORDS.each do |word|
request.env['rack.request.form_hash'][name].gsub!(word, options[:replacement]) if value =~ word
end
end if form_input
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment