Class: Mail::AttachmentsList

Inherits:
Array
  • Object
show all
Defined in:
lib/mail/attachments_list.rb

Instance Method Summary collapse

Constructor Details

#initialize(parts_list) ⇒ AttachmentsList

Returns a new instance of AttachmentsList.



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/mail/attachments_list.rb', line 4

def initialize(parts_list)
  @parts_list = parts_list
  parts_list.map { |p|
    if p.parts.empty?
      p if p.attachment?
    else
      p.attachments
    end
  }.flatten.compact.each { |a| self << a }
  self
end

Instance Method Details

#[](index_value) ⇒ Object

Returns the attachment by filename or at index.

mail.attachments = File.read(‘test.png’) mail.attachments = File.read(‘test.jpg’)

mail.attachments.filename #=> ‘test.png’ mail.attachments.filename #=> ‘test.jpg’



23
24
25
26
27
28
29
# File 'lib/mail/attachments_list.rb', line 23

def [](index_value)
  if index_value.is_a?(Fixnum)
    self.fetch(index_value)
  else
    self.select { |a| a.filename == index_value }.first
  end
end

#[]=(name, value) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/mail/attachments_list.rb', line 31

def []=(name, value)
  default_values = { :content_type => "#{set_mime_type(name)}; filename=\"#{name}\"",
                     :content_transfer_encoding => 'Base64',
                     :content_disposition => "attachment; filename=\"#{name}\"" }

  if value.is_a?(Hash)

    default_values[:body] = value.delete(:content) if value[:content]

    default_values[:body] = value.delete(:data) if value[:data]

    # Only force encode base64 if the user has not specified an encoding
    if value[:transfer_encoding]
      default_values[:content_transfer_encoding] = value.delete(:transfer_encoding)
    elsif value[:encoding]
      default_values[:content_transfer_encoding] = value.delete(:encoding)
    else
      default_values[:body] = Mail::Encodings::Base64.encode(default_values[:body])
    end

    if value[:mime_type]
      default_values[:content_type] = value.delete(:mime_type)
    end

    hash = default_values.merge(value)
  else
    default_values[:body] = Mail::Encodings::Base64.encode(value)
    hash = default_values
  end

  @parts_list << Part.new(hash)
end

#set_mime_type(filename) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/mail/attachments_list.rb', line 64

def set_mime_type(filename)
  # Have to do this because MIME::Types is not Ruby 1.9 safe yet
  if RUBY_VERSION >= '1.9'
    new_file = String.new(filename).force_encoding(Encoding::BINARY)
    ext = new_file.split('.'.force_encoding(Encoding::BINARY)).last
    filename = "file.#{ext}".force_encoding('US-ASCII')
  end
  @mime_type = MIME::Types.type_for(filename).first
end