Skip to content

Instantly share code, notes, and snippets.

@bsa7
Last active September 21, 2015 04:34
Show Gist options
  • Save bsa7/2c383f8eac887423d442 to your computer and use it in GitHub Desktop.
Save bsa7/2c383f8eac887423d442 to your computer and use it in GitHub Desktop.
clean ruby http response from unsupportes symbols
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