Class: Boutique::Emailer

Inherits:
Object
  • Object
show all
Defined in:
lib/boutique/emailer.rb

Instance Method Summary collapse

Constructor Details

#initialize(list, directory = nil) ⇒ Emailer

Returns a new instance of Emailer.



3
4
5
6
# File 'lib/boutique/emailer.rb', line 3

def initialize(list, directory = nil)
  @list = list
  @directory = directory
end

Instance Method Details

#blast(path, locals = {}) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/boutique/emailer.rb', line 50

def blast(path, locals = {})
  yaml, body = preamble(full_path(path))
  email_key = yaml['key']
  @list.subscribers.where(confirmed: true).each do |subscriber|
    # TODO: speed up by moving filter outside of loop
    if Email.first(email_key: yaml['key'], subscriber: subscriber).nil?
      self.deliver(subscriber, path, locals)
    end
  end
end

#deliver(subscriber, path, locals = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/boutique/emailer.rb', line 23

def deliver(subscriber, path, locals = {})
  locals = locals.merge(
    subscribe_url: @list.subscribe_url,
    confirm_url: subscriber.confirm_url,
    unsubscribe_url: subscriber.unsubscribe_url)
  yaml, body = self.render(path, locals, true)
  if yaml['day'] == 0
    ymd = Date.today.strftime("%Y-%m-%d")
    Email.create(email_key: "#{yaml['key']}-#{ymd}", subscriber: subscriber)
  else
    raise "Unconfirmed #{subscriber.email} for #{yaml['key']}" if !subscriber.confirmed
    Email.create(email_key: yaml['key'], subscriber: subscriber)
  end
  Pony.mail(
    to: subscriber.email,
    from: @list.from,
    subject: yaml['subject'],
    headers: {'Content-Type' => 'text/html'},
    body: body)
rescue DataMapper::SaveFailureError
  raise "Duplicate email #{yaml['key']} to #{subscriber.email}"
end

#deliver_zero(subscriber) ⇒ Object



46
47
48
# File 'lib/boutique/emailer.rb', line 46

def deliver_zero(subscriber)
  self.deliver(subscriber, emails[0])
end

#dripObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/boutique/emailer.rb', line 61

def drip
  today = Date.today
  max_day = emails.keys.max || 0
  subscribers = @list.subscribers.
    where(confirmed: true).
    where { (drip_on < today) & (drip_day < max_day) }
  subscribers.each do |subscriber|
    subscriber.drip_on = today
    subscriber.drip_day += 1
    subscriber.save
    if (email_path = emails[subscriber.drip_day])
      self.deliver(subscriber, email_path)
    end
  end
end

#render(path, locals = {}, pre = false) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/boutique/emailer.rb', line 8

def render(path, locals = {}, pre = false)
  path = @directory ?
    File.join(@directory, path) :
    full_path(path)
  raise "File not found: #{path}" if !File.exist?(path)

  yaml, body = preamble(path)
  templates_for(path).each do |template|
    blk = proc { body }
    body = template.new(path, &blk).render(self, locals)
  end

  pre ? [yaml, body] : body
end