lib/eco/api/common/people/entry_factory.rb in eco-helpers-2.4.9 vs lib/eco/api/common/people/entry_factory.rb in eco-helpers-2.5.1
- old
+ new
@@ -84,14 +84,14 @@
# @param options [Hash] further options.
# @option options [String] :encoding optional parameter to read `file:` by expecting certain encoding.
# @option options [Boolean] :check_headers signals if the `csv` file headers should be expected.
# @return [Eco::API::Common::People::Entries] collection of `Eco::API::Common::People::PersonEntry`.
def entries(data: (no_data = true; nil), file: (no_file = true; nil), format: (no_format = true; nil), **options)
- fatal("You should at least use data: or file:, but not both") if no_data == no_file
+ fatal("You should at least use data: or file:, but not both") if no_data == no_file
fatal("You must specify a valid format: (symbol) when you use file.") if file && no_format
- fatal("Format should be a Symbol. Given '#{format}'") if format && !format.is_a?(Symbol)
- fatal("There is no parser/serializer for format ':#{format.to_s}'") unless no_format || @person_parser.defined?(format)
+ fatal("Format should be a Symbol. Given '#{format}'") if format && !format.is_a?(Symbol)
+ fatal("There is no parser/serializer for format ':#{format.to_s}'") unless no_format || @person_parser.defined?(format)
options.merge!(content: data) unless no_data
options.merge!(file: file) unless no_file
options.merge!(format: format) unless no_format
@@ -111,13 +111,12 @@
end
end
# Get content only when it's not :xls
# note: even if content was provided, file takes precedence
if (format != :xls) && file
- content = get_file_content(file, encoding)
+ content = get_file_content(file, encoding: encoding)
end
- #content = get_file_content(file, format, encoding) if (format != :xls) && file
case content
when Hash
logger.error("Input data as 'Hash' not supported. Expecting 'Enumerable' or 'String'")
exit(1)
@@ -127,19 +126,20 @@
when Enumerable
sample = content.to_a.first
case sample
when Hash, Array, ::CSV::Row
Eco::CSV::Table.new(content).to_array_of_hashes
+ when NilClass
+ abort("There is NO input data")
else
- logger.error("Input content 'Array' of '#{sample.class}' is not supported.")
+ abort("Input content 'Array' of '#{sample.class}' is not supported.")
end
else
if file && format == :xls
person_parser.parse(format, file)
else
- logger.error("Could not obtain any data out of these: #{kargs}. Given content: '#{content.class}'")
- exit(1)
+ abort("Could not obtain any data out of these: #{kargs}. Given content: '#{content.class}'")
end
end.tap do |out_array|
start_from_two = (format == :csv) || format == :xls
out_array.each_with_index do |entry_hash, i|
entry_hash["idx"] = start_from_two ? i + 2 : i + 1
@@ -186,9 +186,14 @@
end
end
end
private
+
+ def abort(message)
+ logger.error(message)
+ exit(1)
+ end
def fatal(msg)
logger.fatal(msg)
raise msg
end