Module: ApiHammer::JsonScriptEscapeHelper
- Defined in:
- lib/api_hammer/json_script_escape_helper.rb
Class Method Summary collapse
-
.j ⇒ Object
takes an object which can be used to generate JSON, and formats it so that it may be safely be inserted inside a
takes an object which can be used to generate JSON, and formats it so that it may be safely be inserted inside a tags.
this is aliased as #j, which replaces the j helper built into rails which requires quotes to be added around strings and does not support objects other than strings.
26 27 28 29 30
# File 'lib/api_hammer/json_script_escape_helper.rb', line 26 def json_script_escape(object) encoded = JSON.generate(object, :quirks_mode => true) escaped = encoded.gsub(%r([<>/])) { |x| x.codepoints.map { |p| "\\u%.4x" % p }.join } escaped.respond_to?(:html_safe) ? escaped.html_safe : escaped end
.json_script_escape(object) ⇒ Object
takes an object which can be used to generate JSON, and formats it so that it may be safely be inserted inside a tags.
this is aliased as #j, which replaces the j helper built into rails which requires quotes to be added around strings and does not support objects other than strings.
19 20 21 22 23
# File 'lib/api_hammer/json_script_escape_helper.rb', line 19 def json_script_escape(object) encoded = JSON.generate(object, :quirks_mode => true) escaped = encoded.gsub(%r([<>/])) { |x| x.codepoints.map { |p| "\\u%.4x" % p }.join } escaped.respond_to?(:html_safe) ? escaped.html_safe : escaped end