Class: AzureCommunicationEmail::Payload

Inherits:
Object
  • Object
show all
Defined in:
lib/azure_communication_email/payload.rb

Instance Method Summary collapse

Constructor Details

#initialize(mail) ⇒ Payload

Returns a new instance of Payload.



9
10
11
# File 'lib/azure_communication_email/payload.rb', line 9

def initialize(mail)
  @mail = mail
end

Instance Method Details

#as_jsonObject



13
14
15
16
17
18
19
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
54
55
# File 'lib/azure_communication_email/payload.rb', line 13

def as_json
  hash = {
    "senderAddress" => first_sender_address,
    "content" => {
      "subject"   => @mail.subject.to_s,
      "plainText" => plain_text_body
    },
    "recipients" => {
      "to" => recipient_objects(:to)
    }
  }

  # Does only work with custom domains
  if (name = first_sender_display_name).present?
    hash["senderDisplayName"] = name
  end

  if (html = html_body).present?
    hash["content"]["html"] = html
  end

  if (cc = recipient_objects(:cc)).present?
    hash["recipients"]["cc"] = cc
  end

  if (bcc = recipient_objects(:bcc)).present?
    hash["recipients"]["bcc"] = bcc
  end

  if (reply_to = recipient_objects(:reply_to)).present?
    hash["replyTo"] = reply_to
  end

  if (headers = custom_headers).present?
    hash["headers"] = headers
  end

  if (attachments = attachment_objects).present?
    hash["attachments"] = attachments
  end

  hash
end

#to_json(*args) ⇒ Object



57
58
59
# File 'lib/azure_communication_email/payload.rb', line 57

def to_json(*args)
  JSON.generate(as_json, *args)
end