Class: Sisimai::Mail::Mbox
- Inherits:
-
Object
- Object
- Sisimai::Mail::Mbox
- Defined in:
- lib/sisimai/mail/mbox.rb
Overview
Sisimai::Mail::Mbox is a mailbox file (UNIX mbox) reader.
Instance Attribute Summary collapse
-
#dir ⇒ Object
readonly
:dir [String] Directory name of the mbox :file [String] File name of the mbox :path [String] Path to mbox :size [Integer] File size of the mbox :offset [Integer] Offset position for seeking :handle [IO::File] File handle.
-
#file ⇒ Object
readonly
:dir [String] Directory name of the mbox :file [String] File name of the mbox :path [String] Path to mbox :size [Integer] File size of the mbox :offset [Integer] Offset position for seeking :handle [IO::File] File handle.
-
#handle ⇒ Object
Returns the value of attribute handle.
-
#offset ⇒ Object
Returns the value of attribute offset.
-
#path ⇒ Object
readonly
:dir [String] Directory name of the mbox :file [String] File name of the mbox :path [String] Path to mbox :size [Integer] File size of the mbox :offset [Integer] Offset position for seeking :handle [IO::File] File handle.
-
#size ⇒ Object
readonly
:dir [String] Directory name of the mbox :file [String] File name of the mbox :path [String] Path to mbox :size [Integer] File size of the mbox :offset [Integer] Offset position for seeking :handle [IO::File] File handle.
Instance Method Summary collapse
-
#initialize(argv1) ⇒ Sisimai::Mail::Mbox
constructor
Constructor of Sisimai::Mail::Mbox.
-
#read ⇒ String
Mbox reader, works as an iterator.
Constructor Details
#initialize(argv1) ⇒ Sisimai::Mail::Mbox
Constructor of Sisimai::Mail::Mbox
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/sisimai/mail/mbox.rb', line 18 def initialize(argv1) raise Errno::ENOENT if File.exist?(argv1) == false raise 'is not a file' if File.ftype(argv1) != 'file' @path = argv1 @dir = File.dirname(argv1) @file = File.basename(argv1) @size = File.size(argv1) @offset = 0 @handle = File.open(argv1, 'r:UTF-8') end |
Instance Attribute Details
#dir ⇒ Object (readonly)
:dir [String] Directory name of the mbox :file [String] File name of the mbox :path [String] Path to mbox :size [Integer] File size of the mbox :offset [Integer] Offset position for seeking :handle [IO::File] File handle
11 12 13 |
# File 'lib/sisimai/mail/mbox.rb', line 11 def dir @dir end |
#file ⇒ Object (readonly)
:dir [String] Directory name of the mbox :file [String] File name of the mbox :path [String] Path to mbox :size [Integer] File size of the mbox :offset [Integer] Offset position for seeking :handle [IO::File] File handle
11 12 13 |
# File 'lib/sisimai/mail/mbox.rb', line 11 def file @file end |
#handle ⇒ Object
Returns the value of attribute handle.
12 13 14 |
# File 'lib/sisimai/mail/mbox.rb', line 12 def handle @handle end |
#offset ⇒ Object
Returns the value of attribute offset.
12 13 14 |
# File 'lib/sisimai/mail/mbox.rb', line 12 def offset @offset end |
#path ⇒ Object (readonly)
:dir [String] Directory name of the mbox :file [String] File name of the mbox :path [String] Path to mbox :size [Integer] File size of the mbox :offset [Integer] Offset position for seeking :handle [IO::File] File handle
11 12 13 |
# File 'lib/sisimai/mail/mbox.rb', line 11 def path @path end |
#size ⇒ Object (readonly)
:dir [String] Directory name of the mbox :file [String] File name of the mbox :path [String] Path to mbox :size [Integer] File size of the mbox :offset [Integer] Offset position for seeking :handle [IO::File] File handle
11 12 13 |
# File 'lib/sisimai/mail/mbox.rb', line 11 def size @size end |
Instance Method Details
#read ⇒ String
Mbox reader, works as an iterator.
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 |
# File 'lib/sisimai/mail/mbox.rb', line 32 def read return nil if self.offset >= self.size seekoffset = self.offset || 0 filehandle = self.handle readbuffer = '' frombuffer = '' begin seekoffset = 0 if seekoffset < 0 filehandle.seek(seekoffset, 0) filehandle.each_line do |r| # Read the UNIX mbox file from 'From ' to the next 'From ' if r.start_with?('From ') && !readbuffer.empty? frombuffer = r break end readbuffer += r end seekoffset = filehandle.pos - frombuffer.bytesize frombuffer = '' self.offset = seekoffset filehandle.close if self.offset >= self.size end return readbuffer.to_s end |