-
Notifications
You must be signed in to change notification settings - Fork 10
/
notify_provider.rb
43 lines (34 loc) · 1.08 KB
/
notify_provider.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class SendEmailService::NotifyProvider
def initialize
@client = EmailAlertAPI.config.notify_client
@template_id = EmailAlertAPI.config.notify.fetch(:template_id)
end
def self.call(**args)
new.call(**args)
end
def call(address:, subject:, body:, reference:)
client.send_email(
email_address: address,
template_id: template_id,
reference: reference,
personalisation: {
subject: subject,
body: body,
},
)
Metrics.sent_to_notify_successfully
:sent
rescue Notifications::Client::RequestError, Net::OpenTimeout, Net::ReadTimeout, SocketError => e
Metrics.failed_to_send_to_notify
Rails.logger.warn(
"Notify communication failure for reference #{reference}. #{e.class}: #{e}",
)
undeliverable_failure?(e) ? :undeliverable_failure : :provider_communication_failure
end
private
attr_reader :client, :template_id
def undeliverable_failure?(error)
return false unless error.is_a?(Notifications::Client::BadRequestError)
error.message.end_with?("Not a valid email address")
end
end