class Crosby::Exporter

Constants

WRITE_MODE

Attributes

mail[R]

Public Class Methods

new(mailer, method, uuid = SecureRandom.hex(8), *args) click to toggle source
# File lib/crosby/exporter.rb, line 11
def initialize(mailer, method, uuid = SecureRandom.hex(8), *args)
  @args = args
  @mailer = mailer
  @method = method
  @uuid = uuid
  @version = Crosby.action_mailer_version

  config = Crosby.config
  @path = config[:export_path]
  @app_name = config[:app_name]
end

Public Instance Methods

export() click to toggle source
# File lib/crosby/exporter.rb, line 23
def export
  create_mail

  return false if ENV["SKIP_CROSBY"]

  ensure_path_exists
  write
  true
end
to_s() click to toggle source
# File lib/crosby/exporter.rb, line 33
def to_s
  create_mail unless mail

  [
    "TO: #{mail.to}",
    "CC: #{mail.cc}",
    "BCC: #{mail.bcc}",
    "REPLY TO: #{mail.reply_to}",
    "FROM: #{mail.from}",
    "HTML:",
    commonize_html(mail.html_part.body.raw_source),
    "TEXT:",
    commonize_text(mail.text_part.body.raw_source)
  ].join("\n")
end

Private Instance Methods

commonize_html(str) click to toggle source
# File lib/crosby/exporter.rb, line 51
def commonize_html(str)
  commonize_text(str)
  compact(Nokogiri::HTML::Document.parse(commonize_text(str)).to_s)
end
commonize_text(str) click to toggle source
# File lib/crosby/exporter.rb, line 56
def commonize_text(str)
  str
    .gsub(/([^=])=\n/, '\1') # single quote required for encoding
    .gsub(/=3D/, "=")
end
compact(str) click to toggle source
# File lib/crosby/exporter.rb, line 62
def compact(str)
  str
    .each_line
    .reject(&:blank?)
    .map{ |line|
      line
        .gsub(/\s+/, " ")
        .gsub(/^ /, "")
        .gsub(/ $/, "\n")
        .gsub(/></, ">\n<")
    }
    .join
end
create_mail() click to toggle source
# File lib/crosby/exporter.rb, line 76
def create_mail
  method = @method

  case @version::MAJOR
  when 2
    method = "create_#{method}"
  when 3
    # supported, no additional action required
  when 4
    # supported, no additional action required
  else
    raise Crosby::UnsupportedVersion, "We do not yet support version " \
      "#{@version::STRING} of ActionMailer"
  end

  @mail = @mailer.send(method, *@args)
end
ensure_path_exists() click to toggle source
# File lib/crosby/exporter.rb, line 94
def ensure_path_exists
  @dir = Dir.new(FileUtils.mkdir_p(@path).first)
end
file(&block) click to toggle source
# File lib/crosby/exporter.rb, line 98
def file(&block)
  File.open(filename, WRITE_MODE, &block)
end
filename() click to toggle source
# File lib/crosby/exporter.rb, line 102
def filename
  File.join(@dir, "#{@mailer.name}.#{@method}.#{@app_name}.#{@uuid}.crosby")
end
write() click to toggle source
# File lib/crosby/exporter.rb, line 106
def write
  file do |f|
    f.write(to_s)
  end
end