Created
March 19, 2014 11:32
-
-
Save hkraji/9639857 to your computer and use it in GitHub Desktop.
Normalize to UTF8 (remove invalid utf8 characters)
This file contains hidden or 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
# | |
# 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