Class: Snitch::Services::Email

Inherits:
Snitch::Service show all
Defined in:
lib/snitch/services/email.rb

Instance Attribute Summary

Attributes inherited from Snitch::Service

#attributes, #base

Instance Method Summary collapse

Methods inherited from Snitch::Service

#method_missing, new_from_name

Constructor Details

#initialize(*args) ⇒ Email

Returns a new instance of Email.



12
13
14
# File 'lib/snitch/services/email.rb', line 12

def initialize(*args)
  super(*args)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Snitch::Service

Instance Method Details

#create_email(message) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/snitch/services/email.rb', line 29

def create_email(message)
  mail = TMail::Mail.new
  mail.to = to
  mail.from = from
  mail.subject = message[0, message.index("\n") || message.length]
  mail.date = Time.now
  mail.set_content_type 'text', 'plain'
  mail.body = message
  mail
end

#lookup(var) ⇒ Object



40
41
42
43
44
# File 'lib/snitch/services/email.rb', line 40

def lookup(var)
  self.send(var)
rescue NameError
  nil
end

#send_email(email) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/snitch/services/email.rb', line 21

def send_email(email)
  email_opts = %W(server port host login password auth_method).collect { |attr| lookup(attr) }

  Net::SMTP.start(*email_opts) do |smtp|
    smtp.send_message(email.to_s, from, to)
  end
end

#tattle(message) ⇒ Object

Sends the email



17
18
19
# File 'lib/snitch/services/email.rb', line 17

def tattle(message)
  send_email(create_email(message))
end