Class: Threatinator::Parsers::Getline::Parser

Inherits:
Threatinator::Parser show all
Defined in:
lib/threatinator/parsers/getline/parser.rb

Overview

Parses an IO, yielding each ‘line’ of data as deliniated by :separator. The text matching :separator will be included.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Threatinator::Parser

#eql?

Constructor Details

#initialize(opts = {}) ⇒ Parser

Returns a new instance of Parser.

Parameters:

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :separator (String) — default: "\n"

    A character that will be used to detect the end of a line.



15
16
17
18
19
20
21
# File 'lib/threatinator/parsers/getline/parser.rb', line 15

def initialize(opts = {})
  @separator = opts.delete(:separator) || "\n"
  unless @separator.length == 1
    raise ArgumentError.new(":separator must be exactly one character long")
  end
  super(opts)
end

Instance Attribute Details

#separatorObject (readonly)

Returns the value of attribute separator.



10
11
12
# File 'lib/threatinator/parsers/getline/parser.rb', line 10

def separator
  @separator
end

Instance Method Details

#==(other) ⇒ Object



23
24
25
26
# File 'lib/threatinator/parsers/getline/parser.rb', line 23

def ==(other)
  @separator == other.separator &&
    super(other)
end

#run(io) {|line| ... } ⇒ Object

Parameters:

  • io (IO)

    The IO to be parsed

Yields:

  • (line)

    Gives one line to the block

Yield Parameters:

  • line (String)

    a line from the IO stream.



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/threatinator/parsers/getline/parser.rb', line 31

def run(io)
  return enum_for(:each) unless block_given?
  lineno = 1
  while str = io.gets(@separator)
    return if str.nil?
    pos_start = io.pos - str.length
    yield Record.new(str, line_number: lineno, pos_start: pos_start, pos_end: io.pos)
    lineno += 1
  end
  nil
end