Class: Sisimai::Mail::STDIN

Inherits:
Object
  • Object
show all
Defined in:
lib/sisimai/mail/stdin.rb

Overview

Sisimai::Mail::STDIN is a reader for getting contents of each email from STDIN

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stdin = $stdin) ⇒ Sisimai::Mail::STDIN

Constructor of Sisimai::Mail::STDIN

Parameters:

  • stdin (IO::STDIN) (defaults to: $stdin)

    Standard-In



15
16
17
18
19
20
21
22
# File 'lib/sisimai/mail/stdin.rb', line 15

def initialize(stdin = $stdin)
  raise 'is not an IO object' if stdin.is_a?(IO) == false

  @path   = '<STDIN>'
  @size   = nil
  @offset = 0
  @handle = stdin
end

Instance Attribute Details

#handleObject

Returns the value of attribute handle.



10
11
12
# File 'lib/sisimai/mail/stdin.rb', line 10

def handle
  @handle
end

#nameObject (readonly)

:path [String] Fixed string “<STDIN>” :size [Integer] Data size which has been read :offset [Integer] The number of emails which have neen read :handle [IO::File] File handle



9
10
11
# File 'lib/sisimai/mail/stdin.rb', line 9

def name
  @name
end

#offsetObject

Returns the value of attribute offset.



10
11
12
# File 'lib/sisimai/mail/stdin.rb', line 10

def offset
  @offset
end

#pathObject (readonly)

:path [String] Fixed string “<STDIN>” :size [Integer] Data size which has been read :offset [Integer] The number of emails which have neen read :handle [IO::File] File handle



9
10
11
# File 'lib/sisimai/mail/stdin.rb', line 9

def path
  @path
end

#sizeObject (readonly)

:path [String] Fixed string “<STDIN>” :size [Integer] Data size which has been read :offset [Integer] The number of emails which have neen read :handle [IO::File] File handle



9
10
11
# File 'lib/sisimai/mail/stdin.rb', line 9

def size
  @size
end

Instance Method Details

#readString

Mbox reader, works as an iterator.

Returns:

  • (String)

    Contents of mbox



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/sisimai/mail/stdin.rb', line 26

def read
  readhandle = self.handle
  readbuffer = ''

  if readhandle
    return nil if readhandle.closed?
  end

  begin
    readhandle = STDIN if readhandle.nil?
    while r = readhandle.gets
      break if readbuffer.size > 0 && r.start_with?('From ')
      readbuffer << r
    end
  ensure
    readhandle.close
  end

  self.size   += readbuffer.size
  self.offset += 1
  return readbuffer
end