Class: Threatinator::Parsers::CSV::Parser
- Inherits:
-
Threatinator::Parser
- Object
- Threatinator::Parser
- Threatinator::Parsers::CSV::Parser
- Defined in:
- lib/threatinator/parsers/csv/parser.rb
Overview
Parses an IO, yielding a record with a CSV::Row.
Instance Attribute Summary collapse
-
#col_separator ⇒ Object
readonly
Returns the value of attribute col_separator.
-
#csv_opts ⇒ Object
readonly
Returns the value of attribute csv_opts.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#row_separator ⇒ Object
readonly
Returns the value of attribute row_separator.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #_build_csv_opts ⇒ Object
-
#initialize(opts = {}) ⇒ Parser
constructor
A new instance of Parser.
- #run(io) {|record| ... } ⇒ Object
Methods inherited from Threatinator::Parser
Constructor Details
#initialize(opts = {}) ⇒ Parser
Returns a new instance of Parser.
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_separator ⇒ Object (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_opts ⇒ Object (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 |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
10 11 12 |
# File 'lib/threatinator/parsers/csv/parser.rb', line 10 def headers @headers end |
#row_separator ⇒ Object (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_opts ⇒ Object
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
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 |