diff options
author | Rowan Crawford <wombleton@gmail.com> | 2014-02-10 14:10:15 +1300 |
---|---|---|
committer | Rowan Crawford <wombleton@gmail.com> | 2014-02-10 14:10:15 +1300 |
commit | efea8f2aa0ac36e83b15f3a56f9f243467f8225b (patch) | |
tree | e4a4a23e3de94e38b888650faf6f9fdc62c4901e | |
parent | 3ed969e88f60e6a9123a09544e553e48a41dfa12 (diff) |
encode does no work if it thinks it's already utf-8 (even if invalid)
Also use duck typing for whether we should use encode
-rw-r--r-- | app/models/incoming_message.rb | 16 |
1 files changed, 8 insertions, 8 deletions
diff --git a/app/models/incoming_message.rb b/app/models/incoming_message.rb index bcf0b6ec9..32123ae49 100644 --- a/app/models/incoming_message.rb +++ b/app/models/incoming_message.rb @@ -35,7 +35,7 @@ require 'htmlentities' require 'rexml/document' require 'zip/zip' -require 'iconv' unless RUBY_VERSION >= '1.9' +require 'iconv' unless String.method_defined?(:encode) class IncomingMessage < ActiveRecord::Base extend MessageProminence @@ -294,7 +294,7 @@ class IncomingMessage < ActiveRecord::Base emails = ascii_chars.scan(MySociety::Validate.email_find_regexp) # Convert back to UCS-2, making a mask at the same time - if RUBY_VERSION >= '1.9' + if String.method_defined?(:encode) emails.map! do |email| # We want the ASCII representation of UCS-2 [email[0].encode('UTF-16LE').force_encoding('US-ASCII'), @@ -516,7 +516,7 @@ class IncomingMessage < ActiveRecord::Base # should instead tell elinks to respect the source # charset use_charset = "utf-8" - if RUBY_VERSION.to_f >= 1.9 + if String.method_defined?(:encode) begin text.encode('utf-8') rescue Encoding::UndefinedConversionError, Encoding::InvalidByteSequenceError @@ -547,7 +547,7 @@ class IncomingMessage < ActiveRecord::Base end def _sanitize_text(text) - if RUBY_VERSION.to_f >= 1.9 + if String.method_defined?(:encode) begin # Test if it's good UTF-8 text.encode('utf-8') @@ -803,10 +803,10 @@ class IncomingMessage < ActiveRecord::Base end # Remove any bad characters - if RUBY_VERSION >= '1.9' - text.encode("utf-8", :invalid => :replace, - :undef => :replace, - :replace => "") + if String.method_defined?(:encode) + # handle "problematic" encoding + text.encode!('UTF-16', 'UTF-8', :invalid => :replace, :undef => :replace, :replace => '') + text.encode('UTF-8', 'UTF-16') else Iconv.conv('utf-8//IGNORE', 'utf-8', text) end |