2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-12-03 23:12:06 +08:00
|
|
|
module ImportScripts
|
|
|
|
module CsvHelper
|
|
|
|
class RowResolver
|
|
|
|
def load(row)
|
|
|
|
@row = row
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.create(cols)
|
|
|
|
Class.new(RowResolver).new(cols)
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(cols)
|
|
|
|
cols.each_with_index do |col, idx|
|
2019-05-07 09:27:05 +08:00
|
|
|
self
|
|
|
|
.class
|
|
|
|
.public_send(:define_method, col.downcase.gsub(/[\W]/, "_").squeeze("_")) { @row[idx] }
|
2015-12-03 23:12:06 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def csv_parse(filename, col_sep = ",")
|
|
|
|
first = true
|
|
|
|
row = nil
|
|
|
|
|
2019-05-31 04:20:57 +08:00
|
|
|
current_row = +""
|
2015-12-03 23:12:06 +08:00
|
|
|
double_quote_count = 0
|
|
|
|
|
|
|
|
File
|
|
|
|
.open(filename)
|
|
|
|
.each_line do |line|
|
|
|
|
line.strip!
|
2023-01-07 19:53:14 +08:00
|
|
|
|
2015-12-03 23:12:06 +08:00
|
|
|
current_row << "\n" unless current_row.empty?
|
|
|
|
current_row << line
|
2023-01-07 19:53:14 +08:00
|
|
|
|
2015-12-03 23:12:06 +08:00
|
|
|
double_quote_count += line.scan('"').count
|
2023-01-07 19:53:14 +08:00
|
|
|
|
2015-12-03 23:12:06 +08:00
|
|
|
next if double_quote_count % 2 == 1 # this row continues on a new line. don't parse until we have the whole row.
|
2023-01-07 19:53:14 +08:00
|
|
|
|
2015-12-03 23:12:06 +08:00
|
|
|
raw =
|
|
|
|
begin
|
|
|
|
CSV.parse(current_row, col_sep: col_sep)
|
|
|
|
rescue CSV::MalformedCSVError => e
|
|
|
|
puts e.message
|
|
|
|
puts "*" * 100
|
|
|
|
puts "Bad row skipped, line is: #{line}"
|
|
|
|
puts
|
|
|
|
puts current_row
|
|
|
|
puts
|
|
|
|
puts "double quote count is : #{double_quote_count}"
|
|
|
|
puts "*" * 100
|
2023-01-07 19:53:14 +08:00
|
|
|
|
2015-12-03 23:12:06 +08:00
|
|
|
current_row = ""
|
|
|
|
double_quote_count = 0
|
2023-01-07 19:53:14 +08:00
|
|
|
|
|
|
|
next
|
|
|
|
end[
|
|
|
|
0
|
|
|
|
]
|
|
|
|
|
|
|
|
if first
|
2015-12-03 23:12:06 +08:00
|
|
|
row = RowResolver.create(raw)
|
2023-01-07 19:53:14 +08:00
|
|
|
|
2015-12-03 23:12:06 +08:00
|
|
|
current_row = ""
|
|
|
|
double_quote_count = 0
|
|
|
|
first = false
|
2023-01-07 19:53:14 +08:00
|
|
|
next
|
|
|
|
end
|
2015-12-03 23:12:06 +08:00
|
|
|
|
|
|
|
row.load(raw)
|
|
|
|
|
|
|
|
yield row
|
|
|
|
|
|
|
|
current_row = ""
|
|
|
|
double_quote_count = 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|