Class: Lockally::Rails::DeliveryMethod

Inherits:
Object
  • Object
show all
Defined in:
lib/lockally/rails/delivery_method.rb

Overview

ActionMailer delivery method that sends through the Lockally API (POST /v1/send).

Registered as :lockally by the railtie, so:

config.action_mailer.delivery_method = :lockally
config.action_mailer.lockally_settings = { api_key: ENV["LOCKALLY_API_KEY"] }

routes every ActionMailer deliver/deliver_later through Lockally.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings = {}) ⇒ DeliveryMethod

Returns a new instance of DeliveryMethod.



20
21
22
23
# File 'lib/lockally/rails/delivery_method.rb', line 20

def initialize(settings = {})
  @settings = settings || {}
  @send_api = settings[:send_api] # injectable for tests
end

Instance Attribute Details

#settingsObject (readonly)

Returns the value of attribute settings.



18
19
20
# File 'lib/lockally/rails/delivery_method.rb', line 18

def settings
  @settings
end

Instance Method Details

#build_payload(mail) ⇒ Object



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
# File 'lib/lockally/rails/delivery_method.rb', line 35

def build_payload(mail)
  payload = {
    from: Array(mail.from).first,
    to: Array(mail.to),
  }
  payload[:cc] = Array(mail.cc) if mail.cc
  payload[:bcc] = Array(mail.bcc) if mail.bcc
  payload[:subject] = mail.subject if mail.subject

  text, html = bodies(mail)
  payload[:text] = text if text
  payload[:html] = html if html

  headers = custom_headers(mail)
  payload[:headers] = headers unless headers.empty?

  attachments = mail.attachments.map do |part|
    ::Lockally::V1SendPostRequestAttachmentsInner.new(
      filename: part.filename || "attachment",
      content_type: part.mime_type || "application/octet-stream",
      content_base64: Base64.strict_encode64(part.body.decoded),
    )
  end
  payload[:attachments] = attachments unless attachments.empty?

  payload
end

#build_request(mail) ⇒ Object

Map a Mail::Message onto the Lockally send request. Public for unit testing.



31
32
33
# File 'lib/lockally/rails/delivery_method.rb', line 31

def build_request(mail)
  ::Lockally::V1SendPostRequest.new(build_payload(mail))
end

#deliver!(mail) ⇒ Object

ActionMailer calls this with a Mail::Message.



26
27
28
# File 'lib/lockally/rails/delivery_method.rb', line 26

def deliver!(mail)
  send_api.v1_send_post(idempotency_key, build_request(mail))
end