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


83
84
85
86
87
# File 'lib/mbox/mail.rb', line 83

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

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content


81
82
83
# File 'lib/mbox/mail.rb', line 81

def content
  @content
end

#headersObject (readonly)

Returns the value of attribute headers


81
82
83
# File 'lib/mbox/mail.rb', line 81

def headers
  @headers
end

#metadataObject (readonly)

Returns the value of attribute metadata


81
82
83
# File 'lib/mbox/mail.rb', line 81

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
# 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)

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

	return if !line || line.empty?

	# metadata parsing
	.parse_from line

	until input.eof? || (line = input.readline).match(options[:separator])
		break unless line.match(/^>+/)

		.parse_from line
	end

	# headers parsing
	current = ''
	begin
		break if line.strip.empty?

		current << line
		if line[0..12] == "Delivered-To:"
			.parse_to line
			next
		end
		if line[0..7] == "Subject:"
			.parse_subject line
		end
	end until input.eof? || (line = input.readline).match(options[:separator])
	headers.parse(current)

	# content parsing
	current = ''
	until input.eof? || (line = input.readline).match(options[:separator])
		next if options[:headers_only]

		current << line
	end

	unless options[:headers_only]
		content.parse(current.chomp)
	end

	# put the last separator back in its place
	if !input.eof? && line
		input.seek(-line.length, IO::SEEK_CUR)
	end

	Mail.new(, headers, content)
end

Instance Method Details

#dateObject


93
94
95
# File 'lib/mbox/mail.rb', line 93

def date
	.from.first.date
end

#fromObject


89
90
91
# File 'lib/mbox/mail.rb', line 89

def from
	.from.first.name
end

#inspectObject


119
120
121
# File 'lib/mbox/mail.rb', line 119

def inspect
	"#<Mail:#{from}>"
end

#save_to(path) ⇒ Object


105
106
107
108
109
# File 'lib/mbox/mail.rb', line 105

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

#subjectObject


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

def subject
	.subject.first
end

#toObject


97
98
99
# File 'lib/mbox/mail.rb', line 97

def to
	.to.first
end

#to_sObject


115
116
117
# File 'lib/mbox/mail.rb', line 115

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

#unread?Boolean


111
112
113
# File 'lib/mbox/mail.rb', line 111

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