Class: Broadcast::DeliveryMethod

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

Constant Summary collapse

false

Instance Method Summary collapse

Constructor Details

#initialize(settings = {}) ⇒ DeliveryMethod

Returns a new instance of DeliveryMethod.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/broadcast/delivery_method.rb', line 10

def initialize(settings = {})
  opts = settings.to_h.dup
  @include_unsubscribe_link =
    if opts.key?(:include_unsubscribe_link)
      opts.delete(:include_unsubscribe_link)
    else
      DEFAULT_INCLUDE_UNSUBSCRIBE_LINK
    end

  @client = Client.new(**opts)
end

Instance Method Details

#deliver!(mail) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/broadcast/delivery_method.rb', line 22

def deliver!(mail)
  @client.send_email(
    to: mail.to&.first,
    subject: mail.subject,
    body: extract_body(mail),
    reply_to: mail.reply_to&.first,
    html_body: (true if mail.html_part),
    include_unsubscribe_link: @include_unsubscribe_link
  )
rescue Broadcast::WarningError
  # The send succeeded — warnings_mode: :raise is about surfacing ignored
  # parameters, not reporting a delivery failure. Wrapping it in
  # DeliveryError would tell ActionMailer the mail didn't go out.
  raise
rescue Broadcast::Error => e
  raise DeliveryError, "Failed to deliver email: #{e.message}"
end