Class: CampaignSubscriptions::RefuelService

Inherits:
Object
  • Object
show all
Defined in:
app/services/campaign_subscriptions/refuel_service.rb

Instance Method Summary collapse

Constructor Details

#initialize(campaign_subscription, offset: :created_at) ⇒ RefuelService

Returns a new instance of RefuelService.

Raises:

  • (ArgumentError)


4
5
6
7
8
9
10
# File 'app/services/campaign_subscriptions/refuel_service.rb', line 4

def initialize(campaign_subscription, offset: :created_at)
  raise ArgumentError, "must be either :current or :created_at" unless [:created_at, :current].include?(offset.to_sym)

  @campaign_subscription = campaign_subscription
  @campaign = @campaign_subscription.caffeinate_campaign
  @offset = offset.to_sym
end

Instance Method Details

#callObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/services/campaign_subscriptions/refuel_service.rb', line 12

def call
  mailings = []

  @campaign.to_dripper.drips.each do |drip|
    mailing = Caffeinate::Mailing.find_or_initialize_from_drip(@campaign_subscription, drip)
    if mailing.new_record?
      mailing.send_at = drip.send_at(@campaign_subscription)
      if @offset == :created_at
        mailing.send_at + (Caffeinate.config.now.call - @campaign_subscription.created_at)
      elsif @offset == :current
        # do nothing on purpose!
      end

      mailing.save!
      mailings << mailing
    end
  end

  mailings
end