Class: NewDataNotifier::Notifier

Inherits:
ActionMailer::Base
  • Object
show all
Defined in:
app/mailers/new_data_notifier/notifier.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.be_monitored_modelsObject



19
20
21
# File 'app/mailers/new_data_notifier/notifier.rb', line 19

def be_monitored_models
  @@models ||= []
end

.be_monitored_models=(models) ⇒ Object



15
16
17
# File 'app/mailers/new_data_notifier/notifier.rb', line 15

def be_monitored_models=(models)
  @@models = models
end

.default_optionsObject



31
32
33
34
35
36
37
# File 'app/mailers/new_data_notifier/notifier.rb', line 31

def default_options
  {
    :sender_address => sender_address,
    :recipients => default_recipients,
    :subject => "[New Data Notification]"
  }
end

.default_recipientsObject



11
12
13
# File 'app/mailers/new_data_notifier/notifier.rb', line 11

def default_recipients
  @@recipients ||= []
end

.default_recipients=(recipients) ⇒ Object



7
8
9
# File 'app/mailers/new_data_notifier/notifier.rb', line 7

def default_recipients=(recipients)
  @@recipients = recipients
end

.get_last_sent_atObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/mailers/new_data_notifier/notifier.rb', line 59

def get_last_sent_at
  time_mark_file_path = File.join(Rails.root, 'tmp', 'last_notification_send_at')
  # read last sent time, if blank, set current time.
  begin
    last_sent_at = Time.parse( File.read(time_mark_file_path) )
    raise "tmp/last_notification_send_at not found" if last_sent_at.blank?
  rescue
    last_sent_at = Time.now
  ensure
    # have to ensure each time after finish send mail should change the last_notification_send_at
    File.open(time_mark_file_path, "w") do |f|
      f.write(Time.now)
    end
  end
  last_sent_at
end

.get_latest_added_dataObject



48
49
50
51
52
53
54
55
56
57
# File 'app/mailers/new_data_notifier/notifier.rb', line 48

def get_latest_added_data
  # find data
  data_hash = {}
  last_sent_at = get_last_sent_at
  NewDataNotifier::Notifier.be_monitored_models.each do |model|
    data_hash[model.downcase.to_sym] = model.constantize.where("created_at >= ?", last_sent_at).order("created_at DESC")
  end
  data_hash.delete_if { |key, value| value.count == 0 }
  data_hash
end

.send_all_notificationObject

used to deliver all notification mails



40
41
42
43
44
45
46
# File 'app/mailers/new_data_notifier/notifier.rb', line 40

def send_all_notification
  data_hash = get_latest_added_data
  # send mail
  unless data_hash.select { |key, value| value.count > 0 }.blank?
    NewDataNotifier::Notifier.notify(data_hash).deliver
  end
end

.sender_addressObject



27
28
29
# File 'app/mailers/new_data_notifier/notifier.rb', line 27

def sender_address
  @@sender_address ||= %("New Data Notifier" <[email protected]>)
end

.sender_address=(address) ⇒ Object



23
24
25
# File 'app/mailers/new_data_notifier/notifier.rb', line 23

def sender_address=(address)
  @@sender_address = address
end

Instance Method Details

#notify(data_hash) ⇒ Object

notification event.



79
80
81
82
83
# File 'app/mailers/new_data_notifier/notifier.rb', line 79

def notify(data_hash)
  @data_hash = data_hash
  options = self.class.default_options
  mail(:to => options[:recipients], :from => options[:sender_address], :subject => options[:subject])
end