Class: Snoopit::Notifiers::Email

Inherits:
Snoopit::Notifier show all
Defined in:
lib/snoopit/notifiers/email.rb

Instance Attribute Summary collapse

Attributes inherited from Snoopit::Notifier

#configuration, #klass, #name

Instance Method Summary collapse

Constructor Details

#initialize(config = nil) ⇒ Email

The name ‘email’ is used by the Snooper to identify type of notifier to create The empty constructor is used to initialize the class when loaded dynamically After loaded dynamically the method set_config is called by the base class to set the configuration



13
14
15
16
17
18
19
20
# File 'lib/snoopit/notifiers/email.rb', line 13

def initialize(config=nil)
  super config, 'email'
  @smtp_server = @config['smtp-server']
  @port        = @config['port']
  @tls         = @config['tls']
  @user        = @config['user']
  @password    = @config['password']
end

Instance Attribute Details

#portObject (readonly)

Returns the value of attribute port.



9
10
11
# File 'lib/snoopit/notifiers/email.rb', line 9

def port
  @port
end

#smtp_serverObject (readonly)

Returns the value of attribute smtp_server.



9
10
11
# File 'lib/snoopit/notifiers/email.rb', line 9

def smtp_server
  @smtp_server
end

#tlsObject (readonly)

Returns the value of attribute tls.



9
10
11
# File 'lib/snoopit/notifiers/email.rb', line 9

def tls
  @tls
end

Instance Method Details

#build_msg(from, to, msg) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/snoopit/notifiers/email.rb', line 43

def build_msg(from, to, msg)
  msg = <<-MSG
From: #{from}
To: #{to.kind_of?(Array) ? to.join(', ') : to }
Content-Type: text/plain
Subject: #{msg[:comment]}
Date: #{DateTime.now.rfc822}

Snooper event on line: #{msg[:match_line_no]} in file: #{msg[:file]}

#{msg[:before].join('')}#{msg[:match]}#{msg[:after].join('')}
MSG
  msg
end

#notify(found, notifier_params) ⇒ Object



22
23
24
# File 'lib/snoopit/notifiers/email.rb', line 22

def notify(found, notifier_params)
  send found, notifier_params
end

#send(msg, notifier_params) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/snoopit/notifiers/email.rb', line 26

def send(msg, notifier_params)
  begin
    auth = notifier_params['authentication'].nil? ? :login : notifier_params['authentication'].to_sym
    from = notifier_params['from'].nil? ? '[email protected]' : notifier_params['from']
    formatted = build_msg from, notifier_params['to'], msg
    smtp = Net::SMTP.new @smtp_server, @port
    helo = @user.split('@')[1] || 'localhost'
    smtp.enable_starttls_auto
    smtp.start helo, @user, @password, auth do |smtp|
      smtp.send_message(formatted, from, notifier_params['to'])
    end
  rescue => e
    Snoopit.logger.warn e.message
    Snoopit.logger.warn e.backtrace
  end
end