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.



75
76
77
78
79
# File 'lib/mbox/mail.rb', line 75

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

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



73
74
75
# File 'lib/mbox/mail.rb', line 73

def content
  @content
end

#headersObject (readonly)

Returns the value of attribute headers.



73
74
75
# File 'lib/mbox/mail.rb', line 73

def headers
  @headers
end

#metadataObject (readonly)

Returns the value of attribute metadata.



73
74
75
# File 'lib/mbox/mail.rb', line 73

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

#fromObject



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

def from
	.from.first.name
end

#inspectObject



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

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

#save_to(path) ⇒ Object



85
86
87
88
89
# File 'lib/mbox/mail.rb', line 85

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

#to_sObject



95
96
97
# File 'lib/mbox/mail.rb', line 95

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

#unread?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/mbox/mail.rb', line 91

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