Class: Blertr::MailNotifier

Inherits:
Notifier show all
Defined in:
lib/blertr/mail_notifier.rb

Instance Attribute Summary

Attributes inherited from Notifier

#error_messages, #names

Instance Method Summary collapse

Methods inherited from Notifier

#in_time_window?, #name, #options, #set_option, #set_time, #will_alert?

Constructor Details

#initializeMailNotifier

Returns a new instance of MailNotifier.



6
7
8
9
# File 'lib/blertr/mail_notifier.rb', line 6

def initialize
  super
  @names = ["mail", "email"]
end

Instance Method Details

#alert(message) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/blertr/mail_notifier.rb', line 11

def alert message
  username = options[:username]
  blert_user = "#{username}@gmail.com"
  password = options[:password]
  receiver = options[:to]
  msg = create_message(message, receiver, blert_user)
  smtp = Net::SMTP.new 'smtp.gmail.com', 587
  smtp.enable_starttls
  smtp.start('smtp.gmail.com', blert_user, password, :plain) do
    smtp.send_message(msg, blert_user, receiver)
  end
end

#can_alert?Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
41
42
43
44
# File 'lib/blertr/mail_notifier.rb', line 35

def can_alert?
  rtn = true
  rtn &= options[:username]
  error_messages << "No :username option in config file" if !options[:username]
  rtn &= options[:password]
  error_messages << "No :password option in config file" if !options[:password]
  rtn &= options[:to]
  error_messages << "No :to option in config file" if !options[:to]
  rtn
end

#create_message(message, to, from) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/blertr/mail_notifier.rb', line 24

def create_message message, to, from
  date = Time.now.strftime("%a, %d %b %Y %H:%M:%S %z")
  msg = "Date: #{date}\n"
  msg += "From: Blertr App <#{from}>\n"
  msg += "To: #{to} <#{to}>\n"
  msg += "Subject: #{message.command_short_name} is done!\n"
  msg += "\n"
  msg += "#{message.command_short_name} has completed.\nCommand: #{message.command}\nTime: #{message.time_string}\n"
  msg
end