Class: Threatinator::Parsers::CSV::Parser

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

Overview

Parses an IO, yielding a record with a CSV::Row.

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):

  • :row_separator (String, :auto)

    A string that represent the row separator. Identical to ::CSV.new’s :row_sep.

  • :col_separator (String)

    A string that represents the column separator. Identical to ::CSV.new’s :col_sep.

  • :headers (Array<String>, :first_row, true, false)

    The header configuration. Identical to ::CSV.new’s :headers.

  • :csv_opts (Hash)

    A hash of options that will be passed to Ruby’s CSV.new.

See Also:

  • CSV


22
23
24
25
26
27
28
29
# File 'lib/threatinator/parsers/csv/parser.rb', line 22

def initialize(opts = {})
  @csv_opts = {}.merge(opts.delete(:csv_opts) || {})
  @row_separator = opts.delete(:row_separator)
  @col_separator = opts.delete(:col_separator)
  @headers = opts.delete(:headers)

  super(opts)
end

Instance Attribute Details

#col_separatorObject (readonly)

Returns the value of attribute col_separator.



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

def col_separator
  @col_separator
end

#csv_optsObject (readonly)

Returns the value of attribute csv_opts.



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

def csv_opts
  @csv_opts
end

#headersObject (readonly)

Returns the value of attribute headers.



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

def headers
  @headers
end

#row_separatorObject (readonly)

Returns the value of attribute row_separator.



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

def row_separator
  @row_separator
end

Instance Method Details

#==(other) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/threatinator/parsers/csv/parser.rb', line 31

def ==(other)
  @csv_opts == other.csv_opts &&
    @row_separator == other.row_separator &&
    @col_separator == other.col_separator &&
    @headers == other.headers &&
    super(other)
end

#_build_csv_optsObject



39
40
41
42
43
44
45
46
# File 'lib/threatinator/parsers/csv/parser.rb', line 39

def _build_csv_opts
  opts = {}.merge(@csv_opts)
  opts[:return_headers] = true
  opts[:row_sep] = @row_separator unless @row_separator.nil?
  opts[:col_sep] = @col_separator unless @col_separator.nil?
  opts[:headers] = @headers unless @headers.nil?
  opts
end

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

Parameters:

  • io (IO)

Yields:

  • (record)

    Gives one line to the block

Yield Parameters:

  • record (Record)

    a record



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/threatinator/parsers/csv/parser.rb', line 51

def run(io)
  lineno = 1
  previous_pos = io.pos
  csv = ::CSV.new(io, _build_csv_opts())
  csv.each do |row|
    begin
      if row.kind_of?(::CSV::Row)
        next if row.header_row?
        row = row.to_hash
      end

      yield Record.new(row, 
                       line_number: lineno, 
                       pos_start: previous_pos, 
                       pos_end: io.pos)

    ensure
      previous_pos = io.pos
      lineno += 1
    end
  end
  nil
end