lib/eco/api/common/people/default_parsers/xls_parser.rb in eco-helpers-3.0.18 vs lib/eco/api/common/people/default_parsers/xls_parser.rb in eco-helpers-3.0.19
- old
+ new
@@ -1,15 +1,12 @@
class Eco::API::Common::People::DefaultParsers::XLSParser < Eco::API::Common::Loaders::Parser
attribute :xls
attr_accessor :already_required
- attr_reader :file
- def parser(file, _deps)
- @file = file
- rows.tap do |rws|
- @file = nil
+ def parser(filename, _deps)
+ rows(file: filename).tap do |rws|
rws.each do |row|
to_string!(row)
end
end
end
@@ -20,50 +17,52 @@
private
def to_string!(row)
row.transform_values! do |val|
- next nil unless val
+ next unless val
next val if val.is_a?(String)
+
val.to_s
end
end
- def headers
+ def expected_headers
log(:warn) {
"Headers detection is using your fields_map.json file (native behaviour)"
}
session.fields_mapper.list(:external).uniq
end
def sheet_name
0
end
- def workbook
+ def workbook(file)
require_reading_libs!
Roo::Spreadsheet.open(file)
end
- def spreadheet(name_or_index = sheet_name)
- workbook.sheet(name_or_index)
+ def spreadheet(name_or_index = sheet_name, file:)
+ workbook(file).sheet(name_or_index)
end
- def rows(target = headers)
- spreadheet.parse(header_search: target, clean: true)
+ def rows(target = expected_headers, file:)
+ spreadheet(file: file).parse(header_search: target, clean: true)
rescue Roo::HeaderRowNotFoundError => e
missing = JSON.parse(e.message)
log(:warn) {
- "The input file is missing these headers: #{missing}"
+ "The input file is missing these expected headers: #{missing}"
}
present = target - missing
- rows(present)
+ rows(present, file: file)
end
def require_reading_libs!
return if already_required
+
require 'roo'
require 'roo-xls'
self.already_required = true
end
end