Class: God::Contacts::Sms

Inherits:
God::Contact show all
Defined in:
lib/god/contacts/sms.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes inherited from God::Contact

#group, #info, #name

Instance Method Summary collapse

Methods inherited from God::Contact

#friendly_name, generate, normalize, valid?

Methods included from God::Configurable

#base_name, complain, #complain, #friendly_name, #prepare, #reset

Class Attribute Details

.delivery_methodObject

Returns the value of attribute delivery_method.



10
11
12
# File 'lib/god/contacts/sms.rb', line 10

def delivery_method
  @delivery_method
end

.formatObject

Returns the value of attribute format.



10
11
12
# File 'lib/god/contacts/sms.rb', line 10

def format
  @format
end

.message_settingsObject

Returns the value of attribute message_settings.



10
11
12
# File 'lib/god/contacts/sms.rb', line 10

def message_settings
  @message_settings
end

.sendmail_settingsObject

Returns the value of attribute sendmail_settings.



10
11
12
# File 'lib/god/contacts/sms.rb', line 10

def sendmail_settings
  @sendmail_settings
end

.server_settingsObject

Returns the value of attribute server_settings.



10
11
12
# File 'lib/god/contacts/sms.rb', line 10

def server_settings
  @server_settings
end

Instance Attribute Details

#phoneObject

Returns the value of attribute phone.



43
44
45
# File 'lib/god/contacts/sms.rb', line 43

def phone
  @phone
end

Instance Method Details

#notify(message, time, priority, category, host) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/god/contacts/sms.rb', line 51

def notify(message, time, priority, category, host)
  begin
    body = Sms.format.call(self.phone, message, time, priority, category, host)

    case Sms.delivery_method
    when :smtp
      notify_smtp(body)
    when :sendmail
      notify_sendmail(body)
    else
      raise "unknown delivery method: #{Sms.delivery_method}"
    end
    
    self.info = "sent sms to #{self.name}:#{self.phone}"
  rescue => e
    applog(nil, :info, "failed to send sms to #{self.name}:#{self.phone}: #{e.message}")
    applog(nil, :debug, e.backtrace.join("\n"))
  end
end

#notify_sendmail(mail) ⇒ Object



87
88
89
90
91
92
# File 'lib/god/contacts/sms.rb', line 87

def notify_sendmail(mail)
  IO.popen("#{Sms.sendmail_settings[:location]} #{Sms.sendmail_settings[:arguments]}","w+") do |sm|
    sm.print(mail.gsub(/\r/, ''))
    sm.flush
  end
end

#notify_smtp(mail) ⇒ Object

private



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/god/contacts/sms.rb', line 73

def notify_smtp(mail)
  args = [Sms.server_settings[:address], Sms.server_settings[:port]]
  if Sms.server_settings[:authentication]
    args << Sms.server_settings[:domain]
    args << Sms.server_settings[:user_name]
    args << Sms.server_settings[:password]
    args << Sms.server_settings[:authentication] 
  end

  Net::SMTP.start(*args) do |smtp|
    smtp.send_message mail, Sms.message_settings[:from], Sms.message_settings[:gateway_email]
  end
end

#valid?Boolean

Returns:

  • (Boolean)


45
46
47
48
49
# File 'lib/god/contacts/sms.rb', line 45

def valid?
  valid = true
  valid &= complain("Attribute 'phone' must be specified", self) if self.phone.nil?
  valid
end