class PhisherPhinder::MailParser::BodyParser

Public Instance Methods

parse(contents) click to toggle source
# File lib/phisher_phinder/mail_parser/body_parser.rb, line 7
def parse(contents)
  mail = ::Mail.new(contents)
  aggregate_body_parts(mail)
end

Private Instance Methods

aggregate_body_parts(mail) click to toggle source
# File lib/phisher_phinder/mail_parser/body_parser.rb, line 14
def aggregate_body_parts(mail)
  accumulator = {text: [], html: []}
  if mail.body.parts.any?
    collapse_content(mail.body, accumulator)
    accumulator.inject({}) { |accum, (type, parts)| accum.merge(type => parts.join) }
  else
    {
      text: mail.body.decoded,
      html: nil
    }
  end
end
collapse_content(part, accumulator) click to toggle source
# File lib/phisher_phinder/mail_parser/body_parser.rb, line 27
def collapse_content(part, accumulator)
  if part.parts.any?
    part.parts.each { |p| collapse_content(p, accumulator) }
  elsif part.content_type =~ %r{text/plain}
    accumulator[:text] << part.decoded
  elsif part.content_type =~ %r{text/html}
    accumulator[:html] << part.decoded
  end
end