class Threatinator::Parsers::CSV::Parser
Parses an IO, yielding a record with a CSV::Row.
Attributes
col_separator[R]
csv_opts[R]
headers[R]
row_separator[R]
Public Class Methods
new(opts = {})
click to toggle source
@param [Hash] opts @option opts [String, :auto] :row_separator A string that represent the row
separator. Identical to ::CSV.new's :row_sep.
@option opts [String] :col_separator A string that represents the column
separator. Identical to ::CSV.new's :col_sep.
@option opts [Array<String>, :first_row, true, false] :headers The header
configuration. Identical to ::CSV.new's :headers.
@option opts [Hash] :csv_opts A hash of options that will be passed to
Ruby's CSV.new.
@see ::CSV
Calls superclass method
Threatinator::Parser::new
# 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
Public Instance Methods
==(other)
click to toggle source
Calls superclass method
Threatinator::Parser#==
# 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_opts()
click to toggle source
# 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(row, line_number: lineno, pos_start: previous_pos, pos_end: pos)| ... }
click to toggle source
@param [IO] io @yield [record] Gives one line to the block @yieldparam record [Record] a record
# 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