Skip to content

Commit

Permalink
Update delivery method
Browse files Browse the repository at this point in the history
The delivery method is where the actual sending and in our case
previewing happens - for that reason it is where the calls to the Notify
API are made.

We've updated this class to make it work for the new way messages are
formed and for simplicity.

We've stopped using webmock in the tests as that felt too much
intergration, it felt like we are testing the notifications_client
rather than our code as long as we pass the correct params to the client
we should feel confident the right thing will happen.
  • Loading branch information
mec committed Apr 19, 2024
1 parent 0ef2a98 commit c554673
Show file tree
Hide file tree
Showing 3 changed files with 222 additions and 142 deletions.
42 changes: 16 additions & 26 deletions lib/mail/notify/delivery_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,28 @@ class DeliveryMethod
attr_accessor :settings, :response

def initialize(settings)
raise ArgumentError, "You must specify an API key" if settings[:api_key].blank?
raise ArgumentError, "You must specify an Notify API key" if settings[:api_key].blank?

@settings = settings
end

def deliver!(mail)
@mail = mail
@personalisation = Personalisation.new(mail)
send_email
def deliver!(message)
params = {
template_id: message.template_id,
email_address: message.to.first,
personalisation: message.personalisation,
email_reply_to_id: message.reply_to_id,
reference: message.reference
}

client.send_email(params.compact)
end

def preview(mail)
personalisation = Personalisation.new(mail).to_h
template_id = mail[:template_id].to_s
def preview(message)
template_id = message.template_id
personalisation = message.personalisation

Rails.logger.info("Getting Notify preview for template id #{template_id}")
client.generate_template_preview(template_id, personalisation: personalisation)
end

Expand All @@ -28,24 +36,6 @@ def preview(mail)
def client
@client ||= Notifications::Client.new(@settings[:api_key], @settings[:base_url])
end

def email_params
{
email_address: @mail.to.first,
template_id: @mail[:template_id].to_s,
personalisation: @personalisation.to_h,
email_reply_to_id: optional_param(:reply_to_id),
reference: optional_param(:reference)
}
end

def optional_param(name)
@mail[name].presence&.to_s
end

def send_email
@response = client.send_email(email_params.compact)
end
end
end
end
Loading

0 comments on commit c554673

Please sign in to comment.