lib/eco/api/session.rb in eco-helpers-0.6.17 vs lib/eco/api/session.rb in eco-helpers-0.7.1
- old
+ new
@@ -1,16 +1,13 @@
module Eco
module API
class Session < Common::Session::BaseSession
- attr_reader :batch, :policy_groups
- attr_accessor :schema, :schemas_hash
- attr_reader :tagtree
+ attr_reader :batch, :policy_groups, :tagtree
attr_reader :job_groups
+ attr_reader :schemas
+ attr_accessor :schema
- #attr_reader :organization
- #alias_method :org, :organization
-
def initialize(init = {})
e = init
msg = "Expected object Eco::API::Session::Config or Eco::API::Common::Session::Environment. Given: #{init}"
raise msg unless e.is_a?(Session::Config) || e.is_a?(Eco::API::Common::Session::Environment)
e = Eco::API::Common::Session::Environment.new(init, session: self) if !e.is_a?(Eco::API::Common::Session::Environment)
@@ -22,17 +19,13 @@
@task = Task.new(enviro)
@job_groups = JobGroups.new(enviro)
@use_cases = Eco::API::UseCases::DefaultCases.new.merge(config.usecases.use_group)
- @schemas = api&.person_schemas.to_a
- @schemas_hash = @schemas.map do |sch|
- [[sch.id, sch], [sch.name.downcase, sch]]
- end.flatten(1).to_h
-
+ @schemas = config.schemas
self.schema = config.people.default_schema || @schemas.first
- @policy_groups = nil
+ @policy_groups = config.policy_groups
@tagtree = nil
if tree_file = config.dig('org', 'tagtree')
tree = file_manager.load_json(tree_file) unless !tree_file
@tagtree = Eco::API::Organization::TagTree.new(tree, enviro: enviro)
@@ -48,14 +41,13 @@
enviro: enviro
})
end
def self.configure
- # TODO: change to Session::Config.new.tap
- conf = Session::Config.new
- yield(conf) if block_given?
- conf
+ Session::Config.new.tap do |conf|
+ yield(conf) if block_given?
+ end
end
# TASKS & JOBS
def do
@task
@@ -97,30 +89,38 @@
end
def schema=(value)
case value
when String
- sch = @schemas_hash[value.downcase]
- fatal "The schema with id or name '#{value}' does not exist" if !sch
- @schema = sch
+ unless @schema = @schemas.schema(value)
+ fatal "The schema with id or name '#{value}' does not exist"
+ end
when Ecoportal::API::V1::PersonSchema
@schema = value
else
logger.error("Session: Missuse of 'schema=' method. Given: #{value}")
return
end
- @person_factory = Eco::API::Common::People::PersonFactory.new(schema: @schema)
+ @person_factory = Eco::API::Common::People::PersonFactory.new(schema: @schema)
- fields_mapper_file = config.people.fields_mapper
- fields_mapper = Eco::Data::Mapper.new(file_manager.load_json(fields_mapper_file))
- @entry_factory = Eco::API::Common::People::EntryFactory.new({
- schema: @schema,
+ # TODO: check PersonEntry#to_internal and #to_external in init_attr_trackers
+ # => when attr_map is avoided, it doesn't work as it should
+ if map_file = config.people.fields_mapper
+ mappings = map_file ? file_manager.load_json(map_file) : []
+ else
+ mappings = []
+ end
+ attr_map = Eco::Data::Mapper.new(mappings)
+
+ @entry_factory = Eco::API::Common::People::EntryFactory.new({
+ schema: @schema,
person_parser: config.people.parser,
- fields_mapper: fields_mapper,
- logger: logger
+ attr_map: attr_map,
+ logger: logger
})
+ self
end
def new_preset(input)
case input
when Ecoportal::API::Internal::Person
@@ -138,18 +138,20 @@
def new_entry(data, dependencies: {})
@entry_factory.new(data, dependencies: dependencies)
end
+ def entries(*args)
+ @entry_factory.entries(*args)
+ end
+
+ def export(*args)
+ @entry_factory.export(*args)
+ end
+
def csv_entries(file)
- rows = []
- if Eco::API::Common::Session::FileManager.file_exists?(file)
- CSV.foreach(file, headers: true, encoding: Eco::API::Common::Session::FileManager.encoding(file)).with_index do |row, i|
- rows.push(row)
- end
- end
- @entry_factory.entries(rows)
+ return entries(file: file, format: :csv)
end
def discarded_entries
file = config.people.discarded_file
file = file_manager.dir.file(file)
@@ -158,10 +160,10 @@
def process_case(name, type: nil, **params)
args = { session: self }.merge(params)
fatal("Undefined usecase '#{name}' of type '#{type.to_s}'") if !@use_cases.defined?(name, type: type)
logger.debug("Session: going to process '#{name}' defined case")
- @use_cases.case(name, type: type).process(**args)
+ @use_cases.case(name, type: type).launch(**args)
end
end
end
end