Skip to content

Commit a21b552

Browse files
committed
Update delivery method
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.
1 parent 5e52732 commit a21b552

File tree

3 files changed

+222
-142
lines changed

3 files changed

+222
-142
lines changed

lib/mail/notify/delivery_method.rb

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,28 @@ class DeliveryMethod
66
attr_accessor :settings, :response
77

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

1111
@settings = settings
1212
end
1313

14-
def deliver!(mail)
15-
@mail = mail
16-
@personalisation = Personalisation.new(mail)
17-
send_email
14+
def deliver!(message)
15+
params = {
16+
template_id: message.template_id,
17+
email_address: message.to.first,
18+
personalisation: message.personalisation,
19+
email_reply_to_id: message.reply_to_id,
20+
reference: message.reference
21+
}
22+
23+
client.send_email(params.compact)
1824
end
1925

20-
def preview(mail)
21-
personalisation = Personalisation.new(mail).to_h
22-
template_id = mail[:template_id].to_s
26+
def preview(message)
27+
template_id = message.template_id
28+
personalisation = message.personalisation
29+
30+
Rails.logger.info("Getting Notify preview for template id #{template_id}")
2331
client.generate_template_preview(template_id, personalisation: personalisation)
2432
end
2533

@@ -28,24 +36,6 @@ def preview(mail)
2836
def client
2937
@client ||= Notifications::Client.new(@settings[:api_key], @settings[:base_url])
3038
end
31-
32-
def email_params
33-
{
34-
email_address: @mail.to.first,
35-
template_id: @mail[:template_id].to_s,
36-
personalisation: @personalisation.to_h,
37-
email_reply_to_id: optional_param(:reply_to_id),
38-
reference: optional_param(:reference)
39-
}
40-
end
41-
42-
def optional_param(name)
43-
@mail[name].presence&.to_s
44-
end
45-
46-
def send_email
47-
@response = client.send_email(email_params.compact)
48-
end
4939
end
5040
end
5141
end

0 commit comments

Comments
 (0)