Skip to content
Browse files

Moved all init and attachment functions of Mail::Part into Mail::Mess…

…age, no interface change
  • Loading branch information...
1 parent a28d850 commit d46819040f873d97568a348620cccdc951eb39a1 @mikel committed
Showing with 58 additions and 56 deletions.
  1. +3 −0 CHANGELOG.rdoc
  2. +55 −2 lib/mail/message.rb
  3. +0 −54 lib/mail/part.rb
View
3 CHANGELOG.rdoc
@@ -1,6 +1,9 @@
== Mon Nov 9 23:38:33 UTC 2009 Mikel Lindsaar <raasdnil@gmail.com>
* Added ability to create new email via a hash or hash-like object. <mikel>
+* Moved all of the Part init into the Message class. Part now just uses Message's init,
+ also moved all the attachment related functions into Message. As Part is a subclass
+ of message, you shouldn't see any interface changes here.
== Fri Nov 6 22:52:10 UTC 2009 Mikel Lindsaar <raasdnil@gmail.com>
View
57 lib/mail/message.rb
@@ -565,7 +565,7 @@ def add_part(part)
self.body << part
end
- # Adds a part to the parts list or creates the part list
+ # Adds a file to the message.
def add_file(options)
add_multipart_mixed_header
if options.is_a?(Hash)
@@ -604,6 +604,25 @@ def decoded
raise NoMethodError, 'Can not decode an entire message, try calling #decoded on the various fields and body or parts if it is a multipart message.'
end
+ # Returns true if this part is an attachment
+ def attachment?
+ find_attachment
+ end
+
+ # Returns the attachment data if there is any
+ def attachment
+ @attachment
+ end
+
+ # Returns the filename of the attachment
+ def filename
+ if attachment?
+ attachment.filename
+ else
+ nil
+ end
+ end
+
private
# 2.1. General Description
@@ -664,17 +683,51 @@ def init_with_hash(hash)
@header = Mail::Header.new
@body = Mail::Body.new
hash.each_pair do |k,v|
- self[k] = v
+ next if k.to_sym == :data
+ if k.to_sym == :filename
+ add_attachment(hash)
+ else
+ self[k] = v
+ end
end
end
+
+ def add_attachment(options_hash)
+ @attachment = Mail::Attachment.new(options_hash)
+ self.content_type = "#{attachment.mime_type}; filename=\"#{attachment.filename}\""
+ self.content_transfer_encoding = "Base64"
+ self.content_disposition = "attachment; filename=\"#{attachment.filename}\""
+ self.body = attachment.encoded
+ end
def init_with_string(string)
self.raw_source = string
set_envelope_header
parse_message
separate_parts if multipart?
+ if filename = attachment?
+ encoding = content_transfer_encoding.encoding if content_transfer_encoding
+ @attachment = Mail::Attachment.new(:filename => filename,
+ :data => body.to_s,
+ :encoding => encoding)
+ end
end
+ # Returns the filename of the attachment (if it exists) or returns nil
+ def find_attachment
+ case
+ when content_type && content_type.filename
+ filename = content_type.filename
+ when content_disposition && content_disposition.filename
+ filename = content_disposition.filename
+ when content_location && content_location.location
+ filename = content_location.location
+ else
+ filename = nil
+ end
+ filename
+ end
+
# Only POP3 is supported for now
def Message.get_all_mail(&block)
self.pop3_get_all_mail(&block)
View
54 lib/mail/part.rb
@@ -2,26 +2,6 @@
module Mail
class Part < Message
- def initialize(*args, &block)
- if args.flatten[0].is_a?(Hash)
- options_hash = args.flatten[0]
- super('') # Make an empty message, we are dealing with an attachment
- @attachment = Mail::Attachment.new(options_hash)
- self.content_type = "#{attachment.mime_type}; filename=\"#{attachment.filename}\""
- self.content_transfer_encoding = "Base64"
- self.content_disposition = "attachment; filename=\"#{attachment.filename}\""
- self.body = attachment.encoded
- else
- super
- if filename = attachment?
- encoding = content_transfer_encoding.encoding if content_transfer_encoding
- @attachment = Mail::Attachment.new(:filename => filename,
- :data => body.to_s,
- :encoding => encoding)
- end
- end
- end
-
# Creates a new empty Content-ID field and inserts it in the correct order
# into the Header. The ContentIdField object will automatically generate
# a unique content ID if you try and encode it or output it to_s without
@@ -32,25 +12,6 @@ def add_content_id(content_id_val = '')
header['content-id'] = content_id_val
end
- # Returns true if this part is an attachment
- def attachment?
- find_attachment
- end
-
- # Returns the attachment data if there is any
- def attachment
- @attachment
- end
-
- # Returns the filename of the attachment
- def filename
- if attachment?
- attachment.filename
- else
- nil
- end
- end
-
# Returns true if the part has a content ID field, the field may or may
# not have a value, but the field exists or not.
def has_content_id?
@@ -116,21 +77,6 @@ def parse_delivery_status_report
@delivery_status_data ||= Header.new(body.to_s.gsub("\r\n\r\n", "\r\n"))
end
- # Returns the filename of the attachment (if it exists) or returns nil
- def find_attachment
- case
- when content_type && content_type.filename
- filename = content_type.filename
- when content_disposition && content_disposition.filename
- filename = content_disposition.filename
- when content_location && content_location.location
- filename = content_location.location
- else
- filename = nil
- end
- filename
- end
-
end
end

0 comments on commit d468190

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