Skip to content

Instantly share code, notes, and snippets.

@badosu
Last active August 29, 2015 14:20
Show Gist options
  • Save badosu/c39715413cb1c807b3ea to your computer and use it in GitHub Desktop.
Save badosu/c39715413cb1c807b3ea to your computer and use it in GitHub Desktop.
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