Class: Eco::API::Session::Batch::Status
- Inherits:
-
Common::Session::BaseSession
- Object
- Common::Session::BaseSession
- Eco::API::Session::Batch::Status
- Defined in:
- lib/eco/api/session/batch/status.rb
Overview
The Batch::Status class aims to offer support to keep memory on:
- what has happened during the execution of a
Session::Batch(i.e. errors) - be able to build a summary of what happened
- gather the people that was returned as a result of the
batch(forgetbatch type)
Class Attribute Summary collapse
-
.modes ⇒ Object
readonly
Returns the value of attribute modes.
Instance Attribute Summary collapse
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#mode ⇒ Symbol
the
modethat thebatchwas run with. -
#queue ⇒ Array<Hash>, ...
readonly
source_queuewith no repeated elements (note: observe that the elimination of duplicates could fail). -
#root ⇒ Eco::API::Session::Job
the
jobthat launched thebatch. -
#source_queue ⇒ Array<Hash>, ...
readonly
The queue as it was originally made (it could contain duplicates).
Attributes inherited from Common::Session::BaseSession
#api, #config, #environment, #file_manager, #logger, #session
Class Method Summary collapse
Instance Method Summary collapse
-
#[](key) ⇒ Ecoportal::API::Common::BatchResponse
Get the assciated
reponseof an input entry objectkey. -
#[]=(key, response) ⇒ Object
Associates an input entry object
keyto itsresponse. -
#errors ⇒ Eco::API::Session::Batch::Errors
Errors object helper.
-
#errors? ⇒ Boolean
trueif there were Server errors,falseotherwise. -
#initialize(e, queue:, method:, mode: :exact) ⇒ Status
constructor
A new instance of Status.
-
#people ⇒ Array<Ecoportal::API::V1::Person>, Array<Ecoportal::API::Internal::Person>
When the batch method has been
get, it gathers into anArraythe people found. - #people_match(key) ⇒ Object
-
#person(key) ⇒ Ecoportal::API::V1::Person, Ecoportal::API::Internal::Person
The person we got from the Server wrapped to the
Personobject for the input entry objectkey. -
#person_match(key) ⇒ Ecoportal::API::V1::Person, Ecoportal::API::Internal::Person
The person we got from the Server wrapped to the
Personobject for the input entry objectkey. -
#received?(key) ⇒ Boolean
Has the entry
keybeen queried to the server?. - #set_people_match(key, people) ⇒ Object
- #set_person_match(key, person) ⇒ Object
-
#success?(key) ⇒ Boolean
Has the entry
key's query to the server been successful. -
#to_index(key) ⇒ Integer
Helper to transform any
keyto anIntegerindex. -
#valid_index(index: nil, entry: nil) ⇒ Object
Index validator to make this object reliable.
Methods inherited from Common::Session::BaseSession
#enviro=, #fatal, #fm, #mailer, #mailer?, #s3uploader, #s3uploader?, #sftp, #sftp?
Constructor Details
#initialize(e, queue:, method:, mode: :exact) ⇒ Status
Returns a new instance of Status.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/eco/api/session/batch/status.rb', line 34 def initialize(e, queue:, method:, mode: :exact) super(e) fatal("In batch operations you must batch an Enumerable. Received: #{queue}") unless queue && queue.is_a?(Enumerable) self.mode = mode @method = method @source_queue = queue que = queue.to_a que = queue if queue.respond_to?(:uniq) if que.length != que.uniq.length logger.warn("Please, review your entries-to-query builder, you have repeated entries") queue = que.uniq end @queue = queue @hash = @queue.each_with_index.map do |entry, i| [entry, i] end.to_h @responses = [] @person_match = [] @people_match = Array.new(@queue.length, []) end |
Class Attribute Details
.modes ⇒ Object (readonly)
Returns the value of attribute modes.
23 24 25 |
# File 'lib/eco/api/session/batch/status.rb', line 23 def modes @modes end |
Instance Attribute Details
#method ⇒ Object (readonly)
Returns the value of attribute method.
19 20 21 |
# File 'lib/eco/api/session/batch/status.rb', line 19 def method @method end |
#mode ⇒ Symbol
the mode that the batch was run with
15 16 17 |
# File 'lib/eco/api/session/batch/status.rb', line 15 def mode @mode end |
#queue ⇒ Array<Hash>, ... (readonly)
source_queue with no repeated elements (note: observe that the elimination of duplicates could fail)
15 16 17 |
# File 'lib/eco/api/session/batch/status.rb', line 15 def queue @queue end |
#root ⇒ Eco::API::Session::Job
the job that launched the batch
15 16 17 |
# File 'lib/eco/api/session/batch/status.rb', line 15 def root @root end |
#source_queue ⇒ Array<Hash>, ... (readonly)
The queue as it was originally made (it could contain duplicates)
15 16 17 |
# File 'lib/eco/api/session/batch/status.rb', line 15 def source_queue @source_queue end |
Class Method Details
.valid_mode?(value) ⇒ Boolean
25 26 27 |
# File 'lib/eco/api/session/batch/status.rb', line 25 def valid_mode?(value) modes.include?(value) end |
Instance Method Details
#[](key) ⇒ Ecoportal::API::Common::BatchResponse
Get the assciated reponse of an input entry object key
87 88 89 |
# File 'lib/eco/api/session/batch/status.rb', line 87 def [](key) @responses[to_index(key)] end |
#[]=(key, response) ⇒ Object
Associates an input entry object key to its response
95 96 97 |
# File 'lib/eco/api/session/batch/status.rb', line 95 def []=(key, response) @responses[to_index(key)] = response end |
#errors ⇒ Eco::API::Session::Batch::Errors
Returns errors object helper.
71 72 73 |
# File 'lib/eco/api/session/batch/status.rb', line 71 def errors @errors ||= Eco::API::Session::Batch::Errors.new(status: self) end |
#errors? ⇒ Boolean
Returns true if there were Server errors, false otherwise.
77 78 79 |
# File 'lib/eco/api/session/batch/status.rb', line 77 def errors? errors.any? end |
#people ⇒ Array<Ecoportal::API::V1::Person>, Array<Ecoportal::API::Internal::Person>
here is where the used mode gets relevance:
- while
:exactmode will keep the order of found people as per order of finalqueue :searchmode will just gather the results (order won't match)
When the batch method has been get, it gathers into an Array the people found.
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/eco/api/session/batch/status.rb', line 152 def people fatal "This batch wasn't a 'get'. Can't obtain people without 'get' method" unless method == :get if mode == :exact out = Array(queue.length) @responses.each_with_index do |response, i| out[i] = response.result if response.success? end elsif mode == :search out = [] queue.each_with_index.map do |entry, i| pers = person(entry) pers ||= person_match(entry) ppl = pers ? [pers] : people_match(entry) out += ppl end end out end |
#people_match(key) ⇒ Object
124 125 126 |
# File 'lib/eco/api/session/batch/status.rb', line 124 def people_match(key) @people_match[to_index(key)] end |
#person(key) ⇒ Ecoportal::API::V1::Person, Ecoportal::API::Internal::Person
it only makes sense when the used batch method was get
The person we got from the Server wrapped to the Person object for the input entry object key
104 105 106 107 |
# File 'lib/eco/api/session/batch/status.rb', line 104 def person(key) return self[key].result if success?(key) nil end |
#person_match(key) ⇒ Ecoportal::API::V1::Person, Ecoportal::API::Internal::Person
- it only makes sense when the batch method used was
getwithq - found using a search criteria (
mode==:search), as opposite to find the person directly byexternal_id
The person we got from the Server wrapped to the Person object for the input entry object key
116 117 118 |
# File 'lib/eco/api/session/batch/status.rb', line 116 def person_match(key) @person_match[to_index(key)] end |
#received?(key) ⇒ Boolean
Has the entry key been queried to the server?
135 136 137 |
# File 'lib/eco/api/session/batch/status.rb', line 135 def received?(key) !!self[key] end |
#set_people_match(key, people) ⇒ Object
128 129 130 |
# File 'lib/eco/api/session/batch/status.rb', line 128 def set_people_match(key, people) @people_match[to_index(key)] = people end |
#set_person_match(key, person) ⇒ Object
120 121 122 |
# File 'lib/eco/api/session/batch/status.rb', line 120 def set_person_match(key, person) @person_match[to_index(key)] = person end |
#success?(key) ⇒ Boolean
Has the entry key 's query to the server been successful
142 143 144 |
# File 'lib/eco/api/session/batch/status.rb', line 142 def success?(key) self[key]&.success? end |
#to_index(key) ⇒ Integer
Helper to transform any key to an Integer index
174 175 176 |
# File 'lib/eco/api/session/batch/status.rb', line 174 def to_index(key) key.is_a?(Integer) ? valid_index(index: key) : valid_index(entry: key) end |
#valid_index(index: nil, entry: nil) ⇒ Object
Index validator to make this object reliable
179 180 181 182 183 184 185 |
# File 'lib/eco/api/session/batch/status.rb', line 179 def valid_index(index: nil, entry: nil) index ||= @hash[entry] unless index && index < @queue.length fatal "You must provide either the index on the final 'queue' or the original entry object of the batch" end index end |