Class: Send

Inherits:
Object
  • Object
show all
Defined in:
lib/correole/send.rb

Class Method Summary collapse

Class Method Details

.run!Object



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
# File 'lib/correole/send.rb', line 3

def self.run!
  qputs "Fetch feed from #{Configuration.feed}."
  feed = Feed.get
  split_feed = Feed.split_items feed
  if split_feed[:unsent_item].empty?
    qputs 'There are no new items, exiting.'
    return
  end
  qputs "There are #{split_feed[:unsent_item].length} new items. The items are the following."
  split_feed[:unsent_item].each_with_index { |i, j| qputs "[#{j+1}] #{i.link}" }
  html = compose_html split_feed
  plain = compose_plain split_feed
  rr = recipients
  rr.each_with_index do |r, i|
    html_r = personalize html, r.email
    plain_r = personalize plain, r.email
    qputs "[#{i+1}/#{rr.size}] Send newsletter to #{r.email}."
    begin
      send_out feed[:title], html_r, plain_r, r.email
    rescue => exc
      qputs "Could not send newsletter to #{r.email} for the following reason."
      qputs exc.message
    end
  end
  if not Configuration.dry_run
    qputs 'Remember new items.'
    split_feed[:unsent_item].each { |i| i.save }
  end
  qputs 'Done.'
end