class Eco::API::UseCases::Default::Locations::CsvToTree < Eco::API::Common::Loaders::UseCase name "csv-to-tree" type :other include Eco::Data::Locations::DSL TIME_FORMAT = '%Y%m%dT%H%M%S' def main(session, options, usecase) options[:end_get] = false tree_struct = org_tree(input_csv) File.open(output_file, "w") do |fd| fd << tree_struct.as_json.to_json end logger.info("Saved structure in '#{output_file}'") end private def output_file @output_file ||= "#{active_enviro}_tree_#{timestamp}.json" end def input_csv @input_csv ||= Eco::CSV.read(input_file, encoding: input_encoding) end def input_file @input_file ||= options.dig(:source, :file) end def input_encoding options.dig(:input, :file, :encoding) || 'utf-8' end def timestamp(date = Time.now) date.strftime(TIME_FORMAT) end def active_enviro config.active_enviro end end