Last active
September 21, 2015 04:34
-
-
Save bsa7/2c383f8eac887423d442 to your computer and use it in GitHub Desktop.
clean ruby http response from unsupportes symbols
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
def cleaned_content body | |
begin | |
cleaned = body.dup.force_encoding('UTF-8') | |
unless cleaned.valid_encoding? | |
cleaned = body.encode('UTF-8', 'Windows-1251') | |
end | |
content = cleaned | |
rescue EncodingError | |
content = body.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '').encode("utf-8") | |
end | |
content | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment