Skip to content

Instantly share code, notes, and snippets.

@hkraji
Created March 19, 2014 11:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hkraji/9639857 to your computer and use it in GitHub Desktop.
Save hkraji/9639857 to your computer and use it in GitHub Desktop.
Normalize to UTF8 (remove invalid utf8 characters)
#
# Takes possible invalid UTF-8 output and ignores invalid bytes
#
# http://po-ru.com/diary/fixing-invalid-utf-8-in-ruby-revisited/
def self.normalize_to_utf8_output(response)
if response[:data]
ic = Iconv.new('UTF-8//IGNORE', 'UTF-8')
output = response[:data][:output]||''
valid_output = ic.iconv(output + ' ')[0..-2]
response[:data][:output] = valid_output
else
Log.warn "Skipping UTF-8 normalization since provided output does not have :data element. Response #{response}"
end
response
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment