Class: Mail
- Inherits:
-
Object
- Object
- Defined in:
- lib/mailread.rb
Overview
The Mail class represents an internet mail message (as per RFC822, RFC2822) with headers and a body.
Instance Method Summary collapse
-
#[](field) ⇒ Object
Return the header corresponding to
field
. -
#body ⇒ Object
Return the message body as an Array of lines.
-
#header ⇒ Object
Return the headers as a Hash.
-
#initialize(f) ⇒ Mail
constructor
Create a new Mail where
f
is either a stream which responds to gets(), or a path to a file.
Constructor Details
#initialize(f) ⇒ Mail
Create a new Mail where f
is either a stream which responds to gets(), or a path to a file. If f
is a path it will be opened.
The whole message is read so it can be made available through the #header, #[] and #body methods.
The "From " line is ignored if the mail is in mbox format.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/mailread.rb', line 12 def initialize(f) unless defined? f.gets f = open(f, "r") opened = true end @header = {} @body = [] begin while line = f.gets() line.chop! next if /^From /=~line # skip From-line break if /^$/=~line # end of header if /^(\S+?):\s*(.*)/=~line (attr = $1).capitalize! @header[attr] = $2 elsif attr line.sub!(/^\s*/, '') @header[attr] += "\n" + line end end return unless line while line = f.gets() break if /^From /=~line @body.push(line) end ensure f.close if opened end end |
Instance Method Details
#[](field) ⇒ Object
Return the header corresponding to field
.
Matching is case-insensitive.
59 60 61 |
# File 'lib/mailread.rb', line 59 def [](field) @header[field.capitalize] end |
#body ⇒ Object
Return the message body as an Array of lines
52 53 54 |
# File 'lib/mailread.rb', line 52 def body return @body end |
#header ⇒ Object
Return the headers as a Hash.
47 48 49 |
# File 'lib/mailread.rb', line 47 def header return @header end |