Class: Mbox::Mail

Inherits:
Object
  • Object
show all
Defined in:
lib/mbox/mail.rb,
lib/mbox/mail/file.rb,
lib/mbox/mail/content.rb,
lib/mbox/mail/headers.rb,
lib/mbox/mail/metadata.rb,
lib/mbox/mail/headers/status.rb,
lib/mbox/mail/headers/content_type.rb

Defined Under Namespace

Classes: Content, File, Headers, Metadata

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(metadata, headers, content) ⇒ Mail

Returns a new instance of Mail.



101
102
103
104
105
# File 'lib/mbox/mail.rb', line 101

def initialize (, headers, content)
	@metadata = 
	@headers  = headers
	@content  = content
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



99
100
101
# File 'lib/mbox/mail.rb', line 99

def content
  @content
end

#headersObject (readonly)

Returns the value of attribute headers.



99
100
101
# File 'lib/mbox/mail.rb', line 99

def headers
  @headers
end

#metadataObject (readonly)

Returns the value of attribute metadata.



99
100
101
# File 'lib/mbox/mail.rb', line 99

def 
  @metadata
end

Class Method Details

.parse(input, options = {}) ⇒ Object



27
28
29
30
31
32
33
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/mbox/mail.rb', line 27

def self.parse (input, options = {})
	 = Mbox::Mail::Metadata.new
	headers  = Mbox::Mail::Headers.new
	content  = Mbox::Mail::Content.new(headers)

	inside = {
		metadata: true,
		headers:  false,
		content:  false
	}

	last = {
		line:  '',
		stuff: ''
	}

	next until input.eof? || (line = input.readline).match(options[:separator])

	return if !line || line.empty?

	.parse_from line

	until input.eof? || ((line = input.readline).match(options[:separator]) && last[:line].empty?)
		if inside[:metadata]
			if line.match(/^>+/)
				.parse_from line
			else
				inside[:metadata]    = false
				inside[:headers] = true

				last[:line] = line.chomp

				next
			end
		elsif inside[:headers]
			if line.strip.empty?
				inside[:headers] = false
				inside[:content] = true

				headers.parse(last[:stuff])

				last[:line]  = line.chomp
				last[:stuff] = ''

				next
			end

			last[:stuff] << line
		elsif inside[:content]
			if options[:headers_only]
				last[:line] = line.chomp

				next
			end

			last[:stuff] << line
		end

		last[:line] = line.chomp
	end

	unless last[:stuff].empty?
		content.parse(last[:stuff])
	end

	if !input.eof? && line
		input.seek(-line.length, IO::SEEK_CUR)
	end

	Mail.new(, headers, content)
end

Instance Method Details

#inspectObject



121
122
123
# File 'lib/mbox/mail.rb', line 121

def inspect
	"#<Mail:#{headers['From']}>"
end

#save_to(path) ⇒ Object



107
108
109
110
111
# File 'lib/mbox/mail.rb', line 107

def save_to (path)
	File.open(path, 'w') {|f|
		f.write to_s
	}
end

#to_sObject



117
118
119
# File 'lib/mbox/mail.rb', line 117

def to_s
	"#{headers}\n#{content}"
end

#unread?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/mbox/mail.rb', line 113

def unread?
	!headers[:status].read? rescue true
end