Class: PostageApp::Mail::Arguments

Inherits:
Object
  • Object
show all
Defined in:
lib/postageapp/mail/arguments.rb

Overview

This class decomposes a Mail::Message into a PostageApp API call that produces the same result when sent.

Constant Summary collapse

HEADERS_IGNORED =

Certain headers need to be ignored since they are generated internally.

Set.new(%w[
  Content-Type
  To
]).freeze

Instance Method Summary collapse

Constructor Details

#initialize(mail) ⇒ Arguments

Creates a new instance with the given Mail::Message binding.



14
15
16
# File 'lib/postageapp/mail/arguments.rb', line 14

def initialize(mail)
  @mail = mail
end

Instance Method Details

#extract(arguments = nil) ⇒ Object

Returns the extracted arguments. If a pre-existing arguments has is supplied, arguments are injected into that.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/postageapp/mail/arguments.rb', line 20

def extract(arguments = nil)
  arguments ||= { }
  arguments['content'] ||= { }
  arguments['headers'] ||= { }

  arguments['recipients'] = @mail.to

  if (@mail.multipart?)
    @mail.parts.each do |part|
      if (part.content_disposition)
        add_attachment(arguments, part)
      else
        add_part(arguments, part)
      end
    end
  else
    add_part(arguments, @mail)
  end

  _headers = arguments['headers']
  @mail.header.fields.each do |field|
    next if (HEADERS_IGNORED.include?(field.name))

    _headers[field.name] = field.value
  end

  if (@mail.has_attachments?)
    @mail.attachments.each do |attachment|
      add_attachment(arguments, attachment)
    end
  end

  [ :send_message, arguments ]
end