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
(forget
batch 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
mode
that thebatch
was run with. -
#queue ⇒ Array<Hash>, ...
readonly
source_queue
with no repeated elements (note: observe that the elimination of duplicates could fail). -
#root ⇒ Eco::API::Session::Job
the
job
that launched thebatch
. -
#source_queue ⇒ Array<Hash>, ...
readonly
The queue as it was originally made (it could contain duplicates).
Attributes inherited from Common::Session::BaseSession
#config, #environment, #session
Attributes included from Language::AuxiliarLogger
Get-specific helpers collapse
-
#people ⇒ Array<Ecoportal::API::V1::Person>, Array<Ecoportal::API::Internal::Person>
When the batch method has been
get
, it gathers into anArray
the 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
Person
object 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
Person
object for the input entry objectkey
. - #set_people_match(key, people) ⇒ Object
- #set_person_match(key, person) ⇒ Object
Class Method Summary collapse
Instance Method Summary collapse
-
#[](key) ⇒ Ecoportal::API::Common::BatchResponse
Get the assciated
reponse
of an input entry objectkey
. -
#[]=(key, response) ⇒ Object
Associates an input entry object
key
to itsresponse
. -
#errors ⇒ Eco::API::Session::Batch::Errors
Errors object helper.
-
#errors? ⇒ Boolean
true
if there were Server errors,false
otherwise. -
#initialize(e, queue:, method:, mode: :exact) ⇒ Status
constructor
A new instance of Status.
-
#received?(key) ⇒ Boolean
Has the entry
key
been queried to the server?. -
#success?(key) ⇒ Boolean
Has the entry
key
's query to the server been successful. -
#to_index(key) ⇒ Integer
Helper to transform any
key
to anInteger
index. -
#valid_index(index: nil, entry: nil) ⇒ Integer
Index validator to make this object reliable.
Methods inherited from Common::Session::BaseSession
#api, #api?, #fatal, #file_manager, #logger, #mailer, #mailer?, #s3uploader, #s3uploader?, #sftp, #sftp?
Methods included from Language::AuxiliarLogger
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
:exact
mode will keep the order of found people as per order of finalqueue
:search
mode will just gather the results (order won't match)
When the batch method has been get
, it gathers into an Array
the people found.
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/eco/api/session/batch/status.rb', line 173 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
159 160 161 |
# File 'lib/eco/api/session/batch/status.rb', line 159 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
139 140 141 142 |
# File 'lib/eco/api/session/batch/status.rb', line 139 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
get
withq
- 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
151 152 153 |
# File 'lib/eco/api/session/batch/status.rb', line 151 def person_match(key) @person_match[to_index(key)] end |
#received?(key) ⇒ Boolean
Has the entry key
been queried to the server?
102 103 104 |
# File 'lib/eco/api/session/batch/status.rb', line 102 def received?(key) !!self[key] end |
#set_people_match(key, people) ⇒ Object
163 164 165 |
# File 'lib/eco/api/session/batch/status.rb', line 163 def set_people_match(key, people) @people_match[to_index(key)] = people end |
#set_person_match(key, person) ⇒ Object
155 156 157 |
# File 'lib/eco/api/session/batch/status.rb', line 155 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
109 110 111 |
# File 'lib/eco/api/session/batch/status.rb', line 109 def success?(key) self[key]&.success? end |
#to_index(key) ⇒ Integer
Helper to transform any key
to an Integer
index
116 117 118 |
# File 'lib/eco/api/session/batch/status.rb', line 116 def to_index(key) key.is_a?(Integer) ? valid_index(index: key) : valid_index(entry: key) end |
#valid_index(index: nil, entry: nil) ⇒ Integer
Index validator to make this object reliable
122 123 124 125 126 127 128 129 130 |
# File 'lib/eco/api/session/batch/status.rb', line 122 def valid_index(index: nil, entry: nil) index ||= @hash[entry] unless index && index < @queue.length msg = "You must provide either the index on the final 'queue' or the original entry object of the batch. " msg += "Given, index=#{index.class}, entry:#{entry.class}" fatal msg end index end |