Class: DataImport::CsvParser
- Inherits:
-
BaseParser
- Object
- BaseParser
- DataImport::CsvParser
- Defined in:
- app/services/data_import/csv_parser.rb
Overview
Service object: csv parser.
Instance Method Summary collapse
Instance Method Details
#column_names ⇒ Object
12 13 14 15 16 17 18 19 |
# File 'app/services/data_import/csv_parser.rb', line 12 def column_names first_row = CSV.open(file_path, 'r', &:first) if first_row_is_header? first_row else first_row.each_with_index.map { |_x, i| "Column#{i}" } end end |
#each_with_index ⇒ Object
4 5 6 7 8 9 10 |
# File 'app/services/data_import/csv_parser.rb', line 4 def each_with_index index = 0 CSV.foreach(file_path) do |row| yield(row, index) index += 1 end end |
#size ⇒ Object
42 43 44 45 46 47 48 49 50 51 |
# File 'app/services/data_import/csv_parser.rb', line 42 def size res = 0 csv = CSV.open(file_path) begin res = csv.count rescue StandardError end csv.close res end |
#validate ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'app/services/data_import/csv_parser.rb', line 21 def validate # require 'CsvLint' errors = [] # dialect = { # "header" => first_row_is_header?, # "delimiter" => "," # } # validator = Csvlint::Validator.new( file_path, dialect ) # validator.validate # errors += validator.errors # Try a plain csv row count csv = CSV.open(file_path) begin csv.count rescue StandardError => e errors << e.to_s end csv.close errors end |