class RakeMailer::FileWriter

Public Class Methods

new(emails = nil) click to toggle source
# File lib/rake_mailer.rb, line 9
def initialize(emails = nil)
  load_yml
  set_email_config(emails)
  set_file_config
  create_file_path
  open_file         
end

Public Instance Methods

close() click to toggle source
# File lib/rake_mailer.rb, line 26
def close
  begin
    @file.close
  rescue Exception => e
    puts "ERROR: Block in close. Rescued. Will attempt to send the mail. Could close the file => #{e.message}"
  end
  send_email
end
file_writer(line) click to toggle source
# File lib/rake_mailer.rb, line 17
def file_writer(line)
  begin
    @file.write(line)
    @file.write("\n")
  rescue Exception => e
    puts "ERROR: Block in file_writer. Rescued. Could not write in the file => #{e.message}"
  end  
end

Private Instance Methods

create_file_path() click to toggle source
# File lib/rake_mailer.rb, line 62
def create_file_path
  begin
    FileUtils::mkdir_p(@file_path)
  rescue Exception => e
    raise "ERROR: Block in create_file_path. Could not create folder => #{e.message}"
  end
end
load_yml() click to toggle source
# File lib/rake_mailer.rb, line 36
def load_yml
  yml_file = YAML.load_file("#{Rails.root}/config/rake_mailer.yml")
  if yml_file && yml_file[Rails.env]
    @rake_mailer_constants = yml_file[Rails.env]
  else
    raise 'ERROR: gem rake_mailer => configuration file is incorrect'
  end
end
open_file() click to toggle source
# File lib/rake_mailer.rb, line 70
def open_file
  begin
    @file = open(@file_location, 'w')
  rescue Exception => e
    raise "ERROR: Block in open_file. Could not open the file => #{e.message}"
  end
end
send_email() click to toggle source
# File lib/rake_mailer.rb, line 78
def send_email
  if @from.present? && @emails.present? && (@from.is_a? String) && ((@emails.is_a? String) || (@emails.is_a? Array))
    RakeMailer::MailIt.custom_text_email(@from, @emails, @file_location, @filename, @subject, @display_system_info).deliver_now
  end
end
set_display_system_info_config() click to toggle source
# File lib/rake_mailer.rb, line 52
def set_display_system_info_config
  @display_system_info = @rake_mailer_constants['display_system_info'].nil? ? true : @rake_mailer_constants['display_system_info']
end
set_email_config(emails) click to toggle source
# File lib/rake_mailer.rb, line 45
def set_email_config(emails)
  @emails = emails || @rake_mailer_constants['emails']
  @from = @rake_mailer_constants['from']
  @subject = "[Rake Mailer] Report for #{Rake.application.top_level_tasks.first}"
  set_display_system_info_config
end
set_file_config() click to toggle source
# File lib/rake_mailer.rb, line 56
def set_file_config
  @filename = Time.now.to_i.to_s + "_#{Rake.application.top_level_tasks.first}" + '.txt'
  @file_path = @rake_mailer_constants['file_path'] || 'tmp/rake_mailer'
  @file_location = File.join(Rails.root, (@file_path), @filename)
end