Class: ScalewayTem::ActionMailer::MessageMapper

Inherits:
Object
  • Object
show all
Defined in:
lib/scaleway_tem/action_mailer/message_mapper.rb

Overview

Turns a Mail::Message into the JSON body Scaleway's Transactional Email API expects.

This class is deliberately pure: it takes a message and a project id and returns a Hash. No network, no configuration, no clock. That is what makes the payload contract, which is the part most likely to be wrong, cheap to test exhaustively without stubbing anything.

The shape is documented at https://www.scaleway.com/en/developers/api/transactional-email/

Constant Summary collapse

SKIPPED_HEADERS =

Headers that already have a home as a top level payload field, or that describe the MIME encoding of a message we are about to take apart and rebuild as JSON. Everything else the caller set is forwarded verbatim in additional_headers, which is how Reply-To reaches Scaleway: the API has no reply_to field of its own, and their documentation uses Reply-To as the worked example for additional_headers.

%w[
  from to cc bcc subject
  mime-version content-type content-transfer-encoding content-disposition
  date message-id
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mail, project_id:) ⇒ MessageMapper

Returns a new instance of MessageMapper.



34
35
36
37
# File 'lib/scaleway_tem/action_mailer/message_mapper.rb', line 34

def initialize(mail, project_id:)
  @mail = mail
  @project_id = project_id
end

Class Method Details

.call(mail, project_id:) ⇒ Object



30
31
32
# File 'lib/scaleway_tem/action_mailer/message_mapper.rb', line 30

def self.call(mail, project_id:)
  new(mail, project_id: project_id).call
end

Instance Method Details

#callObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/scaleway_tem/action_mailer/message_mapper.rb', line 39

def call
  payload = {
    from: sender,
    to: addresses(mail[:to]),
    subject: mail.subject.to_s,
    project_id: project_id
  }

  cc = addresses(mail[:cc])
  bcc = addresses(mail[:bcc])
  payload[:cc] = cc if cc.any?
  payload[:bcc] = bcc if bcc.any?

  text = body_for(:text)
  html = body_for(:html)
  payload[:text] = text if text
  payload[:html] = html if html

  attachments = attachments_for
  headers = additional_headers
  payload[:attachments] = attachments if attachments.any?
  payload[:additional_headers] = headers if headers.any?

  payload
end