Last active
August 29, 2015 14:20
-
-
Save badosu/c39715413cb1c807b3ea to your computer and use it in GitHub Desktop.
This file contains 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
require 'sanitize' | |
class Roda | |
module RodaPlugins | |
# The h plugin adds an +h+ instance method that will HTML | |
# escape the input and return it. | |
# | |
# The following example will return "<foo>" as the body. | |
# | |
# plugin :h | |
# | |
# route do |r| | |
# h('<foo>') | |
# end | |
module Sanitize | |
OPTS = {}.freeze | |
def self.configure(app, opts=OPTS) | |
app.opts[:sanitizer] = ::Sanitize.new(opts) | |
end | |
module ClassMethods | |
def sanitizer | |
opts[:sanitizer] | |
end | |
end | |
module InstanceMethods | |
def s | |
self.class.sanitizer | |
end | |
end | |
module RequestMethods | |
def sanitize!(fields) | |
s = roda_class.sanitizer | |
fields.each do |field| | |
params[field] = s.fragment(params[field]) | |
end | |
end | |
end | |
end | |
register_plugin(:sanitize, Sanitize) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment