readd type to pase function

This commit is contained in:
viehlieb 2023-01-20 15:11:23 +01:00
parent bfae38147e
commit cdaacbf5b9
6 changed files with 14 additions and 12 deletions

View file

@ -34,14 +34,16 @@ module FoodsoftArticleImport
# @param file [File, Tempfile]
# @option opts [String] type file format (required) (see {.file_formats})
# @return [File, Roo::Spreadsheet] file with encoding set if needed
def self.parse(file, custom_file=nil, type='bnn', **opts, &blk)
def self.parse(file, custom_file_path=nil, type='bnn', **opts, &blk)
# @todo handle wrong or undetected type
type = opts[:type] || 'bnn'
puts type
parser = file_formats[type]
if block_given?
parser.parse(file, custom_file, **opts, &blk)
parser.parse(file, custom_file_path, **opts, &blk)
else
data = []
parser.parse(file, custom_file, **opts) { |a| data << a }
parser.parse(file, custom_file_path, **opts) { |a| data << a }
data
end
end

View file

@ -27,7 +27,7 @@ module FoodsoftArticleImport
RES_PARSE_UNIT_LIST.map {|r| /#{r}\s*$/} +
RES_PARSE_UNIT_LIST.map {|r| /-#{r}/}
def self.parse(file, custom_file=nil, **opts)
def self.parse(file, custom_file_path=nil, **opts)
opts = OPTIONS.merge(opts)
ss = FoodsoftArticleImport.open_spreadsheet(file, **opts)

View file

@ -9,13 +9,13 @@ module FoodsoftArticleImport
@@codes = Hash.new
@@midgard = Hash.new
# Loads the codes_file config/bnn_codes.yml into the class variable @@codes
def self.load_codes(custom_file=nil)
def self.load_codes(custom_file_path=nil)
@gem_lib = File.expand_path "../../", __FILE__
dir = File.join @gem_lib, 'foodsoft_article_import'
begin
@@codes = YAML::load(File.open(File.join(dir,"bnn_codes.yml"))).symbolize_keys
if(custom_file)
custom_codes = YAML::load(File.open(custom_file)).symbolize_keys
if(custom_file_path)
custom_codes = YAML::load(File.open(custom_file_path)).symbolize_keys
custom_codes.keys.each do |key|
if @@codes.keys.include?(key)
custom_codes[key] =custom_codes[key].merge @@codes[key]
@ -52,10 +52,10 @@ module FoodsoftArticleImport
}.freeze
# parses a bnn-file
def self.parse(file, custom_file=nil, **opts)
def self.parse(file, custom_file_path=nil, **opts)
encoding = opts[:encoding] || OPTIONS[:encoding]
col_sep = opts[:col_sep] || OPTIONS[:col_sep]
self.load_codes(custom_file)
self.load_codes(custom_file_path)
CSV.foreach(file, {col_sep: col_sep, encoding: encoding, headers: true}).with_index(1) do |row, i|
# check if the line is empty
unless row[0] == "" || row[0].nil?

View file

@ -19,7 +19,7 @@ module FoodsoftArticleImport
encoding: "UTF-8" # @todo check this
}.freeze
def self.parse(file, custom_file=nil, **opts)
def self.parse(file, custom_file_path=nil, **opts)
global_manufacturer = nil
file.set_encoding(opts[:encoding] || OPTIONS[:encoding])

View file

@ -14,7 +14,7 @@ module FoodsoftArticleImport
OPTIONS = {}.freeze
# parses a string or file
def self.parse(file, custom_file=nil, opts={})
def self.parse(file, custom_file_path=nil, opts={})
doc = Nokogiri.XML(file, nil, nil,
Nokogiri::XML::ParseOptions::RECOVER +
Nokogiri::XML::ParseOptions::NONET +

View file

@ -16,7 +16,7 @@ module FoodsoftArticleImport::Foodsoft
# Parses Foodsoft file
# the yielded article is a simple hash
def self.parse(file, custom_file=nil, **opts)
def self.parse(file, custom_file_path=nil, **opts)
opts = OPTIONS.merge(opts)
ss = FoodsoftArticleImport.open_spreadsheet(file, **opts)