Created
September 17, 2015 23:34
-
-
Save pauljm/8c73a6581717acdf3eea to your computer and use it in GitHub Desktop.
Strategy for handling non-ASCII with Mail
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
# Escape non-ASCII characters and forward slashes as unicode escape sequences, | |
# but with forward slashes instead of backslashes, since backslashes already have meaning | |
def asciiify(str) | |
str.unpack('U*').map { |i| | |
(i == 47 || i == 92 || i > 127) ? "/u" + i.to_s(16).rjust(4, '0') : i.chr | |
}.join | |
end | |
# Decode "forward slashed" unicode escape sequences | |
def deasciiify(str) | |
str.gsub(/\/u([\da-fA-F]{4})/) { |m| | |
[$1].pack("H*").unpack("n*").pack("U*") | |
} | |
end | |
raw = "Abé Allen <[email protected]>, 粗楷体简 <[email protected]>, [email protected], //// <[email protected]>" | |
list = Mail::AddressList.new(asciiify(raw)) | |
puts deasciiify(list.addresses[1].format) # 粗楷体简 <[email protected]> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment