Class: DataImport::CsvParser

Inherits:
BaseParser
  • Object
show all
Defined in:
app/services/data_import/csv_parser.rb

Instance Method Summary collapse

Instance Method Details

#column_namesObject



11
12
13
14
15
16
17
18
19
20
# File 'app/services/data_import/csv_parser.rb', line 11

def column_names
  first_row = CSV.open(file_path, 'r') { |csv| csv.first }
  headers = []
  if first_row_is_header?
    headers = first_row
  else
    headers  = first_row.each_with_index.map { |x,i| "Column#{i}" }
  end
  headers
end

#each_with_indexObject



3
4
5
6
7
8
9
# File 'app/services/data_import/csv_parser.rb', line 3

def each_with_index
  index = 0
  CSV.foreach(file_path) do |row|
    yield(row, index)
    index += 1
  end
end

#sizeObject



44
45
46
47
48
49
50
51
52
53
# File 'app/services/data_import/csv_parser.rb', line 44

def size
  res = 0
  csv = CSV.open(file_path)
  begin
    res = csv.count
  rescue StandardError => exc
  end
  csv.close
  res
end

#validateObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/services/data_import/csv_parser.rb', line 23

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 => exc
    errors << exc.to_s
  end
  csv.close
  errors
end