Class: Eco::API::Session

Inherits:
Common::Session::BaseSession show all
Defined in:
lib/eco/api/session.rb,
lib/eco/api/session/batch.rb,
lib/eco/api/session/config.rb,
lib/eco/api/session/batch/job.rb,
lib/eco/api/session/batch/jobs.rb,
lib/eco/api/session/config/api.rb,
lib/eco/api/session/config/apis.rb,
lib/eco/api/session/config/sftp.rb,
lib/eco/api/session/batch/errors.rb,
lib/eco/api/session/batch/status.rb,
lib/eco/api/session/config/files.rb,
lib/eco/api/session/config/logger.rb,
lib/eco/api/session/config/mailer.rb,
lib/eco/api/session/config/people.rb,
lib/eco/api/session/batch/feedback.rb,
lib/eco/api/session/batch/policies.rb,
lib/eco/api/session/config/workflow.rb,
lib/eco/api/session/batch/base_policy.rb,
lib/eco/api/session/batch/jobs_groups.rb,
lib/eco/api/session/config/s3_storage.rb,
lib/eco/api/session/config/base_config.rb,
lib/eco/api/session/config/post_launch.rb,
lib/eco/api/session/batch/request_stats.rb

Overview

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

Defined Under Namespace

Classes: Batch, Config

Instance Attribute Summary

Attributes inherited from Common::Session::BaseSession

#api, #config, #environment, #file_manager, #logger, #session

Pure organization helpers collapse

People and Input entries helpers collapse

Session workflow and batch job launces collapse

Additional resources collapse

Instance Method Summary collapse

Methods inherited from Common::Session::BaseSession

#enviro=, #fatal, #fm, #mailer, #mailer?, #s3uploader, #s3uploader?, #sftp, #sftp?

Constructor Details

#initialize(init = {}) ⇒ Session

Returns a new instance of Session.

Parameters:



7
8
9
10
11
12
13
14
15
16
17
18
# 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

Instance Method Details

#batchEco::API::Session::Batch

Returns provides helper to launch batch operations.

Returns:



21
22
23
# File 'lib/eco/api/session.rb', line 21

def batch
  @batch  ||= Batch.new(enviro)
end

#csv_entries(file) ⇒ Eco::API::Common::People::Entries

Generates an entries collection from a csv input file.

Parameters:

  • file (String)

    file to generate the entries from.

Returns:

See Also:



165
166
167
# File 'lib/eco/api/session.rb', line 165

def csv_entries(file)
  return entries(file: file, format: :csv)
end

#discarded_entriesEco::API::Common::People::Entries

Note:

requires session.config.people.discarded_file to be defined.

Generates the collection of entries that should be discarded from an update.

Returns:



172
173
174
175
176
177
# File 'lib/eco/api/session.rb', line 172

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) ⇒ Eco::API::Common::People::Entries

Returns collection of entries.

Parameters:

  • data (Array<Hash>)

    data to be parsed. It cannot be used alongside with file:

  • file (String)

    absolute or relative path to the input file. It cannot be used alongside with data:.

  • 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:.

  • encoding (String)

    optional parameter to read file: by expecting certain encoding.

Returns:

See Also:



155
156
157
158
159
# File 'lib/eco/api/session.rb', line 155

def entries(*args)
  entry_factory.entries(*args).tap do |collection|
    logger.info("Loaded #{collection.length} input entries.")
  end
end

#entry_factory(schema: nil) ⇒ Eco::API::Common::People::EntryFactory

Helper to obtain a EntryFactory

Parameters:

  • schema (String, Ecoportal::API::V1::PersonSchema) (defaults to: nil)

    schema to which associate the EntryFactory, where String can be the name or the id of the schema.

Returns:



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/eco/api/session.rb', line 93

def entry_factory(schema: nil)
  schema = to_schema(schema) || self.schema
  return @entry_factories[schema&.id] if @entry_factories.key?(schema&.id)

  mappings = []
  if map_file = config.people.fields_mapper
    mappings = map_file ? file_manager.load_json(map_file) : []
  end

  @entry_factories[schema&.id] = Eco::API::Common::People::EntryFactory.new(
    enviro,
    schema:        schema,
    person_parser: config.people.parser,
    attr_map:      Eco::Data::Mapper.new(mappings)
  )
end

#export(*args) ⇒ Object

Parameters:

  • data (Eco::API::Organization::People)

    data to be parsed.

  • file (String)

    absolute or relative path to the ouput file.

  • format (Symbol)

    it specifies the format of the output file: (i.e. :xml, :csv). There must be a parser/serializer defined for it.

  • encoding (String)

    optional parameter to geneate file: content by unsing certain encoding.

See Also:



134
135
136
# File 'lib/eco/api/session.rb', line 134

def export(*args)
  entry_factory.export(*args)
end

#job_group(name, order: :last) ⇒ Eco::API::Session::Batch::Jobs



221
222
223
224
225
226
227
228
# File 'lib/eco/api/session.rb', line 221

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_groupsEco::API::Session::Batch::JobsGroups



216
217
218
# File 'lib/eco/api/session.rb', line 216

def job_groups
  @job_groups ||= Batch::JobsGroups.new(enviro)
end

#jobs_launch(simulate: false) ⇒ Object



237
238
239
# File 'lib/eco/api/session.rb', line 237

def jobs_launch(simulate: false)
  job_groups.launch(simulate: simulate)
end

#login_providersObject



33
34
35
# File 'lib/eco/api/session.rb', line 33

def 
  config.
end

#mail(**kargs) ⇒ Object

Sends an email

Parameters:

  • to (String)

    destination email address

  • subject (String)

    subject of the email

  • body (String)

    html or plain text message

See Also:



252
253
254
255
256
257
258
259
# File 'lib/eco/api/session.rb', line 252

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

#microEco::API::MicroCases

Set of helpers to simplify your code

Returns:

See Also:



201
202
203
# File 'lib/eco/api/session.rb', line 201

def micro
  @micro    ||= Eco::API::MicroCases.new(enviro)
end

#new_entry(data, dependencies: {}) ⇒ Eco::API::Common::People::PersonEntry

Builds the entry for the given data.



148
149
150
# File 'lib/eco/api/session.rb', line 148

def new_entry(data, dependencies: {})
  entry_factory.new(data, dependencies: dependencies)
end

#new_job(group, name, type, usecase, sets = [:core, :details, :account]) ⇒ Eco::API::Session::Batch::Job

Shortcut to create a job of certain type within a group



232
233
234
# File 'lib/eco/api/session.rb', line 232

def new_job(group, name, type, usecase, sets = [:core, :details, :account])
  job_group(group).new(name, usecase: usecase, type: type, sets: sets)
end

#new_person(**keyed_args) ⇒ Ecoportal::API::Internal::Person

Parameters:

  • data (Array<Hash>)

    data to be parsed. The external hashed entry.

Returns:

See Also:



141
142
143
# File 'lib/eco/api/session.rb', line 141

def new_person(**keyed_args)
  person_factory.new(**keyed_args)
end

#new_preset(input) ⇒ Hash

Note:

for each flag/ability it will take the highest among those mapped for the present usergroups.

Builds the presets using the usergroup ids of the input.

Parameters:

Returns:

  • (Hash)

    with custom presets.



68
69
70
71
72
73
74
75
76
77
# File 'lib/eco/api/session.rb', line 68

def new_preset(input)
  case input
  when Ecoportal::API::Internal::Person
    presets_factory.new(*input&.&.policy_group_ids)
  when Array
    presets_factory.new(*input)
  else
    presets_factory.new(input)
  end
end

#parse_attribute(attr, source, phase = :internal) ⇒ Object

Note:

the use of these method requires to know which is the expected format of source

Allows to use the defined parsers

Parameters:

  • attr (String)

    type (Symbol) or attribute (String) to target a specific parser.

  • source (Any)

    source value to be parsed.

  • phase (Symbol) (defaults to: :internal)

    the phase when this parser should be active.



125
126
127
128
129
130
# File 'lib/eco/api/session.rb', line 125

def parse_attribute(attr, source, phase = :internal)
  unless parsers = entry_factory.person_parser
    raise "There are no parsers defined"
  end
  parsers.parse(attr, source)
end

#person_factory(schema: nil) ⇒ Eco::API::Common::People::PersonFactory

Helper to obtain a PersonFactory

Parameters:

  • schema (String, Ecoportal::API::V1::PersonSchema) (defaults to: nil)

    schema to which associate the PersonFactory, where String can be the name or the id of the schema.

Returns:



115
116
117
118
# File 'lib/eco/api/session.rb', line 115

def person_factory(schema: nil)
  schema = to_schema(schema) || self.schema
  @person_factories[schema&.id] ||= Eco::API::Common::People::PersonFactory.new(schema: schema)
end

#policy_groupsObject



28
29
30
# File 'lib/eco/api/session.rb', line 28

def policy_groups
  config.policy_groups
end

#post_launchObject



205
206
207
# File 'lib/eco/api/session.rb', line 205

def post_launch
  @post_launch ||= config.post_launch.select(usecases)
end

#presets_factoryObject

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



80
81
82
83
84
85
86
# File 'lib/eco/api/session.rb', line 80

def presets_factory
  @presets_factory ||= Eco::API::Organization::PresetsFactory.new({
    presets_custom: file_manager.dir.file(config.people.presets_custom, should_exist: true),
    presets_map:    file_manager.dir.file(config.people.presets_map,    should_exist: true),
    enviro:         enviro
  })
end

#process_case(name, io: nil, type: nil, **params) ⇒ Object

See Also:

  • UseCases::Case#launch


210
211
212
213
# File 'lib/eco/api/session.rb', line 210

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) ⇒ String+

Uploads content into a file, a file or a directory to S3

Parameters:

  • content (String) (defaults to: nil)

    content to be uploaded (requires file)

  • file (String) (defaults to: nil)

    name of the file to be uploaded

  • directory (String) (defaults to: nil)

    name of source directory to be uploaded

  • recurse (Boolean) (defaults to: false)

    used with directory: deepen in the folder structure? (false: default)

  • link (Boolean) (defaults to: false)

    return link(s) (true) or path(s) (false: default)

Returns:

  • (String, Array<String>)

    either paths to S3 objects if link is false, or link otherwise

See Also:



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/eco/api/session.rb', line 271

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

#schemaString, Ecoportal::API::V1::PersonSchema

Returns current active session's schema.

Returns:

  • (String, Ecoportal::API::V1::PersonSchema)

    current active session's schema



51
52
53
54
# File 'lib/eco/api/session.rb', line 51

def schema
  self.schema = config.people.default_schema || schemas.first unless @schema
  @schema
end

#schema=(value) ⇒ Object

Note:

observe that it is essential for the parsers/serialisers to identify target/present attributes.

Sets the current target PersonSchema of this session.

Parameters:

  • value (String, Ecoportal::API::V1::PersonSchema)

    where String can be the name or the id of the schema.



59
60
61
62
# File 'lib/eco/api/session.rb', line 59

def schema=(value)
  @schema = to_schema(value)
  self
end

#schemasObject



43
44
45
# File 'lib/eco/api/session.rb', line 43

def schemas
  config.schemas
end

#summaryObject



242
243
244
# File 'lib/eco/api/session.rb', line 242

def summary
  job_groups.summary
end

#tagtreeObject



38
39
40
# File 'lib/eco/api/session.rb', line 38

def tagtree
  config.tagtree(enviro: enviro)
end

#usecasesEco::API::UseCases

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

Returns:



191
192
193
194
195
196
# File 'lib/eco/api/session.rb', line 191

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:) ⇒ Object

Opens up the workflow configuration



183
184
185
186
187
# File 'lib/eco/api/session.rb', line 183

def workflow(io:)
  config.workflow.tap do |wf|
    yield(wf, io) if block_given?
  end
end