Class: Mailer

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

Defined Under Namespace

Classes: SettingNotFound, SettingUncompleteError

Constant Summary collapse

SETTING_FILE =
"mail_setting.yaml"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMailer

Returns a new instance of Mailer.



37
38
39
40
# File 'lib/mailer.rb', line 37

def initialize
  @options = {}
  @error_message = ""
end

Instance Attribute Details

#error_messageObject (readonly)

Returns the value of attribute error_message.



18
19
20
# File 'lib/mailer.rb', line 18

def error_message
  @error_message
end

Class Method Details

.createObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/mailer.rb', line 20

def self.create
  this = instance
  this.clear
  setting_file_path = File.join(Narou.get_root_dir, SETTING_FILE)
  if File.exists?(setting_file_path)
    options = YAML.load_file(setting_file_path)
    unless options.delete(:complete)
      raise SettingUncompleteError, "設定ファイルの書き換えが終了していないようです。\n" +
                                    "設定ファイルは #{setting_file_path} にあります"
    end
    this.options = options
  else
    raise SettingNotFound
  end
  this
end

Instance Method Details

#clearObject



42
43
44
# File 'lib/mailer.rb', line 42

def clear
  @options.clear
end

#options=(options) ⇒ Object



46
47
48
# File 'lib/mailer.rb', line 46

def options=(options)
  @options.merge!(options)
end

#send(message, attached_file_path = nil) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/mailer.rb', line 50

def send(message, attached_file_path = nil)
  @error_message = ""
  params = @options.dup
  params[:body] = message
  params[:charset] = "UTF-8"
  params[:text_part_charset] = "UTF-8"
  if attached_file_path
    params[:attachments] = { File.basename(attached_file_path) => File.binread(attached_file_path) }
  end
  begin
    Pony.mail(params)
  rescue StandardError => e
    @error_message = e.message
    return false
  end
  true
end