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 Feb 23, 2024
1 parent bb4cb5d commit 440b4a9
Show file tree
Hide file tree
Showing 4 changed files with 222 additions and 143 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
1 change: 0 additions & 1 deletion mail-notify.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "rspec-rails", "~> 5.1"
spec.add_development_dependency "standard", "~> 1"
spec.add_development_dependency "sqlite3", "~> 1.6.2"
spec.add_development_dependency "webmock", "~> 3.16.0"
spec.add_development_dependency "rspec-mocks", "~> 3.12.6"

spec.add_dependency "rack", ">= 2.1.4.1"
Expand Down
Loading

0 comments on commit 440b4a9

Please sign in to comment.