Moved to ruby 1.9 encoding handling.

This commit is contained in:
Martin Edenhofer 2012-12-05 01:28:04 +01:00
parent 97c330ab63
commit ccbf586ec7
2 changed files with 54 additions and 6 deletions

View file

@ -1,17 +1,41 @@
# encoding: utf-8 # encoding: utf-8
require 'mail' require 'mail'
require 'iconv' #require 'iconv'
class Channel::EmailParser class Channel::EmailParser
def conv (charset, string) def conv (charset, string)
# if no charset is given, use LATIN1 as default
if !charset || charset == 'US-ASCII' || charset == 'ASCII-8BIT' if !charset || charset == 'US-ASCII' || charset == 'ASCII-8BIT'
charset = 'LATIN1' charset = 'LATIN1'
end end
return string if charset.downcase == 'utf8' || charset.downcase == 'utf-8'
# return if string is false
return string if !string
# validate already existing utf8 strings
if charset.downcase == 'utf8' || charset.downcase == 'utf-8'
begin
# return if encoding is valid
utf8 = string.force_encoding('UTF-8')
return utf8 if utf8.valid_encoding?
# try to encode from Windows-1252 to utf8
string.encode!( 'UTF-8', 'Windows-1252' )
rescue EncodingError => e
puts "Bad encoding: #{new_value.inspect}"
string.encode!( 'UTF-8', invalid: :replace, undef: :replace, replace: '?' )
end
return string
end
# puts '-------' + charset # puts '-------' + charset
# puts string # puts string
# string.encode("UTF-8")
Iconv.conv( 'UTF8', charset, string ) # convert string
string.encode!( 'UTF-8', charset.upcase )
# Iconv.conv( 'UTF8', charset, string )
end end
=begin =begin
@ -73,7 +97,7 @@ class Channel::EmailParser
# set all headers # set all headers
mail.header.fields.each { |field| mail.header.fields.each { |field|
data[field.name.downcase.to_sym] = field.to_s data[field.name.downcase.to_sym] = conv( 'utf8', field.to_s )
} }
# set extra headers # set extra headers

View file

@ -34,6 +34,7 @@ Some Text',
:result => { :result => {
0 => { 0 => {
:ticket_priority => '3 high', :ticket_priority => '3 high',
:title => 'some subject',
}, },
1 => { 1 => {
:ticket_article_sender => 'System', :ticket_article_sender => 'System',
@ -41,6 +42,25 @@ Some Text',
}, },
}, },
}, },
{
:data => "From: me@example.com
To: customer@example.com
Subject: äöü some subject
Some Textäöü".encode("ISO-8859-1"),
:success => true,
:result => {
0 => {
:ticket_priority => '2 normal',
:title => 'äöü some subject',
},
1 => {
:body => 'Some Textäöü',
:ticket_article_sender => 'Customer',
:ticket_article_type => 'email',
},
},
},
] ]
files.each { |file| files.each { |file|
@ -52,7 +72,11 @@ Some Text',
[ 0, 1, 2 ].each { |level| [ 0, 1, 2 ].each { |level|
if file[:result][level] if file[:result][level]
file[:result][level].each { |key, value| file[:result][level].each { |key, value|
if result[level].send(key).respond_to?('name')
assert_equal( result[level].send(key).name, value.to_s) assert_equal( result[level].send(key).name, value.to_s)
else
assert_equal( result[level].send(key), value.to_s)
end
} }
end end
} }