lib/eco/api/common/people/entry_factory.rb in eco-helpers-2.0.25 vs lib/eco/api/common/people/entry_factory.rb in eco-helpers-2.0.26
- old
+ new
@@ -78,25 +78,25 @@
# - if the `format:` you provide is not a `Symbol`.
# - if there is no _parser/serializer_ defined for `format:`.
# @param data [Array<Hash>] data to be parsed. It cannot be used alongside with `file:`
# @param file [String] absolute or relative path to the input file. It cannot be used alongside with `data:`.
# @param format [Symbol] it must be used when you use the option `file:` (i.e. `:xml`, `:csv`), as it specifies the format of the input `file:`.
- # @param encoding [String] optional parameter to read `file:` by expecting certain encoding.
+ # @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), encoding: nil)
+ 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 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)
- kargs = {}
- kargs.merge!(content: data) unless no_data
- kargs.merge!(file: file) unless no_file
- kargs.merge!(format: format) unless no_format
- kargs.merge!(encoding: encoding) if encoding
+ options.merge!(content: data) unless no_data
+ options.merge!(file: file) unless no_file
+ options.merge!(format: format) unless no_format
- Entries.new(to_array_of_hashes(**kargs), klass: PersonEntry, factory: self)
+ Entries.new(to_array_of_hashes(**options), klass: PersonEntry, factory: self)
end
def to_array_of_hashes(**kargs)
data = []
content, file, encoding, format = kargs.values_at(:content, :file, :encoding, :format)
@@ -116,10 +116,11 @@
case content
when Hash
logger.error("Input data as 'Hash' not supported. Expecting 'Enumerable' or 'String'")
exit(1)
when String
- to_array_of_hashes(content: person_parser.parse(format, content))
+ deps = {check_headers: true} if kargs[:check_headers]
+ to_array_of_hashes(content: person_parser.parse(format, content, deps: deps || {}))
when Enumerable
sample = content.to_a.first
case sample
when Hash, Array, ::CSV::Row
Eco::CSV::Table.new(content).to_array_of_hashes