Class: Nuntius::DeliverCampaignService

Inherits:
ApplicationService show all
Defined in:
app/services/nuntius/deliver_campaign_service.rb

Instance Method Summary collapse

Instance Method Details

#deliverObject



17
18
19
20
21
22
# File 'app/services/nuntius/deliver_campaign_service.rb', line 17

def deliver
  transporter = BaseTransport.class_from_name(campaign.transport).new
  campaign.list.subscribers.subscribed.each do |subscriber|
    transporter.deliver(new_message(subscriber))
  end
end

#new_message(subscriber, assigns = {}) ⇒ Object



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
# File 'app/services/nuntius/deliver_campaign_service.rb', line 24

def new_message(subscriber, assigns = {})
  assigns["campaign"] = context.campaign
  assigns["subscriber"] = subscriber
  if subscriber.nuntiable
    name = Nuntius::BaseMessenger.liquid_variable_name_for(subscriber.nuntiable)
    assigns[name] = subscriber.nuntiable
  end
  message = Nuntius::Message.new(transport: campaign.transport, campaign: campaign, nuntiable: subscriber.nuntiable, metadata: campaign.)

  locale = nil
  if subscriber.nuntiable
    locale_proc = Nuntius::BaseMessenger.messenger_for_obj(subscriber.nuntiable).locale
    locale = instance_exec(subscriber.nuntiable, &locale_proc) if locale_proc
  end

  message.from = render(:from, assigns, locale)
  message.to = case campaign.transport
  when "mail"
    subscriber.email
  when "sms"
    subscriber.phone_number
  when "voice"
    subscriber.phone_number
  end

  message.subject = render(:subject, assigns, locale)
  message.html = render(:html, assigns, locale, layout: campaign.layout&.data)

  message
end

#performObject



12
13
14
15
# File 'app/services/nuntius/deliver_campaign_service.rb', line 12

def perform
  deliver
  # campaign.sent!
end

#translation_scopeObject



55
56
57
58
59
# File 'app/services/nuntius/deliver_campaign_service.rb', line 55

def translation_scope
  scope = %w[nuntius]
  scope << campaign.layout.name.underscore.tr(" ", "_") if layout
  scope.join(".")
end