Class: MIME::MimeMessageParser

Inherits:
Object
  • Object
show all
Defined in:
lib/mime/mime_message_parser.rb

Class Method Summary collapse

Class Method Details

.parse(input_stream, content_type) ⇒ Object

Parses a mime message and returns an array of hashes for each part of the message The hash will contain the following:

  • :content_type - The content type of the message part

  • :content_id - The id of the content (useful for dealing with MTOM/XOP)

  • :content - The actual content of the message as a String



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/mime/mime_message_parser.rb', line 22

def self.parse(input_stream, content_type)
  parts = []
  mts = FixedMimeTokenStream.new()
  mts.parse_headless(input_stream, content_type)
  current_message = {}
  until(mts.state == MimeTokenStream::T_END_OF_STREAM)
    case mts.state
    when MimeTokenStream::T_FIELD
      if mts.field_name.eql?('Content-Type')
        current_message[:content_type] = mts.field_value.strip
      end
      if mts.field_name.eql?('Content-ID')
        current_message[:content_id] = mts.field_value.strip
      end
    when MimeTokenStream::T_BODY
      reader = java.io.LineNumberReader.new(mts.reader)
      content = ''
      while (line = reader.readLine)
        content << line
        content << "\n"
      end
      current_message[:content] = content
      parts << current_message
      current_message = {}
    end
    mts.next
  end
  parts
end