FEATURE: backport a minimal String#scrub

BUGFIX: invalid byte sequence in email would explode all processing
This commit is contained in:
Sam
2013-12-30 14:05:25 +11:00
parent 81eec5ff06
commit 9738c4ff48
3 changed files with 19 additions and 10 deletions

View File

@@ -0,0 +1,16 @@
class String
# A poor man's scrub, Ruby 2.1 has a much better implementation, but this will do
unless method_defined? :scrub
def scrub
str = dup.force_encoding("utf-8")
unless str.valid_encoding?
# work around bust string with a double conversion
str.encode!("utf-16","utf-8",:invalid => :replace)
str.encode!("utf-8","utf-16")
end
str
end
end
end