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



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

def initialize(to, host = 'localhost', options = {})
  require 'net/smtp'
  @to      = to
  @host    = host
  @options = options
  @data    = []
  @content_type = nil
end

Instance Attribute Details

#content_typeObject

Returns the value of attribute content_type.



6
7
8
# File 'lib/request_log_analyzer/mailer.rb', line 6

def content_type
  @content_type
end

#dataObject

Returns the value of attribute data.



6
7
8
# File 'lib/request_log_analyzer/mailer.rb', line 6

def data
  @data
end

#hostObject

Returns the value of attribute host.



6
7
8
# File 'lib/request_log_analyzer/mailer.rb', line 6

def host
  @host
end

#toObject

Returns the value of attribute to.



6
7
8
# File 'lib/request_log_analyzer/mailer.rb', line 6

def to
  @to
end

Instance Method Details

#<<(string) ⇒ Object



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

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
# 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.to_s}"
  content_type= "Content-Type: #{@content_type}" if @content_type
msg = <<END_OF_MESSAGE
From: #{from_alias} <#{from}>
To: #{to_alias} <#{@to}>
Subject: #{subject}
#{content_type}

#{@data.to_s}
END_OF_MESSAGE

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

  return [msg, from, to] 
end

#puts(string) ⇒ Object



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

def puts string
  data << string
end