module Eco module API class Session class Config < Hash attr_reader :name def initialize(name = :default) super(nil) @name = name self["org"] = {} end def clone(name) keys.each_with_object(self.class.new(name)) do |key, cnf| begin cnf[key] = self[key].clone(config: cnf) rescue ArgumentError cnf[key] = self[key].clone end end end def reopen yield(self) end def apis self["apis"] ||= Eco::API::Session::Config::Apis.new(config: self) end def logger self["logger"] ||= Eco::API::Session::Config::Logger.new(config: self) end def s3storage self["s3_storage"] ||= Eco::API::Session::Config::S3Storage.new(config: self) end def files self["files"] ||= Eco::API::Session::Config::Files.new(config: self) end def mailer self["mailer"] ||= Eco::API::Session::Config::Mailer.new(config: self) end def org self["org"] end def people self["people"] ||= Eco::API::Session::Config::People.new(config: self) end # LOGGER def log_console_level=(value) logger.console_level= value end def log_file_level=(value) logger.file_level = value end def log_file=(file) logger.file = file end def timestamp_console=(value) logger.timestamp_console = value end def log_connection=(value) logger.log_connection = value end # API def dry_run! self["dry-run"] = true end def dry_run? self["dry-run"] end def run_mode=(mode) apis.active_api.mode = mode end def run_mode_local? apis.active_api.local? end def run_mode_remote? apis.active_api.remote? end def apis? apis.apis? end def add_api(name, **kargs) apis.add(name, **kargs) self end def active_api(name) apis.active_name = name self end def api(logger = ::Logger.new(IO::NULL)) apis.api(logger) end # FILES def working_directory=(path) files.working_directory = path end def working_directory(mode: nil) unless mode wd = files.working_directory return wd unless wd.to_s.strip.empty? end mode ||= :active_api if mode == :active_api apis.active_root_name elsif mode == :raw_api_name apis.active_name else files.working_directory end end def file_timestamp_pattern=(pattern) files.timestamp_pattern = pattern end def file_manager Eco::API::Common::Session::FileManager.new(self) end def require(file = nil, match: nil) if match file_manager.dir.dir_files(pattern: match).each do |file| require_relative File.expand_path(file) end else require_relative "#{File.expand_path(file_manager.dir.file(file))}" end end # ORG def location_codes=(file) org["location_codes"] = file end def location_codes org["location_codes"] end def locations_mapper return @locations_mapper if instance_variable_defined?(:@locations_mapper) file = file_manager.newest(location_codes) @locations_mapper = Eco::Data::Mapper.new(file_manager.load_json(file), internal: :first) end def tagtree=(file) org["tagtree"] = file end def tagtree(enviro: nil) return @tagtree if instance_variable_defined?(:@tagtree) && @tagtree.enviro == enviro if tree_file = org["tagtree"] tree = [] tree = file_manager.load_json(tree_file) unless !tree_file @tagtree = Eco::API::Organization::TagTree.new(tree, enviro: enviro) end end def policy_groups return @policy_groups if instance_variable_defined?(:@policy_groups) pgs = api&.policy_groups.to_a @policy_groups = Eco::API::Organization::PolicyGroups.new(pgs) end def schemas return @schemas if instance_variable_defined?(:@schemas) schs = api&.person_schemas.to_a @schemas = Eco::API::Organization::PersonSchemas.new(schs) end def login_providers return @login_providers if instance_variable_defined?(:@login_providers) provs = api&.login_providers.to_a @login_providers = Eco::API::Organization::LoginProviders.new(provs) end def session @session ||= Eco::API::Session.new(self) end # PEOPLE def default_usergroup=(value) people.default_usergroup = value end def discarded_people_file=(value) people.discarded_file = value end def people_cache=(file) people.cache = file end def requests_backup_folder=(folder) people.requests_folder = folder end # PERSON FIELDS MAPPER def person_fields_mapper=(file) people.fields_mapper = file end def default_schema=(name) people.default_schema = name end # ACCOUNT PRESETS def presets_custom=(file) people.presets_custom = file end def presets_map=(file) people.presets_map = file end # CUSTOM PERSON PARSERS def person_parser(format: :csv, &block) people.parser(format: format, &block) end def usecases @usecases = self["usecases"] ||= Eco::API::UseCases.new if block_given? yield(@usecases) self else @usecases end end def post_launch self["post_launch"] ||= Eco::API::Session::Config::PostLaunch.new(config: self) end def policies @policies = self["policies"] ||= Eco::API::Policies.new if block_given? yield(@policies) self else @policies end end def error_handlers @error_handlers = self["error_handlers"] ||= Eco::API::Error::Handlers.new if block_given? yield(@error_handlers) self else @error_handlers end end def workflow @workflow = self["workflow"] ||= Eco::API::Session::Config::Workflow.new(config: self) @workflow.tap do |wf| yield(wf) if block_given? end end end end end end require_relative 'config/base_config' require_relative 'config/api' require_relative 'config/apis' require_relative 'config/logger' require_relative 'config/mailer' require_relative 'config/s3_storage' require_relative 'config/files' require_relative 'config/people' require_relative 'config/post_launch' require_relative 'config/workflow'