Class: RequestLogAnalyzer::Mailer

Inherits:
Object
  • Object
show all
Defined in:
lib/request_log_analyzer/mailer.rb

Overview

Mail report to a specified emailaddress

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(to, host = 'localhost', options = {}) ⇒ Mailer

Initialize a mailer to to email address to mail to host the mailer host (defaults to localhost) options Specific style options

Options

  • :debug Do not actually mail

  • :from_alias The from alias

  • :to_alias The to alias

  • :subject The message subject



16
17
18
19
20
21
22
23
24
25
# File 'lib/request_log_analyzer/mailer.rb', line 16

def initialize(to, host = 'localhost', options = {})
  require 'net/smtp'
  @to      = to
  @host    = host

  @port    = 25
  @options = options
  @host, @port = host.split(':') if @host.include?(':')
  @data    = []
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



4
5
6
# File 'lib/request_log_analyzer/mailer.rb', line 4

def data
  @data
end

#hostObject

Returns the value of attribute host.



4
5
6
# File 'lib/request_log_analyzer/mailer.rb', line 4

def host
  @host
end

#portObject

Returns the value of attribute port.



4
5
6
# File 'lib/request_log_analyzer/mailer.rb', line 4

def port
  @port
end

#toObject

Returns the value of attribute to.



4
5
6
# File 'lib/request_log_analyzer/mailer.rb', line 4

def to
  @to
end

Instance Method Details

#<<(string) ⇒ Object



54
55
56
# File 'lib/request_log_analyzer/mailer.rb', line 54

def <<(string)
  data << string
end

#mailObject

Send all data in @data to the email address used during initialization. Returns array containg [message_data, from_email_address, to_email_address] of sent email.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/request_log_analyzer/mailer.rb', line 29

def mail
  from          = @options[:from]        || '[email protected]'
  from_alias    = @options[:from_alias]  || 'Request-log-analyzer reporter'
  to_alias      = @options[:to_alias]    || to
  subject       = @options[:subject]     || "Request log analyzer report - generated on #{Time.now}"
  content_type  = ''
  content_type  = 'Content-Type: text/html; charset="ISO-8859-1";' if @data.map { |l| l.include?('html') }.include?(true)
  msg = "From: \#{from_alias} <\#{from}>\nTo: \#{to_alias} <\#{@to}>\nSubject: \#{subject}\n\#{content_type}\n\n\#{@data.join(\"\\n\")}\n"

  unless @options[:debug]
    Net::SMTP.start(@host, @port) do |smtp|
      smtp.send_message msg, from, to
    end
  end

  [msg, from, to]
end

#puts(string) ⇒ Object



58
59
60
# File 'lib/request_log_analyzer/mailer.rb', line 58

def puts(string)
  data << string
end