class Eco::API::Session

Class to manage the current session. Central helper of resources.

Public Class Methods

new(init = {}) click to toggle source

@param init [Eco::API::Session::Config, Eco::API::Common::Session::Environment] object to ini the session

# File lib/eco/api/session.rb, line 7
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)
  super(e)

  @entry_factories  = {}
  @person_factories = {}

  logger.debug("LINE COMMAND: #{$0} #{ARGV.join(" ")}")
end

Public Instance Methods

batch() click to toggle source

@return [Eco::API::Session::Batch] provides helper to launch batch operations.

# File lib/eco/api/session.rb, line 21
def batch
  @batch  ||= Batch.new(enviro)
end
csv_entries(file, **kargs) click to toggle source

Generates an entries collection from a csv input file. @see Eco::API::Common::People::EntryFactory#entries @param file [String] file to generate the entries from. @param (see Eco::API::Session#entries) @return [Eco::API::Common::People::Entries] collection of entries.

# File lib/eco/api/session.rb, line 158
def csv_entries(file, **kargs)
  kargs.merge!({
    file: file,
    format: :csv
  })
  return entries(**kargs)
end
discarded_entries() click to toggle source

Generates the collection of entries that should be discarded from an update. @note requires `session.config.people.discarded_file` to be defined. @return [Eco::API::Common::People::Entries] collection of entries.

# File lib/eco/api/session.rb, line 169
def discarded_entries
  return @discarded_entries if instance_variable_defined?(:@discarded_entries)
  file = config.people.discarded_file
  fatal("You have not specified the 'discarded_people_file'") unless file = file_manager.dir.file(file)
  @discarded_entries = csv_entries(file)
end
entries(*args) click to toggle source

@see Eco::API::Common::People::EntryFactory#entries @param (see Eco::API::Common::People::EntryFactory#entries) @return [Eco::API::Common::People::Entries] collection of entries.

# File lib/eco/api/session.rb, line 147
def entries(*args)
  entry_factory.entries(*args).tap do |collection|
    logger.info("Loaded #{collection.length} input entries.")
  end
end
entry_factory(schema: nil) click to toggle source

Helper to obtain a EntryFactory @param schema [String, Ecoportal::API::V1::PersonSchema] `schema` to which associate the EntryFactory,

where `String` can be the _name_ or the _id_ of the schema.

@return [Eco::API::Common::People::EntryFactory] associated to `schema`.

If `schema` is `nil` or not provided it uses the currently associated to the `session`
# File lib/eco/api/session.rb, line 84
def entry_factory(schema: nil)
  schema = to_schema(schema) || self.schema
  return @entry_factories[schema&.id] if @entry_factories.key?(schema&.id)
  unless @entry_factories.empty?
    @entry_factories[schema&.id] = @entry_factories.values.first.newFactory(schema: schema)
    return @entry_factories[schema&.id]
  end

  @entry_factories[schema&.id] = Eco::API::Common::People::EntryFactory.new(
    enviro,
    schema:        schema,
    person_parser: config.people.parser,
    attr_map:      fields_mapper
  )
end
export(*args) click to toggle source

@see Eco::API::Common::People::EntryFactory#export @param (see Eco::API::Common::People::EntryFactory#export)

# File lib/eco/api/session.rb, line 126
def export(*args)
  entry_factory.export(*args)
end
fields_mapper() click to toggle source

@return [Eco::Data::Mapper] the mappings between the internal and external attribute/property names.

# File lib/eco/api/session.rb, line 70
def fields_mapper
  return @fields_mapper if instance_variable_defined?(:@fields_mapper)
  mappings = []
  if map_file = config.people.fields_mapper
    mappings = map_file ? file_manager.load_json(map_file) : []
  end
  @fields_mapper = Eco::Data::Mapper.new(mappings)
end
job_group(name, order: :last) click to toggle source

It retrives the group of `Batch::Jobs` named `name`. It creates it if it doesn't exist. @return [Eco::API::Session::Batch::Jobs]

# File lib/eco/api/session.rb, line 232
def job_group(name, order: :last)
  case
  when job_groups.exists?(name)
    job_groups[name]
  else
    job_groups.new(name, order: order)
  end
end
job_groups() click to toggle source

@return [Eco::API::Session::Batch::JobsGroups]

# File lib/eco/api/session.rb, line 226
def job_groups
  @job_groups ||= Batch::JobsGroups.new(enviro)
end
jobs_launch(simulate: false) click to toggle source

@see Eco::API::Session::Batch::JobsGroups#launch

# File lib/eco/api/session.rb, line 249
def jobs_launch(simulate: false)
  job_groups.launch(simulate: simulate)
end
login_providers() click to toggle source

@see Eco::API::Session::Config#login_providers

# File lib/eco/api/session.rb, line 33
def login_providers
  config.login_providers
end
mail(**kargs) click to toggle source

Sends an email @see Eco::API::Common::Session::Mailer#mail @param (see Eco::API::Common::Session::Mailer#mail)

# File lib/eco/api/session.rb, line 264
def mail(**kargs)
  if mailer?
    mailer.mail(**kargs)
  else
    logger.error("You are trying to use the mailer, but it's not configured")
    nil
  end
end
micro() click to toggle source

Set of helpers to simplify your code @see Eco::API::MicroCases @return [Eco::API::MicroCases]

# File lib/eco/api/session.rb, line 211
def micro
  @micro    ||= Eco::API::MicroCases.new(enviro)
end
new_entry(data, dependencies: {}) click to toggle source

Builds the entry for the given data. @see Eco::API::Common::People::EntryFactory#new @return [Eco::API::Common::People::PersonEntry] parsed entry.

# File lib/eco/api/session.rb, line 140
def new_entry(data, dependencies: {})
  entry_factory(schema: data&.details&.schema_id).new(data, dependencies: dependencies)
end
new_job(group, name, type, usecase, sets = [:core, :details, :account], &block) click to toggle source

Shortcut to create a job of certain type within a group @param [see @Eco::API::Session::Batch::Jobs#new] @return [Eco::API::Session::Batch::Job]

# File lib/eco/api/session.rb, line 244
def new_job(group, name, type, usecase, sets = [:core, :details, :account], &block)
  job_group(group).new(name, usecase: usecase, type: type, sets: sets, &block)
end
new_person(**keyed_args) click to toggle source

@see Eco::API::Common::People::EntryFactory#new @param (see Eco::API::Common::People::EntryFactory#new) @return [Ecoportal::API::Internal::Person]

# File lib/eco/api/session.rb, line 133
def new_person(**keyed_args)
  person_factory.new(**keyed_args)
end
parse_attribute(attr, source, phase = :internal, deps: {}) click to toggle source

Allows to use the defined parsers @note the use of these method requires to know which is the expected format of `source` @param attr [String] type (`Symbol`) or attribute (`String`) to target a specific parser. @param source [Any] source value to be parsed. @param phase [Symbol] the phase when this parser should be active. @param phase [Symbol] the phase when this parser should be active. @return [Object] the parsed attribute.

# File lib/eco/api/session.rb, line 117
def parse_attribute(attr, source, phase = :internal, deps: {})
  unless parsers = entry_factory.person_parser
    raise "There are no parsers defined"
  end
  parsers.parse(attr, source, phase, deps: deps)
end
person_factory(schema: nil) click to toggle source

Helper to obtain a PersonFactory @param schema [String, Ecoportal::API::V1::PersonSchema] `schema` to which associate the PersonFactory,

where `String` can be the _name_ or the _id_ of the schema.

@return [Eco::API::Common::People::PersonFactory] associated to `schema`.

If `schema` is `nil` or not provided it uses the currently associated to the `session`
# File lib/eco/api/session.rb, line 105
def person_factory(schema: nil)
  schema = to_schema(schema) || self.schema
  @person_factories[schema&.id] ||= Eco::API::Common::People::PersonFactory.new(schema: schema)
end
policies() click to toggle source

Does merge `Eco::API::Policies::DefaultPolicies` with the custom policies. @note

- the default policies are added at the end (meaning they will run after the custom policies)

@return [Eco::API::Policies]

# File lib/eco/api/session.rb, line 201
def policies
  @policies ||= config.policies.dup.tap do |policies|
    default_policies = Eco::API::Policies::DefaultPolicies.new
    policies.merge(default_policies)
  end
end
policy_groups() click to toggle source

@see Eco::API::Session::Config#policy_groups

# File lib/eco/api/session.rb, line 28
def policy_groups
  config.policy_groups
end
post_launch() click to toggle source
# File lib/eco/api/session.rb, line 215
def post_launch
  @post_launch ||= config.post_launch.select(usecases)
end
presets_factory() click to toggle source

Helper to state the abilities that a person should have with given their usergroups

# File lib/eco/api/session.rb, line 65
def presets_factory
  @presets_factory ||= Eco::API::Organization::PresetsFactory.new(enviro: enviro)
end
process_case(name, io: nil, type: nil, **params) click to toggle source

@see Eco::API::UseCases::Case#launch

# File lib/eco/api/session.rb, line 220
def process_case(name, io: nil, type: nil, **params)
  args = { session: self }.merge(params)
  usecases.case(name, type: type).launch(io: io, **args)
end
s3upload(content: nil, file: nil, directory: nil, recurse: false, link: false) click to toggle source

Uploads content into a file, a file or a directory to S3 @see Eco::API::Common::Session::S3Uploader#upload @see Eco::API::Common::Session::S3Uploader#upload_file @see Eco::API::Common::Session::S3Uploader#upload_directory @param content [String] content to be uploaded (requires `file`) @param file [String] name of the file to be uploaded @param directory [String] name of source directory to be uploaded @param recurse [Boolean] used with `directory`: deepen in the folder structure? (`false`: default) @param link [Boolean] return _link(s)_ (`true`) or _path(s)_ (`false`: default) @return [String, Array<String>] either paths to S3 objects if `link` is `false`, or link otherwise

# File lib/eco/api/session.rb, line 283
def s3upload(content: nil, file: nil, directory: nil, recurse: false, link: false)
  if s3uploader?
    if content == :target
      path = micro.s3upload_targets
    elsif content && file
      path = s3uploader.upload(file, content)
    elsif file
      path = s3uploader.upload_file(file)
    elsif directory
      path = s3uploader.upload_directory(directory, recurse: recurse)
    else
      logger.error("To use Session.s3upload, you must specify either directory, file or content and file name")
    end
    return path unless link
    s3uploader.link(path)
  else
    logger.error("You are trying to use S3 uploader, but it's not configured")
    nil
  end
end
schema() click to toggle source

@return [String, Ecoportal::API::V1::PersonSchema] current active session's schema

# File lib/eco/api/session.rb, line 51
def schema
  self.schema = config.people.default_schema || schemas.first unless @schema
  @schema
end
schema=(value) click to toggle source

Sets the current target `PersonSchema` of this session. @note observe that it is essential for the parsers/serialisers to identify target/present attributes. @param value [String, Ecoportal::API::V1::PersonSchema] where `String` can be the name or the id of the schema.

# File lib/eco/api/session.rb, line 59
def schema=(value)
  @schema = to_schema(value)
  self
end
schemas() click to toggle source

@see Eco::API::Session::Config#schemas

# File lib/eco/api/session.rb, line 43
def schemas
  config.schemas
end
summary() click to toggle source

@see Eco::API::Session::Batch::JobsGroups#summary

# File lib/eco/api/session.rb, line 254
def summary
  job_groups.summary
end
tagtree() click to toggle source

@see Eco::API::Session::Config#tagtree

# File lib/eco/api/session.rb, line 38
def tagtree
  config.tagtree(enviro: enviro)
end
usecases() click to toggle source

Does merge `Eco::API::UseCases::DefaultCases` with the custom cases. @note

- the order matters, as a default usecase can be redefined by a custom one with same name

@return [Eco::API::UseCases]

# File lib/eco/api/session.rb, line 190
def usecases
  @usecases ||= config.usecases.dup.tap do |cases|
    all_cases = Eco::API::UseCases::DefaultCases.new.merge(config.usecases)
    cases.merge(all_cases)
  end
end
workflow(io:) { |wf, io| ... } click to toggle source

Opens up the `workflow` configuration

# File lib/eco/api/session.rb, line 180
def workflow(io:)
  config.workflow.tap do |wf|
    yield(wf, io) if block_given?
  end
end

Private Instance Methods

same_schema?(schema1, schema2) click to toggle source

Comparer to state if 2 schemas are different

# File lib/eco/api/session.rb, line 308
def same_schema? (schema1, schema2)
  eq   = schema1&.id == schema2&.id
  eq ||= schema1&.name&.downcase == schema2&.name&.downcase
  schema1 && schema2 && eq
end
to_schema(value) click to toggle source

from schema `id` or `name` to a PersonSchema object

# File lib/eco/api/session.rb, line 315
def to_schema(value)
  return nil unless value
  sch = nil
  case value
  when String
    unless sch = schemas.schema(value)
      fatal "The schema with id or name '#{value}' does not exist."
    end
  when Ecoportal::API::V1::PersonSchema
    sch = value
  else
    fatal "Required String or Ecoportal::API::V1::PersonSchema. Given: #{value}"
  end
  sch
end