Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

refined character encoding capability with 1.8 rubies and 1.9 rubies.

  • Loading branch information...
commit 764a62abd845e9c794e159c1565b79e9ab9ae1bb 1 parent a7b2d34
@monde authored
View
5 History.txt
@@ -1,3 +1,8 @@
+### 3.9.0 / 2012-10-18 (Jean-Pierre - A French chef)
+
+* 1 minor enhancement
+ * refined character encoding capability with 1.8 rubies and 1.9 rubies.
+
### 3.8.2 / 2012-08-21 (Seth - Pickles The Drummer's brother)
* 2 minor enhancements
View
2  lib/mms2r.rb
@@ -105,7 +105,7 @@ def self.debug(thing, options = {})
end
-%W{ yaml mail fileutils pathname tmpdir yaml digest/sha1 iconv exifr }.each do |g|
+%W{ yaml mail fileutils pathname tmpdir yaml digest/sha1 exifr }.each do |g|
begin
require g
rescue LoadError
View
36 lib/mms2r/media.rb
@@ -265,18 +265,21 @@ def subject
def body
text_file = default_text
- @body = text_file ? IO.readlines(text_file.path).join.strip : ""
- @body.force_encoding("ISO-8859-1") if RUBY_VERSION >= "1.9" && @body.encoding == "ASCII-8BIT"
- begin
- ic = Iconv.new('UTF-8', 'ISO-8859-1' )
+ if RUBY_VERSION < "1.9"
+ @body = text_file ? IO.read(text_file.path).strip : ""
+ require 'iconv'
+ ic = Iconv.new('UTF-8', 'ISO-8859-1')
@body = ic.iconv(@body)
@body << ic.iconv(nil)
ic.close
- rescue Exception => e
+ else
+ @body = text_file ? IO.read(text_file.path, :mode => "rb").strip : ""
+ @body = @body.chars.select{|i| i.valid_encoding?}.join
end
- if @body.blank? && html_file = default_html
+ if @body.blank? &&
+ html_file = default_html
html = Nokogiri::HTML(IO.read(html_file.path))
@body = (html.xpath("//head/title").map(&:text) + html.xpath("//body/*").map(&:text)).join(" ")
end
@@ -402,7 +405,6 @@ def process_media(part)
if part.part_type? =~ /^text\// ||
part.part_type? == 'application/smil'
type, content = transform_text_part(part)
- mode = 'wb'
else
if part.part_type? == 'application/octet-stream'
type = type_from_filename(filename?(part))
@@ -410,12 +412,11 @@ def process_media(part)
type = part.part_type?
end
content = part.body.decoded
- mode = 'wb' # open with binary bit for Windows for non text
end
return type, nil if content.nil? || content.empty?
log("#{self.class} writing file #{file}", :info)
- File.open(file, mode){ |f| f.write(content) }
+ File.open(file, 'wb'){ |f| f.write(content) }
return type, file
end
@@ -443,26 +444,25 @@ def process_html_part(part)
# See the transform section in the discussion of the built-in
# configuration.
- def transform_text(type, text, original_encoding = 'ISO-8859-1')
+ def transform_text(type, text)
return type, text if !config['transform'] || !(transforms = config['transform'][type])
- begin
- ic = Iconv.new('UTF-8', original_encoding )
- utf_t = ic.iconv(text)
- utf_t << ic.iconv(nil)
+ if RUBY_VERSION < "1.9"
+ require 'iconv'
+ ic = Iconv.new('UTF-8', 'ISO-8859-1')
+ text = ic.iconv(text)
+ text << ic.iconv(nil)
ic.close
- rescue Exception => e
- utf_t = text
end
transforms.each do |transform|
next unless transform.size == 2
p = transform.first
r = transform.last
- utf_t = utf_t.gsub(p, r) rescue utf_t
+ text = text.gsub(p, r) rescue text
end
- return type, utf_t
+ return type, text
end
##
View
4 lib/mms2r/version.rb
@@ -6,11 +6,11 @@ def self.major
end
def self.minor
- 8
+ 9
end
def self.patch
- 2
+ 0
end
def self.pre
View
2  test/fixtures/sprint-ajax-response-success.json
@@ -1 +1 @@
-{"totalMediaItems":2,"shareType":"normal","nomediaItem":"false","isOnlyVideo":null,"creationDate":"May 31, 2012","from":"(513)545-0000","offset":null,"externalMessageId":"XXXXXXXXXXXXXXXXXX","Results":[{"elementID":"0","hasVoiceCaption":false,"URL":{"elementID":"0","indexCount":0,"audio":null,"thumb":"\/retailers\/PCSNEXTEL\/ui-refresh\/images\/background\/slide_no_media_90x90.gif","annotationVoiceID":null,"image":"\/retailers\/PCSNEXTEL\/ui-refresh\/images\/background\/slide_no_media_90x90.gif","video":null},"description":"First text content. ","mediaItemNum":0,"isDRMProtected":false,"externalMessageId":"XXXXXXXXXXXXXXXXXX","mediaType":"TEXT","restOperation":"false","folderFullName":"\/RECIPIENT"},{"elementID":"3","hasVoiceCaption":false,"URL":{"elementID":"3","indexCount":1,"audio":"","thumb":"http:\/\/pictures.sprintpcs.com:80\/mmps\/048_0736849c3f1a9d27_1\/3.jpg?partExt=.jpg&&&outquality=90&ext=.jpg&&size=40,40&squareoutput=255,255,255&aspectcrop=0.5,0.5,1.0,1.0,1.0","annotationVoiceID":null,"image":"http:\/\/pictures.sprintpcs.com:80\/mmps\/048_0736849c3f1a9d27_1\/3.jpg?partExt=.jpg&&&outquality=90&ext=.jpg","video":""},"description":"Second text content. ","mediaItemNum":1,"isDRMProtected":false,"externalMessageId":"XXXXXXXXXXXXXXXXXX","mediaType":"IMAGE","restOperation":"false","folderFullName":"\/RECIPIENT"}],"invite":null,"tmemo":null,"toAddress":"textinsubmit@gmail.com","mediaIndex":0,"subject":"New Message","isDRMProtected":false,"expirationDate":"Expires in 57 Days","voiceURL":null,"guest":"true","folderFullName":"\/RECIPIENT"}
+{"totalMediaItems":2,"shareType":"normal","nomediaItem":"false","isOnlyVideo":null,"creationDate":"May 31, 2012","from":"(513)545-0000","offset":null,"externalMessageId":"XXXXXXXXXXXXXXXXXX","Results":[{"elementID":"0","hasVoiceCaption":false,"URL":{"elementID":"0","indexCount":0,"audio":null,"thumb":"\/retailers\/PCSNEXTEL\/ui-refresh\/images\/background\/slide_no_media_90x90.gif","annotationVoiceID":null,"image":"\/retailers\/PCSNEXTEL\/ui-refresh\/images\/background\/slide_no_media_90x90.gif","video":null},"description":"First text content.","mediaItemNum":0,"isDRMProtected":false,"externalMessageId":"XXXXXXXXXXXXXXXXXX","mediaType":"TEXT","restOperation":"false","folderFullName":"\/RECIPIENT"},{"elementID":"3","hasVoiceCaption":false,"URL":{"elementID":"3","indexCount":1,"audio":"","thumb":"http:\/\/pictures.sprintpcs.com:80\/mmps\/048_0736849c3f1a9d27_1\/3.jpg?partExt=.jpg&&&outquality=90&ext=.jpg&&size=40,40&squareoutput=255,255,255&aspectcrop=0.5,0.5,1.0,1.0,1.0","annotationVoiceID":null,"image":"http:\/\/pictures.sprintpcs.com:80\/mmps\/048_0736849c3f1a9d27_1\/3.jpg?partExt=.jpg&&&outquality=90&ext=.jpg","video":""},"description":"Second text content. ","mediaItemNum":1,"isDRMProtected":false,"externalMessageId":"XXXXXXXXXXXXXXXXXX","mediaType":"IMAGE","restOperation":"false","folderFullName":"\/RECIPIENT"}],"invite":null,"tmemo":null,"toAddress":"textinsubmit@gmail.com","mediaIndex":0,"subject":"New Message","isDRMProtected":false,"expirationDate":"Expires in 57 Days","voiceURL":null,"guest":"true","folderFullName":"\/RECIPIENT"}
View
16 test/test_invalid_byte_seq_outlook.rb
@@ -6,21 +6,9 @@ class TestInvalidByteSeqOutlook < Test::Unit::TestCase
def test_bad_outlook
mail = mail('invalid-byte-seq-outlook.mail')
mms = MMS2R::Media.new(mail)
- body = mms.body
-=begin
- assert_equal "+919812345678", mms.number
- assert_equal "1nbox.net", mms.carrier
- assert_equal 2, mms.media.size
-
- assert_not_nil mms.media['text/plain']
- assert_equal 1, mms.media['text/plain'].size
- assert_equal "testing123456789012", open(mms.media['text/plain'].first).read
-
- assert_not_nil mms.media['image/jpeg']
- assert_equal 1, mms.media['text/plain'].size
- assert_match(/@003\.jpg$/, mms.media['image/jpeg'].first)
-=end
+ assert_equal "RE: Issue 14794:Don M. says.. Aaron - I completed RNS Test Question 3, which was done as a 'Cargo Control Number' query. H", mms.subject
+ assert_match /Don Doe said less than a minute ago/im, mms.body
mms.purge
end
View
2  test/test_messaging_sprintpcs_com.rb
@@ -14,7 +14,7 @@ def test_simple_text
file = mms.media['text/plain'][0]
assert_not_nil file
assert File::exist?(file), "file #{file} does not exist"
- text = IO.readlines("#{file}").join
+ text = IO.read("#{file}")
assert_match(/hello world/, text)
mms.purge
end
View
16 test/test_mms2r_media.rb
@@ -233,20 +233,16 @@ def test_transform_text_to_utf8
file = mms.media['text/plain'][0]
assert_not_nil file
assert_equal true, File::exist?(file)
- text_lines = IO.readlines("#{file}")
- text = text_lines.join
-
- # ASCII-8BIT -> D'ici un mois G\xE9orgie
- # UTF-8 -> D'ici un mois Géorgie
-
if RUBY_VERSION < "1.9"
- assert_equal("sample email message Fwd: sub D'ici un mois Géorgie", mms.subject)
- assert_equal("D'ici un mois Géorgie body", text_lines.first.strip)
+ text = IO.read("#{file}")
else
- assert_equal(Iconv.new('UTF-8', 'ISO-8859-1').iconv("sample email message Fwd: sub D'ici un mois Géorgie"), mms.subject)
- assert_equal(Iconv.new('UTF-8', 'ISO-8859-1').iconv("D'ici un mois G\xE9orgie body"), text_lines.first.strip)
+ text = IO.read("#{file}", :mode => "rb")
end
+ # ASCII-8BIT -> D'ici un mois G\xE9orgie
+ # UTF-8 -> D'ici un mois Géorgie
+
+ assert_equal("sample email message Fwd: sub D'ici un mois Géorgie", mms.subject)
end
def test_subject
View
4 test/test_mms_att_net.rb
@@ -103,7 +103,7 @@ def test_image_from_blackberry
assert_equal "example.com", mms.carrier
assert_not_nil mms.media['text/plain']
- assert_equal "Hello world", IO.readlines(mms.media['text/plain'].first).join.strip
+ assert_equal "Hello world", IO.read(mms.media['text/plain'].first).strip
assert_not_nil mms.media['image/jpeg'].first
assert_match(/\/BC-WAKE\.jpg$/, mms.media['image/jpeg'].first)
@@ -118,7 +118,7 @@ def test_image_from_blackberry2
assert_equal 2, mms.media.size
assert_not_nil mms.media['text/plain']
- assert_match(/^Testing memorymail from my blackberry and at&t.$/, IO.readlines(mms.media['text/plain'].first).join.strip)
+ assert_match(/^Testing memorymail from my blackberry and at&t.$/, IO.read(mms.media['text/plain'].first).strip)
assert_not_nil mms.media['image/jpeg'].first
assert_match(/IMG00367.jpg/, mms.media['image/jpeg'].first)
View
2  test/test_mms_mycricket_com.rb
@@ -48,7 +48,7 @@ def test_image_and_text
file = mms.media['text/plain'].first
assert_equal true, File::exist?(file), "file #{file} does not exist"
- text = IO.readlines("#{file}").join
+ text = IO.read("#{file}")
assert_match(/Hello World/, text)
assert_match(/02-14-08_2114.jpg$/, mms.media['image/jpeg'].first)
View
2  test/test_mms_uscc_net.rb
@@ -24,7 +24,7 @@ def test_mms_uscc_net
file = mms.media['text/plain'][0]
assert_not_nil file
assert_equal true, File::exist?(file)
- text = IO.readlines("#{file}").join
+ text = IO.read("#{file}")
assert_match(/This is what i do at work most the day/, text)
mms.purge
View
4 test/test_orangemms_net.rb
@@ -58,7 +58,7 @@ def test_orange_france_processed_content
assert_equal 1, mms.media['text/plain'].size
file = mms.media['text/plain'].first
assert File::exist?(file), "file #{file} does not exist"
- text = IO.readlines("#{file}").join
+ text = IO.read("#{file}")
assert_match(/Test ma poule/, text)
# image
@@ -100,7 +100,7 @@ def test_orange_poland_content
file = mms.media['text/plain'][0]
assert_not_nil file
assert File::exist?(file), "file #{file} does not exist"
- text = IO.readlines("#{file}").join
+ text = IO.read("#{file}")
assert_match(/pozdro600/, text)
mms.purge
end
View
2  test/test_pm_sprint_com.rb
@@ -242,7 +242,7 @@ def test_message_is_missing_in_mail
assert_equal 2, mms.media['text/plain'].size
# test that the message was extracted from the ajax response
- message = IO.readlines(mms.media['text/plain'].first).join("")
+ message = IO.read(mms.media['text/plain'].first)
assert_equal "First text content.", message
# test that the &nbsp; was removed ()
View
2  test/test_pxt_vodafone_net_nz.rb
@@ -20,7 +20,7 @@ def test_pxt_text_returns_text_plain
file = @mms.media['text/plain'][0]
assert_not_nil(file)
assert(File::exist?(file), "file #{file} does not exist")
- text = IO.readlines("#{file}").join
+ text = IO.read("#{file}")
assert_match(/Kia ora ano Luke/, text)
assert_match(/Kia ora ano Luke/, @mms.body)
View
2  test/test_tmomail_net.rb
@@ -37,7 +37,7 @@ def test_message_with_body_text
file = mms.media['text/plain'][0]
assert_not_nil file
assert File::exist?(file), "file #{file} does not exist"
- text = IO.readlines("#{file}").join
+ text = IO.read("#{file}")
assert_equal "Lillies", text.strip
mms.purge
View
2  test/test_unicel_com.rb
@@ -19,7 +19,7 @@ def test_subject_number_image_unicel
assert_equal 337, File.size(image)
file = mms.media['text/plain'][0]
- text = IO.readlines("#{file}").join
+ text = IO.read("#{file}")
assert_match(/2068675309/, text)
mms.purge
View
8 test/test_vzwpix_com.rb
@@ -79,7 +79,7 @@ def test_simple_text
assert_not_nil file
assert_equal true, File::exist?(file)
- text = IO.readlines("#{file}").join
+ text = IO.read("#{file}")
assert_match(/hello world/, text)
mms.purge
end
@@ -102,7 +102,7 @@ def test_image_with_body_text
file = mms.media['text/plain'][0]
assert_not_nil file
assert_equal true, File::exist?(file)
- text = IO.readlines("#{file}").join
+ text = IO.read("#{file}")
assert_no_match(/Regexp.escape(@ad_old)/, text)
assert_no_match(/Regexp.escape@greet/, text)
assert_equal "? Weird", text
@@ -139,7 +139,7 @@ def test_simple_text_vtext
file = mms.media['text/plain'][0]
assert_not_nil file
assert_equal true, File::exist?(file)
- text = IO.readlines("#{file}").join
+ text = IO.read("#{file}")
assert_match(/hello world/, text)
mms.purge
end
@@ -151,7 +151,7 @@ def test_image_from_blackberry
assert_equal 'yahoo.com', mms.carrier
assert_not_nil mms.media['text/plain']
- assert_equal "Wonderful picture!", IO.readlines(mms.media['text/plain'].first).join.strip
+ assert_equal "Wonderful picture!", IO.read(mms.media['text/plain'].first).strip
assert_not_nil mms.media['image/jpeg'].first
assert_match(/\/IMG00016\.jpg$/, mms.media['image/jpeg'].first)
Please sign in to comment.
Something went wrong with that request. Please try again.