Class: LetterOpener::Message
- Inherits:
-
Object
- Object
- LetterOpener::Message
- Defined in:
- lib/letter_opener/message.rb
Instance Attribute Summary collapse
-
#mail ⇒ Object
readonly
Returns the value of attribute mail.
Class Method Summary collapse
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #auto_link(text) ⇒ Object
- #bcc ⇒ Object
- #body ⇒ Object
- #cc ⇒ Object
- #content_type ⇒ Object
- #encoding ⇒ Object
- #filepath ⇒ Object
- #from ⇒ Object
- #h(content) ⇒ Object
-
#initialize(location, mail, part = nil) ⇒ Message
constructor
A new instance of Message.
- #render ⇒ Object
- #reply_to ⇒ Object
- #sender ⇒ Object
- #template ⇒ Object
- #to ⇒ Object
- #type ⇒ Object
Constructor Details
#initialize(location, mail, part = nil) ⇒ Message
Returns a new instance of Message.
16 17 18 19 20 21 |
# File 'lib/letter_opener/message.rb', line 16 def initialize(location, mail, part = nil) @location = location @mail = mail @part = part @attachments = [] end |
Instance Attribute Details
#mail ⇒ Object (readonly)
Returns the value of attribute mail.
5 6 7 |
# File 'lib/letter_opener/message.rb', line 5 def mail @mail end |
Class Method Details
.rendered_messages(location, mail) ⇒ Object
7 8 9 10 11 12 13 14 |
# File 'lib/letter_opener/message.rb', line 7 def self.(location, mail) = [] << new(location, mail, mail.html_part) if mail.html_part << new(location, mail, mail.text_part) if mail.text_part << new(location, mail) if .empty? .each(&:render) .sort end |
Instance Method Details
#<=>(other) ⇒ Object
112 113 114 115 |
# File 'lib/letter_opener/message.rb', line 112 def <=>(other) order = %w[rich plain] order.index(type) <=> order.index(other.type) end |
#auto_link(text) ⇒ Object
102 103 104 105 106 |
# File 'lib/letter_opener/message.rb', line 102 def auto_link(text) text.gsub(URI.regexp(%W[https http])) do |link| "<a href=\"#{ link }\">#{ link }</a>" end end |
#bcc ⇒ Object
86 87 88 |
# File 'lib/letter_opener/message.rb', line 86 def bcc @bcc ||= Array(@mail['bcc']).join(", ") end |
#body ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/letter_opener/message.rb', line 58 def body @body ||= begin body = (@part && @part.body || @mail.body).to_s mail..each do || body.gsub!(.url, "attachments/#{.filename}") end body end end |
#cc ⇒ Object
82 83 84 |
# File 'lib/letter_opener/message.rb', line 82 def cc @cc ||= Array(@mail['cc']).join(", ") end |
#content_type ⇒ Object
54 55 56 |
# File 'lib/letter_opener/message.rb', line 54 def content_type @part && @part.content_type || @mail.content_type end |
#encoding ⇒ Object
98 99 100 |
# File 'lib/letter_opener/message.rb', line 98 def encoding body.respond_to?(:encoding) ? body.encoding : "utf-8" end |
#filepath ⇒ Object
50 51 52 |
# File 'lib/letter_opener/message.rb', line 50 def filepath File.join(@location, "#{type}.html") end |
#from ⇒ Object
70 71 72 |
# File 'lib/letter_opener/message.rb', line 70 def from @from ||= Array(@mail['from']).join(", ") end |
#h(content) ⇒ Object
108 109 110 |
# File 'lib/letter_opener/message.rb', line 108 def h(content) CGI.escapeHTML(content) end |
#render ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/letter_opener/message.rb', line 23 def render FileUtils.mkdir_p(@location) if mail..any? = File.join(@location, 'attachments') FileUtils.mkdir_p() mail..each do || filename = .filename.gsub(/[^\w.]/, '_') path = File.join(, filename) unless File.exists?(path) # true if other parts have already been rendered File.open(path, 'wb') { |f| f.write(.body.raw_source) } end @attachments << [.filename, "attachments/#{URI.escape(filename)}"] end end File.open(filepath, 'w') do |f| f.write ERB.new(template).result(binding) end end |
#reply_to ⇒ Object
90 91 92 |
# File 'lib/letter_opener/message.rb', line 90 def reply_to @reply_to ||= Array(@mail['reply-to']).join(", ") end |
#sender ⇒ Object
74 75 76 |
# File 'lib/letter_opener/message.rb', line 74 def sender @sender ||= Array(@mail['sender']).join(", ") end |
#template ⇒ Object
46 47 48 |
# File 'lib/letter_opener/message.rb', line 46 def template File.read(File.("../message.html.erb", __FILE__)) end |
#to ⇒ Object
78 79 80 |
# File 'lib/letter_opener/message.rb', line 78 def to @to ||= Array(@mail['to']).join(", ") end |
#type ⇒ Object
94 95 96 |
# File 'lib/letter_opener/message.rb', line 94 def type content_type =~ /html/ ? "rich" : "plain" end |