class Spreewald::MailToPlaintextConverter

Attributes

mail[R]
type[R]

Public Class Methods

new(mail, type = '') click to toggle source
# File lib/spreewald_support/mail_to_plaintext_converter.rb, line 5
def initialize(mail, type = '')
  @mail = mail
  @type = type
end

Public Instance Methods

run() click to toggle source
# File lib/spreewald_support/mail_to_plaintext_converter.rb, line 10
def run
  body = ''
  if mail.multipart?
    body = if mail.html_part && type != 'plain-text'
      text_from_html(mail.html_part.body.to_s)
    elsif mail.text_part && type != 'HTML'
      mail.text_part.body.to_s
    else
      mail.body.to_s
    end
  else
    if body_text_html?
      body = text_from_html(mail.body.to_s)
    else
      body = mail.body.to_s
    end
  end
  body.gsub("\r\n", "\n") # The mail gem (>= 2.7.1) switched from \n to \r\n line breaks (LF to CRLF) in plain text mails.
end

Private Instance Methods

body_text_html?() click to toggle source
# File lib/spreewald_support/mail_to_plaintext_converter.rb, line 34
def body_text_html?
  mail.body.to_s.include? "<html>"
end
text_from_html(html) click to toggle source
# File lib/spreewald_support/mail_to_plaintext_converter.rb, line 38
def text_from_html(html)
  Nokogiri::HTML(html).at_css('body').text.gsub(/[\r\n]+/, "\n")
end