-
Notifications
You must be signed in to change notification settings - Fork 10
/
immediate_email_builder.rb
72 lines (55 loc) · 1.46 KB
/
immediate_email_builder.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
class ImmediateEmailBuilder
include Callable
def initialize(content, subscriptions)
@content = content
@subscriptions = subscriptions
end
def call
Email.timed_bulk_insert(records, ImmediateEmailGenerationService::BATCH_SIZE)
.pluck("id")
end
private
attr_reader :content, :subscriptions
def records
@records ||= begin
now = Time.zone.now
subscriptions.map do |subscription|
subscriber = subscription.subscriber
{
address: subscriber.address,
subject: subject,
body: body(subscription, subscriber),
subscriber_id: subscriber.id,
created_at: now,
updated_at: now,
}
end
end
end
def subject
"Update from GOV.UK for: #{content.title}"
end
def body(subscription, subscriber)
list = subscription.subscriber_list
<<~BODY
Update from GOV.UK for:
# #{list.title}
---
#{middle_section(subscription)}
---
#{FooterPresenter.call(subscriber, subscription)}
BODY
end
def middle_section(subscription)
subscriber_list = subscription.subscriber_list
presenter = "#{content.class.name}Presenter".constantize
section = presenter.call(content, subscription)
source_url = SourceUrlPresenter.call(
subscriber_list.url,
utm_source: subscriber_list.slug,
utm_content: "immediate",
)
section += "\n\n#{source_url}" if source_url
section
end
end