Skip to content
This repository
Browse code

Added sorting of parts, default is text/plain, then text/enriched and…

… text/html. Access through Body#set_sort_order and Body#sort_parts! (called from Body#encode automatically)
  • Loading branch information...
commit fbafc494a59734b0704760b6a8e4164df40aedca 1 parent fc65df3
Mikel Lindsaar authored
5 CHANGELOG.rdoc
Source Rendered
... ... @@ -1,3 +1,8 @@
  1 +== Mon 28 Dec 2009 01:21:52 UTC Mikel Lindsaar <raasdnil@gmail.com>
  2 +
  3 +* Added sorting of parts, default is text/plain, then text/enriched and text/html. Access through Body#set_sort_order and Body#sort_parts! (called from Body#encode automatically)
  4 +* Version bump to 1.4.2
  5 +
1 6 == Sun Dec 27 10:38:24 UTC 2009 Mikel Lindsaar <raasdnil@gmail.com>
2 7
3 8 * Updating treetop and mail to initialize uninitialized instance variables to
2  Rakefile
@@ -12,7 +12,7 @@ require 'bundler'
12 12
13 13 spec = Gem::Specification.new do |s|
14 14 s.name = "mail"
15   - s.version = "1.4.1"
  15 + s.version = "1.4.2"
16 16 s.author = "Mike Lindsaar"
17 17 s.email = "raasdnil@gmail.com"
18 18 s.homepage = "http://github.com/mikel/mail"
23 lib/mail/body.rb
@@ -30,6 +30,7 @@ def initialize(string = '')
30 30 @boundary = nil
31 31 @preamble = nil
32 32 @epilogue = nil
  33 + @part_sort_order = [ "text/plain", "text/enriched", "text/html" ]
33 34 @parts = []
34 35 if string.blank?
35 36 @raw_source = ''
@@ -89,7 +90,26 @@ def =~(regexp)
89 90 def match(regexp)
90 91 self.decoded.match(regexp)
91 92 end
92   -
  93 +
  94 + # Allows you to set the sort order of the parts, overriding the default sort order.
  95 + # Defaults to 'text/plain', then 'text/enriched', then 'text/html' with any other content
  96 + # type coming after.
  97 + def set_sort_order(order)
  98 + @part_sort_order = order
  99 + end
  100 +
  101 + # Allows you to sort the parts according to the default sort order, or the sort order you
  102 + # set with :set_sort_order.
  103 + #
  104 + # sort_parts! is also called from :encode, so there is no need for you to call this explicitly
  105 + def sort_parts!
  106 + order = @part_sort_order
  107 + @parts = @parts.sort do |a, b|
  108 + a_order = order.index(a.content_type.string.downcase) || 1000
  109 + b_order = order.index(b.content_type.string.downcase) || 1000
  110 + a_order <=> b_order
  111 + end
  112 + end
93 113
94 114 # Returns the raw source that the body was initialized with, without
95 115 # any tampering
@@ -101,6 +121,7 @@ def raw_source
101 121 # raw source. Need to implement
102 122 def encoded
103 123 if multipart?
  124 + self.sort_parts!
104 125 encoded_parts = parts.map { |p| p.encoded }
105 126 ([preamble] + encoded_parts).join(crlf_boundary) + end_boundary + epilogue.to_s
106 127 else
6 lib/mail/message.rb
@@ -486,7 +486,8 @@ def add_charset
486 486 if body.only_us_ascii?
487 487 content_type.parameters['charset'] = 'US-ASCII'
488 488 else
489   - STDERR.puts("Non US-ASCII detected and no charset defined.\nDefaulting to UTF-8, set your own if this is incorrect.")
  489 + warning = "Non US-ASCII detected and no charset defined.\nDefaulting to UTF-8, set your own if this is incorrect.\nCalled from:\n#{caller.join("\n")}"
  490 + STDERR.puts(warning)
490 491 content_type.parameters['charset'] = 'UTF-8'
491 492 end
492 493 end
@@ -498,7 +499,8 @@ def add_transfer_encoding
498 499 if body.only_us_ascii?
499 500 header['Content-Transfer-Encoding'] = '7bit'
500 501 else
501   - STDERR.puts("Non US-ASCII detected and no content-transfer-encoding defined.\nDefaulting to 8bit, set your own if this is incorrect.")
  502 + warning = "Non US-ASCII detected and no content-transfer-encoding defined.\nDefaulting to 8bit, set your own if this is incorrect.\nCalled from:\n#{caller.join("\n")}"
  503 + STDERR.puts(warning)
502 504 header['Content-Transfer-Encoding'] = '8bit'
503 505 end
504 506 end
2  lib/mail/version.rb
@@ -3,7 +3,7 @@ module Mail
3 3 module VERSION
4 4 MAJOR = 1
5 5 MINOR = 4
6   - TINY = 1
  6 + TINY = 2
7 7
8 8 STRING = [MAJOR, MINOR, TINY].join('.')
9 9 end
68 spec/mail/body_spec.rb
@@ -212,6 +212,74 @@
212 212 body.parts.length.should == 1
213 213 body.should be_multipart
214 214 end
  215 +
  216 + it "should allow you to sort the parts" do
  217 + body = Mail::Body.new('')
  218 + body << Mail::Part.new("content-type: text/html\r\nsubject: HTML")
  219 + body << Mail::Part.new("content-type: text/plain\r\nsubject: Plain Text")
  220 + body << Mail::Part.new("content-type: text/enriched\r\nsubject: Enriched")
  221 + body.parts.length.should == 3
  222 + body.should be_multipart
  223 + body.sort_parts!
  224 + body.parts[0].content_type.value.should == "text/plain"
  225 + body.parts[1].content_type.value.should == "text/enriched"
  226 + body.parts[2].content_type.value.should == "text/html"
  227 + end
  228 +
  229 + it "should allow you to sort the parts with an arbitrary sort order" do
  230 + body = Mail::Body.new('')
  231 + body.set_sort_order([ "text/plain", "text/html", "text/enriched" ])
  232 + body << Mail::Part.new("content-type: text/html\r\nsubject: HTML")
  233 + body << Mail::Part.new("content-type: text/plain\r\nsubject: Plain Text")
  234 + body << Mail::Part.new("content-type: text/enriched\r\nsubject: Enriched")
  235 + body.parts.length.should == 3
  236 + body.should be_multipart
  237 + body.sort_parts!
  238 + body.parts[0].content_type.value.should == "text/plain"
  239 + body.parts[1].content_type.value.should == "text/html"
  240 + body.parts[2].content_type.value.should == "text/enriched"
  241 + end
  242 +
  243 + it "should allow you to sort the parts with an arbitrary sort order" do
  244 + body = Mail::Body.new('')
  245 + body.set_sort_order(["application/x-yaml", "text/plain"])
  246 + body << Mail::Part.new("content-type: text/plain\r\nsubject: HTML")
  247 + body << Mail::Part.new("content-type: text/html\r\nsubject: Plain Text")
  248 + body << Mail::Part.new("content-type: application/x-yaml\r\nsubject: Enriched")
  249 + body.parts.length.should == 3
  250 + body.should be_multipart
  251 + body.sort_parts!
  252 + body.parts[0].content_type.value.should == "application/x-yaml"
  253 + body.parts[1].content_type.value.should == "text/plain"
  254 + body.parts[2].content_type.value.should == "text/html"
  255 + end
  256 +
  257 + it "should sort the parts on encode" do
  258 + body = Mail::Body.new('')
  259 + body << Mail::Part.new("content-type: text/html\r\nsubject: HTML")
  260 + body << Mail::Part.new("content-type: text/plain\r\nsubject: Plain Text")
  261 + body << Mail::Part.new("content-type: text/enriched\r\nsubject: Enriched")
  262 + body.parts.length.should == 3
  263 + body.should be_multipart
  264 + body.encoded
  265 + body.parts[0].content_type.value.should == "text/plain"
  266 + body.parts[1].content_type.value.should == "text/enriched"
  267 + body.parts[2].content_type.value.should == "text/html"
  268 + end
  269 +
  270 + it "should put the part types it doesn't know about at the end" do
  271 + body = Mail::Body.new('')
  272 + body << Mail::Part.new("content-type: text/html\r\nsubject: HTML")
  273 + body << Mail::Part.new("content-type: text/plain\r\nsubject: Plain Text")
  274 + body << Mail::Part.new("content-type: image/jpeg\r\n")
  275 + body.parts.length.should == 3
  276 + body.should be_multipart
  277 + body.encoded
  278 + body.parts[0].content_type.value.should == "text/plain"
  279 + body.parts[1].content_type.value.should == "text/html"
  280 + body.parts[2].content_type.value.should == "image/jpeg"
  281 + end
  282 +
215 283 end
216 284
217 285 describe "matching" do
6 spec/mail/message_spec.rb
@@ -1410,7 +1410,7 @@ def basic_email
1410 1410 mail = Mail.new
1411 1411 mail.body = body
1412 1412 mail.content_transfer_encoding = "8bit"
1413   - STDERR.should_receive(:puts).with("Non US-ASCII detected and no charset defined.\nDefaulting to UTF-8, set your own if this is incorrect.")
  1413 + STDERR.should_receive(:puts).with(/Non US-ASCII detected and no charset defined.\nDefaulting to UTF-8, set your own if this is incorrect./m)
1414 1414 mail.to_s =~ %r{Content-Type: text/plain; charset=UTF-8}
1415 1415 end
1416 1416
@@ -1420,7 +1420,7 @@ def basic_email
1420 1420 mail.body = body
1421 1421 mail.content_type = "text/plain"
1422 1422 mail.content_transfer_encoding = "8bit"
1423   - STDERR.should_receive(:puts).with("Non US-ASCII detected and no charset defined.\nDefaulting to UTF-8, set your own if this is incorrect.")
  1423 + STDERR.should_receive(:puts).with(/Non US-ASCII detected and no charset defined.\nDefaulting to UTF-8, set your own if this is incorrect./m)
1424 1424 mail.to_s =~ %r{Content-Type: text/plain; charset=UTF-8}
1425 1425 end
1426 1426
@@ -1479,7 +1479,7 @@ def basic_email
1479 1479 mail.content_type = "text/plain; charset=utf-8"
1480 1480 mail.should be_has_content_type
1481 1481 mail.should be_has_charset
1482   - STDERR.should_receive(:puts).with("Non US-ASCII detected and no content-transfer-encoding defined.\nDefaulting to 8bit, set your own if this is incorrect.")
  1482 + STDERR.should_receive(:puts).with(/Non US-ASCII detected and no content-transfer-encoding defined.\nDefaulting to 8bit, set your own if this is incorrect./m)
1483 1483 mail.to_s =~ %r{Content-Transfer-Encoding: 8bit}
1484 1484 end
1485 1485

0 comments on commit fbafc49

Please sign in to comment.
Something went wrong with that request. Please try again.