Class: LetterOpener::Message
- Inherits:
-
Object
- Object
- LetterOpener::Message
- Defined in:
- lib/letter_opener/message.rb
Constant Summary collapse
- ERROR_MSG =
'%s or default configuration must be given'.freeze
Instance Attribute Summary collapse
-
#mail ⇒ Object
readonly
Returns the value of attribute mail.
Class Method Summary collapse
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #attachment_filename(attachment) ⇒ Object
- #auto_link(text) ⇒ Object
- #bcc ⇒ Object
- #body ⇒ Object
- #cc ⇒ Object
- #content_type ⇒ Object
- #encoding ⇒ Object
- #filepath ⇒ Object
- #from ⇒ Object
- #h(content) ⇒ Object
-
#initialize(mail, options = {}) ⇒ Message
constructor
A new instance of Message.
- #render ⇒ Object
- #reply_to ⇒ Object
- #sender ⇒ Object
- #template ⇒ Object
- #to ⇒ Object
- #type ⇒ Object
Constructor Details
#initialize(mail, options = {}) ⇒ Message
Returns a new instance of Message.
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/letter_opener/message.rb', line 21 def initialize(mail, = {}) @mail = mail @location = [:location] || LetterOpener.configuration.location @part = [:part] @template = [:message_template] || LetterOpener.configuration. @attachments = [] raise ArgumentError, ERROR_MSG % 'options[:location]' unless @location raise ArgumentError, ERROR_MSG % 'options[:message_template]' unless @template end |
Instance Attribute Details
#mail ⇒ Object (readonly)
Returns the value of attribute mail.
8 9 10 |
# File 'lib/letter_opener/message.rb', line 8 def mail @mail end |
Class Method Details
.rendered_messages(mail, options = {}) ⇒ Object
10 11 12 13 14 15 16 17 |
# File 'lib/letter_opener/message.rb', line 10 def self.(mail, = {}) = [] << new(mail, .merge(part: mail.html_part)) if mail.html_part << new(mail, .merge(part: mail.text_part)) if mail.text_part << new(mail, ) if .empty? .each(&:render) .sort end |
Instance Method Details
#<=>(other) ⇒ Object
125 126 127 128 |
# File 'lib/letter_opener/message.rb', line 125 def <=>(other) order = %w[rich plain] order.index(type) <=> order.index(other.type) end |
#attachment_filename(attachment) ⇒ Object
121 122 123 |
# File 'lib/letter_opener/message.rb', line 121 def () .filename.gsub(/[^\w\-.]/, '_') end |
#auto_link(text) ⇒ Object
111 112 113 114 115 |
# File 'lib/letter_opener/message.rb', line 111 def auto_link(text) text.gsub(URI::Parser.new.make_regexp(%W[https http])) do |link| "<a href=\"#{ link }\">#{ link }</a>" end end |
#bcc ⇒ Object
95 96 97 |
# File 'lib/letter_opener/message.rb', line 95 def bcc @bcc ||= Array(@mail['bcc']).join(", ") end |
#body ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/letter_opener/message.rb', line 67 def body @body ||= begin body = (@part || @mail).decoded mail..each do || body.gsub!(.url, "attachments/#{()}") end body end end |
#cc ⇒ Object
91 92 93 |
# File 'lib/letter_opener/message.rb', line 91 def cc @cc ||= Array(@mail['cc']).join(", ") end |
#content_type ⇒ Object
63 64 65 |
# File 'lib/letter_opener/message.rb', line 63 def content_type @part && @part.content_type || @mail.content_type end |
#encoding ⇒ Object
107 108 109 |
# File 'lib/letter_opener/message.rb', line 107 def encoding body.respond_to?(:encoding) ? body.encoding : "utf-8" end |
#filepath ⇒ Object
59 60 61 |
# File 'lib/letter_opener/message.rb', line 59 def filepath File.join(@location, "#{type}.html") end |
#from ⇒ Object
79 80 81 |
# File 'lib/letter_opener/message.rb', line 79 def from @from ||= Array(@mail['from']).join(", ") end |
#h(content) ⇒ Object
117 118 119 |
# File 'lib/letter_opener/message.rb', line 117 def h(content) CGI.escapeHTML(content) end |
#render ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/letter_opener/message.rb', line 32 def render FileUtils.mkdir_p(@location) if mail..any? = File.join(@location, 'attachments') FileUtils.mkdir_p() mail..each do || filename = () path = File.join(, filename) unless File.exist?(path) # true if other parts have already been rendered File.open(path, 'wb') { |f| f.write(.body.raw_source) } end @attachments << [.filename, "attachments/#{CGI.escape(filename)}"] end end File.open(filepath, 'w') do |f| f.write ERB.new(template).result(binding) end end |
#reply_to ⇒ Object
99 100 101 |
# File 'lib/letter_opener/message.rb', line 99 def reply_to @reply_to ||= Array(@mail['reply-to']).join(", ") end |
#sender ⇒ Object
83 84 85 |
# File 'lib/letter_opener/message.rb', line 83 def sender @sender ||= Array(@mail['sender']).join(", ") end |
#template ⇒ Object
55 56 57 |
# File 'lib/letter_opener/message.rb', line 55 def template File.read(File.("../templates/#{@template}.html.erb", __FILE__)) end |
#to ⇒ Object
87 88 89 |
# File 'lib/letter_opener/message.rb', line 87 def to @to ||= Array(@mail['to']).join(", ") end |
#type ⇒ Object
103 104 105 |
# File 'lib/letter_opener/message.rb', line 103 def type content_type =~ /html/ ? "rich" : "plain" end |