Skip to content

Instantly share code, notes, and snippets.

@bholzer
Created July 8, 2014 18:51
Show Gist options
  • Save bholzer/25e8b25b6c6a14b941ee to your computer and use it in GitHub Desktop.
Save bholzer/25e8b25b6c6a14b941ee to your computer and use it in GitHub Desktop.
class ApplicationController < ActionController::Base
after_filter :escape_response
def escape_response
if response.content_type == 'application/json'
response.body = escape_hash_or_array(JSON.parse(response.body)).to_json
end
end
#realized that this also creates a new Proc object for every recursive call. Needs to be fixed
def escape_hash_or_array(hash_or_array)
escape_value = lambda do |value|
if value.is_a?(String)
value = ERB::Util.h(value)
elsif [Array, Hash].any?{|klass| value.is_a?(klass) }
value = escape_hash_or_array(value)
end
end
hash_or_array.is_a?(Hash) ? hash_or_array.each_value(&escape_value) : hash_or_array.each(&escape_value)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment