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.



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

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

Instance Attribute Details

#error_messageObject (readonly)

Returns the value of attribute error_message.



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

def error_message
  @error_message
end

Class Method Details

.createObject



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

def self.create
  this = instance
  this.clear
  setting_file_path = File.join(Narou.root_dir, SETTING_FILE)
  if File.exist?(setting_file_path)
    options = YAML.unsafe_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



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

def clear
  @options.clear
end

#extname(path) ⇒ Object

.kepub.epub を考慮する必要がある



73
74
75
76
77
78
79
# File 'lib/mailer.rb', line 73

def extname(path)
  if File.basename(path) =~ /\A[^.]+(.+)/
    $1
  else
    ""
  end
end

#options=(options) ⇒ Object



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

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

#send(id, message, attached_file_path = nil) ⇒ Object



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

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