Skip to content

Instantly share code, notes, and snippets.

@julik
Created March 2, 2010 09:20
Show Gist options
  • Save julik/319375 to your computer and use it in GitHub Desktop.
Save julik/319375 to your computer and use it in GitHub Desktop.
class UnicodeEnforcer
def new(app)
@app = app
end
def call(env)
return @app.call(env) unless env["Content-Type"] =~ "utf-8"
req = Rack::Request.new(env)
req.params = force_encoding(req.params)
@app.call(env)
end
def force_encoding(on)
case on
when String
on.force_encoding(Encoding::UTF8)
when Array
on.map(&method(:force_encoding))
when Hash
on.inject({}) do | n, (k, v) |
n.merge(force_encoding(k) => force_encoding(v))
end
else
on
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment