Class: Lazylead::FilePostman

Inherits:
Object
  • Object
show all
Defined in:
lib/lazylead/postman.rb

Overview

A postman that sends emails to the html file. Use ENV to set a root directory. By default

ENV['file_postman_dir'] = "."

Instance Method Summary collapse

Constructor Details

#initialize(log = Log.new, env = ENV.to_h) ⇒ FilePostman

Returns a new instance of FilePostman.



109
110
111
112
# File 'lib/lazylead/postman.rb', line 109

def initialize(log = Log.new, env = ENV.to_h)
  @log = log
  @env = env
end

Instance Method Details

#filename(opts) ⇒ Object

Assemble file name where email to be print



134
135
136
137
138
# File 'lib/lazylead/postman.rb', line 134

def filename(opts)
  dir = @env.fetch("file_postman_dir", ".")
  FileUtils.mkdir_p(dir)
  File.join(dir, Zaru.sanitize!("#{Time.now.nsec}-#{opts['subject']}.html"))
end

#send(opts) ⇒ Object

Send an email.

:opts

the mail configuration like to, from, cc, subject, template.



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/lazylead/postman.rb', line 116

def send(opts)
  if opts.msg_to.empty?
    @log.warn "ll-015: Email can't be sent as 'to' is empty, more: '#{opts}'"
    ""
  else
    file = filename(opts)
    File.open(file, "w") do |f|
      f.write "<!-- to=#{opts.msg_to}, from=#{opts.msg_from}, cc=#{opts.msg_cc}, " \
              "subject=#{opts['subject']}, attachments=#{opts.msg_attachments} -->"
      f.write opts.msg_body
    end
    @log.debug "Mail '#{opts['subject']}' for #{opts.msg_to} sent to " \
               "'#{file.to_s.colorize(:light_blue)}'"
    file
  end
end