Created
          October 12, 2009 19:56 
        
      - 
      
- 
        Save eddanger/208686 to your computer and use it in GitHub Desktop. 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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