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 ⇒ Object
Nativelly to this gem, a batch against the server is lauched by an Eco::API::Session::Job.
-
#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(ev, 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(ev, queue:, method:, mode: :exact) ⇒ Status
Returns a new instance of Status.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/eco/api/session/batch/status.rb', line 42 def initialize(ev, queue:, method:, mode: :exact) super(ev) msg = "In batch operations you must batch an Enumerable. Received: #{queue.class}" fatal(msg) unless 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 log(: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.
21 22 23 |
# File 'lib/eco/api/session/batch/status.rb', line 21 def modes @modes end |
Instance Attribute Details
#method ⇒ Object (readonly)
Returns the value of attribute method.
29 30 31 |
# File 'lib/eco/api/session/batch/status.rb', line 29 def method @method end |
#mode ⇒ Symbol
the mode
that the batch
was run with
17 18 19 |
# File 'lib/eco/api/session/batch/status.rb', line 17 def mode @mode end |
#queue ⇒ Array<Hash>, ... (readonly)
source_queue
with no repeated elements (note: observe that the elimination of duplicates could fail)
17 18 19 |
# File 'lib/eco/api/session/batch/status.rb', line 17 def queue @queue end |
#root ⇒ Object
Nativelly to this gem, a batch against the server is lauched by an Eco::API::Session::Job.
When the batch returns the BatchStatus
, the Job
assigns itself as root
of it.
17 18 19 |
# File 'lib/eco/api/session/batch/status.rb', line 17 def root @root end |
#source_queue ⇒ Array<Hash>, ... (readonly)
The queue as it was originally made (it could contain duplicates)
17 18 19 |
# File 'lib/eco/api/session/batch/status.rb', line 17 def source_queue @source_queue end |
Class Method Details
.valid_mode?(value) ⇒ Boolean
23 24 25 |
# File 'lib/eco/api/session/batch/status.rb', line 23 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
99 100 101 |
# File 'lib/eco/api/session/batch/status.rb', line 99 def [](key) @responses[to_index(key)] end |
#[]=(key, response) ⇒ Object
Associates an input entry object key
to its response
107 108 109 |
# File 'lib/eco/api/session/batch/status.rb', line 107 def []=(key, response) @responses[to_index(key)] = response end |
#errors ⇒ Eco::API::Session::Batch::Errors
Returns errors object helper.
81 82 83 |
# File 'lib/eco/api/session/batch/status.rb', line 81 def errors @errors ||= Eco::API::Session::Batch::Errors.new(status: self) end |
#errors? ⇒ Boolean
Returns true
if there were Server errors, false
otherwise.
87 88 89 |
# File 'lib/eco/api/session/batch/status.rb', line 87 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.
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/eco/api/session/batch/status.rb', line 187 def people msg = "This batch wasn't a 'get'. Can't obtain people without 'get' method" fatal msg 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
172 173 174 |
# File 'lib/eco/api/session/batch/status.rb', line 172 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
152 153 154 |
# File 'lib/eco/api/session/batch/status.rb', line 152 def person(key) self[key].result if success?(key) 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
164 165 166 |
# File 'lib/eco/api/session/batch/status.rb', line 164 def person_match(key) @person_match[to_index(key)] end |
#received?(key) ⇒ Boolean
Has the entry key
been queried to the server?
114 115 116 |
# File 'lib/eco/api/session/batch/status.rb', line 114 def received?(key) !!self[key] end |
#set_people_match(key, people) ⇒ Object
176 177 178 |
# File 'lib/eco/api/session/batch/status.rb', line 176 def set_people_match(key, people) @people_match[to_index(key)] = people end |
#set_person_match(key, person) ⇒ Object
168 169 170 |
# File 'lib/eco/api/session/batch/status.rb', line 168 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
121 122 123 |
# File 'lib/eco/api/session/batch/status.rb', line 121 def success?(key) self[key]&.success? end |
#to_index(key) ⇒ Integer
Helper to transform any key
to an Integer
index
128 129 130 |
# File 'lib/eco/api/session/batch/status.rb', line 128 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
134 135 136 137 138 139 140 141 142 143 |
# File 'lib/eco/api/session/batch/status.rb', line 134 def valid_index(index: nil, entry: nil) index ||= @hash[entry] return index if index && index < @queue.length msg = "You must provide either the index on the final 'queue' " msg << "or the original entry object of the batch. " msg << "Given, index=#{index.class}, entry:#{entry.class}" fatal msg end |