class Sisimai::Mail::STDIN

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

Attributes

handle[RW]
name[R]

Imported from p5-Sisimail/lib/Sisimai/Mail/STDIN.pm :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

offset[RW]
path[R]

Imported from p5-Sisimail/lib/Sisimai/Mail/STDIN.pm :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

size[R]

Imported from p5-Sisimail/lib/Sisimai/Mail/STDIN.pm :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

Public Class Methods

new(stdin = $stdin) click to toggle source

Constructor of Sisimai::Mail::STDIN @param [IO::STDIN] stdin Standard-In @return [Sisimai::Mail::STDIN] Object

# File lib/sisimai/mail/stdin.rb, line 17
def initialize(stdin = $stdin)
  raise 'is not an IO object' unless stdin.is_a?(IO)

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

Public Instance Methods

read() click to toggle source

Mbox reader, works as a iterator. @return [String] Contents of mbox

# File lib/sisimai/mail/stdin.rb, line 28
def read
  readhandle = self.handle
  readbuffer = ''

  if readhandle
    return nil if readhandle.closed?
  end

  begin
    readhandle = STDIN unless readhandle
    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