Class: Eco::API::Session
- Inherits:
-
Common::Session::BaseSession
- Object
- Common::Session::BaseSession
- Eco::API::Session
- Defined in:
- lib/eco/api/session.rb,
lib/eco/api/session/task.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
Instance Attribute Summary
Attributes inherited from Common::Session::BaseSession
#api, #config, #environment, #file_manager, #logger, #session
Instance Method Summary collapse
-
#batch ⇒ Eco::API::Session::Batch
Provides helper to launch batch operations.
-
#csv_entries(file) ⇒ Eco::API::Common::People::Entries
Generates an entries collection from a csv input file.
-
#discarded_entries ⇒ Eco::API::Common::People::Entries
Generates the collection of entries that should be discarded from an update.
-
#do ⇒ Eco::API::Session::Task
Helper to perform multiple operations in one go.
-
#entries(*args) ⇒ Eco::API::Common::People::Entries
Collection of entries.
-
#entry_factory(schema: nil) ⇒ Eco::API::Common::People::EntryFactory
Helper to obtain a EntryFactory.
- #export(*args) ⇒ Object
-
#initialize(init = {}) ⇒ Session
constructor
A new instance of Session.
- #job_group(name, order: :last) ⇒ Object
- #job_groups ⇒ Object
- #jobs_launch(simulate: false) ⇒ Object
- #login_providers ⇒ Object
-
#mail(**kargs) ⇒ Object
Sends an email.
-
#new_entry(data, dependencies: {}) ⇒ Eco::API::Common::People::PersonEntry
Builds the entry for the given data.
- #new_person(**keyed_args) ⇒ Ecoportal::API::Internal::Person
-
#new_preset(input) ⇒ Hash
Builds the presets using the usergroup ids of the input.
-
#parse_attribute(attr, source) ⇒ Object
Allows to use the defined parsers.
-
#person_factory(schema: nil) ⇒ Eco::API::Common::People::PersonFactory
Helper to obtain a PersonFactory.
- #policy_groups ⇒ Object
- #post_launch ⇒ Object
- #process_case(name, io: nil, type: nil, **params) ⇒ Object
-
#s3upload(content: nil, file: nil, directory: nil, recurse: false, link: false) ⇒ String+
Uploads content into a file, a file or a directory to S3.
-
#schema ⇒ String, Ecoportal::API::V1::PersonSchema
Current active session's schema.
-
#schema=(value) ⇒ Object
Sets the current target
PersonSchemaof this session. - #schemas ⇒ Object
- #summary ⇒ Object
- #tagtree ⇒ Object
- #usecases ⇒ Object
- #workflow(io:) ⇒ Object
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.
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
#batch ⇒ Eco::API::Session::Batch
Returns provides helper to launch batch operations.
27 28 29 |
# File 'lib/eco/api/session.rb', line 27 def batch @batch ||= Batch.new(enviro) end |
#csv_entries(file) ⇒ Eco::API::Common::People::Entries
Generates an entries collection from a csv input file.
154 155 156 |
# File 'lib/eco/api/session.rb', line 154 def csv_entries(file) return entries(file: file, format: :csv) end |
#discarded_entries ⇒ Eco::API::Common::People::Entries
requires session.config.people.discarded_file to be defined.
Generates the collection of entries that should be discarded from an update.
161 162 163 164 165 166 |
# File 'lib/eco/api/session.rb', line 161 def discarded_entries return @discarded_entries if instance_variable_defined?(:@discarded_entries) file = config.people.discarded_file file = file_manager.dir.file(file) @discarded_entries = csv_entries(file) end |
#do ⇒ Eco::API::Session::Task
Helper to perform multiple operations in one go.
22 23 24 |
# File 'lib/eco/api/session.rb', line 22 def do @task ||= Task.new(enviro) end |
#entries(*args) ⇒ Eco::API::Common::People::Entries
Returns collection of entries.
144 145 146 147 148 |
# File 'lib/eco/api/session.rb', line 144 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
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/eco/api/session.rb', line 85 def entry_factory(schema: nil) schema = to_schema(schema) || self.schema return @entry_factories[schema&.id] if @entry_factories.key?(schema&.id) # TODO: check PersonEntry#to_internal and #to_external in init_attr_trackers # => when attr_map is avoided, it doesn't work as it should 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({ schema: schema, person_parser: config.people.parser, attr_map: Eco::Data::Mapper.new(mappings), logger: logger }) end |
#export(*args) ⇒ Object
125 126 127 |
# File 'lib/eco/api/session.rb', line 125 def export(*args) entry_factory.export(*args) end |
#job_group(name, order: :last) ⇒ Object
191 192 193 194 195 196 197 198 |
# File 'lib/eco/api/session.rb', line 191 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 ⇒ Object
187 188 189 |
# File 'lib/eco/api/session.rb', line 187 def job_groups @job_groups ||= Batch::JobsGroups.new(enviro) end |
#jobs_launch(simulate: false) ⇒ Object
200 201 202 |
# File 'lib/eco/api/session.rb', line 200 def jobs_launch(simulate: false) job_groups.launch(simulate: simulate) end |
#login_providers ⇒ Object
37 38 39 |
# File 'lib/eco/api/session.rb', line 37 def login_providers config.login_providers end |
#mail(**kargs) ⇒ Object
Sends an email
210 211 212 213 214 215 216 217 |
# File 'lib/eco/api/session.rb', line 210 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 |
#new_entry(data, dependencies: {}) ⇒ Eco::API::Common::People::PersonEntry
Builds the entry for the given data.
138 139 140 |
# File 'lib/eco/api/session.rb', line 138 def new_entry(data, dependencies: {}) entry_factory.new(data, dependencies: dependencies) end |
#new_person(**keyed_args) ⇒ Ecoportal::API::Internal::Person
131 132 133 |
# File 'lib/eco/api/session.rb', line 131 def new_person(**keyed_args) person_factory.new(**keyed_args) end |
#new_preset(input) ⇒ Hash
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.
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/eco/api/session.rb', line 69 def new_preset(input) case input when Ecoportal::API::Internal::Person presets_factory.new(*input&.account&.policy_group_ids) when Array presets_factory.new(*input) else presets_factory.new(input) end end |
#parse_attribute(attr, source) ⇒ Object
the use of these method requires to know which is the expected format of source
Allows to use the defined parsers
118 119 120 121 122 123 |
# File 'lib/eco/api/session.rb', line 118 def parse_attribute(attr, source) 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
109 110 111 112 |
# File 'lib/eco/api/session.rb', line 109 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_groups ⇒ Object
32 33 34 |
# File 'lib/eco/api/session.rb', line 32 def policy_groups config.policy_groups end |
#post_launch ⇒ Object
178 179 180 |
# File 'lib/eco/api/session.rb', line 178 def post_launch @post_launch ||= config.post_launch.select(usecases) end |
#process_case(name, io: nil, type: nil, **params) ⇒ Object
182 183 184 185 |
# File 'lib/eco/api/session.rb', line 182 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
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/eco/api/session.rb', line 229 def s3upload(content: nil, file: nil, directory: nil, recurse: false, link: false) if s3uploader? if content == :target path = self.do.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 ⇒ String, Ecoportal::API::V1::PersonSchema
Returns current active session's schema.
52 53 54 55 |
# File 'lib/eco/api/session.rb', line 52 def schema self.schema = config.people.default_schema || schemas.first unless @schema @schema end |
#schema=(value) ⇒ Object
observe that it is essential for the parsers/serialisers to identify target/present attributes.
Sets the current target PersonSchema of this session.
60 61 62 63 |
# File 'lib/eco/api/session.rb', line 60 def schema=(value) @schema = to_schema(value) self end |
#schemas ⇒ Object
47 48 49 |
# File 'lib/eco/api/session.rb', line 47 def schemas config.schemas end |
#summary ⇒ Object
204 205 206 |
# File 'lib/eco/api/session.rb', line 204 def summary job_groups.summary end |
#tagtree ⇒ Object
42 43 44 |
# File 'lib/eco/api/session.rb', line 42 def tagtree config.tagtree(enviro: enviro) end |
#usecases ⇒ Object
174 175 176 |
# File 'lib/eco/api/session.rb', line 174 def usecases @usecases ||= Eco::API::UseCases::DefaultCases.new.merge(config.usecases) end |
#workflow(io:) ⇒ Object
168 169 170 171 172 |
# File 'lib/eco/api/session.rb', line 168 def workflow(io:) config.workflow.tap do |wf| yield(wf, io) if block_given? end end |