Class: Mbox::Mail::Content

Inherits:
Array
  • Object
show all
Defined in:
lib/mbox/mail/content.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(headers, content = [], attachments = []) ⇒ Content

Returns a new instance of Content



27
28
29
30
31
32
# File 'lib/mbox/mail/content.rb', line 27

def initialize (headers, content = [], attachments = [])
	@headers     = headers
	@attachments = attachments

	push *content
end

Instance Attribute Details

#attachmentsObject (readonly)

Returns the value of attribute attachments



25
26
27
# File 'lib/mbox/mail/content.rb', line 25

def attachments
  @attachments
end

#headersObject (readonly)

Returns the value of attribute headers



25
26
27
# File 'lib/mbox/mail/content.rb', line 25

def headers
  @headers
end

Instance Method Details

#parse(text, headers = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/mbox/mail/content.rb', line 34

def parse (text, headers = {})
	headers = @headers.merge(headers)
	type    = headers[:content_type]

	if type && type.mime && type.boundary && matches = type.mime.match(%r{multipart/(\w+)})
		text.sub(/^.*?--#{Regexp.escape(type.boundary)}\n/m, '').sub(/--#{Regexp.escape(type.boundary)}--$/m, '').split("--#{type.boundary}\n").each {|part|
			stream = StringIO.new(part)

			headers = ''
			until stream.eof? || (line = stream.readline).chomp.empty?
				headers << line
			end
			headers = Headers.parse(headers)

			content = !stream.eof? ? stream.readline : ''
			until stream.eof? 
				content << stream.readline
			end
			content.chomp!

			file = File.new(headers, content)

			if (headers[:content_disposition] || '').match(/^attachment/)
				attachments << file
			else
				self << file
			end
		}
	else
		self << File.new(headers, text)
	end

	self
end