Class: Eco::API::Session::Batch::Status

Inherits:
Common::Session::BaseSession show all
Defined in:
lib/eco/api/session/batch/status.rb

Overview

The Batch::Status class aims to offer support to keep memory on:

  1. what has happened during the execution of a Session::Batch (i.e. errors)
  2. be able to build a summary of what happened
  3. gather the people that was returned as a result of the batch (for get batch type)

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes inherited from Common::Session::BaseSession

#config, #environment, #session

Attributes included from Language::AuxiliarLogger

#logger

Get-specific helpers collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Common::Session::BaseSession

#api, #api?, #fatal, #file_manager, #logger, #mailer, #mailer?, #s3uploader, #s3uploader?, #sftp, #sftp?

Methods included from Language::AuxiliarLogger

#log

Constructor Details

#initialize(ev, queue:, method:, mode: :exact) ⇒ Status

Returns a new instance of Status.

Parameters:

  • e (Eco::API::Common::Session::Environment)

    requires a session environment, as any child of Eco::API::Common::Session::BaseSession

  • queue (Array<Hash>, Array<Ecoportal::API::V1::Person>, Array<Ecoportal::API::Internal::Person>)

    the source_queue

  • method (Symbol)

    the type of batch operation

  • mode (Symbol) (defaults to: :exact)

    the mode of batch operation. It can be :search or :exact



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

.modesObject (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

#methodObject (readonly)

Returns the value of attribute method.



29
30
31
# File 'lib/eco/api/session/batch/status.rb', line 29

def method
  @method
end

#modeSymbol

the mode that the batch was run with

Returns:

  • (Symbol)

    the current value of mode



17
18
19
# File 'lib/eco/api/session/batch/status.rb', line 17

def mode
  @mode
end

#queueArray<Hash>, ... (readonly)

source_queue with no repeated elements (note: observe that the elimination of duplicates could fail)

Returns:

  • (Array<Hash>, Array<Ecoportal::API::V1::Person>, Array<Ecoportal::API::Internal::Person>)

    the current value of queue



17
18
19
# File 'lib/eco/api/session/batch/status.rb', line 17

def queue
  @queue
end

#rootObject

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.

Parameters:

  • object (Eco::API::Session::Job)


17
18
19
# File 'lib/eco/api/session/batch/status.rb', line 17

def root
  @root
end

#source_queueArray<Hash>, ... (readonly)

The queue as it was originally made (it could contain duplicates)

Returns:

  • (Array<Hash>, Array<Ecoportal::API::V1::Person>, Array<Ecoportal::API::Internal::Person>)

    the current value of source_queue



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

Returns:

  • (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

Parameters:

  • key (Integer, Hash, Ecoportal::API::V1::Person, Ecoportal::API::Internal::Person)

    these are the index options:

    1. Integer: index/position of the entry in the final queue
    2. Hash: entry queries can be raw Hash objects
    3. Person object: the most common case is to use the person wrapper classes of the Ecoportal::API namespace.

Returns:

  • (Ecoportal::API::Common::BatchResponse)


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

Parameters:

  • key (Integer, Hash, Ecoportal::API::V1::Person, Ecoportal::API::Internal::Person)
  • response

    key [Ecoportal::API::Common::BatchResponse]

See Also:



107
108
109
# File 'lib/eco/api/session/batch/status.rb', line 107

def []=(key, response)
  @responses[to_index(key)] = response
end

#errorsEco::API::Session::Batch::Errors

Returns errors object helper.

Returns:



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.

Returns:

  • (Boolean)

    true if there were Server errors, false otherwise

See Also:



87
88
89
# File 'lib/eco/api/session/batch/status.rb', line 87

def errors?
  errors.any?
end

#peopleArray<Ecoportal::API::V1::Person>, Array<Ecoportal::API::Internal::Person>

Note:

here is where the used mode gets relevance:

  • while :exact mode will keep the order of found people as per order of final queue
  • :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.

Returns:

  • (Array<Ecoportal::API::V1::Person>, Array<Ecoportal::API::Internal::Person>)

    all the people that has been found.

Raises:

  • (Exception)

    when the method of the batch operation was other than get



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

Note:

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

Parameters:

  • key (Integer, Hash, Ecoportal::API::V1::Person, Ecoportal::API::Internal::Person)

Returns:

  • (Ecoportal::API::V1::Person, Ecoportal::API::Internal::Person)

See Also:



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

Note:
  • it only makes sense when the batch method used was get with q
  • found using a search criteria (mode == :search), as opposite to find the person directly by external_id

The person we got from the Server wrapped to the Person object for the input entry object key

Parameters:

  • key (Integer, Hash, Ecoportal::API::V1::Person, Ecoportal::API::Internal::Person)

Returns:

  • (Ecoportal::API::V1::Person, Ecoportal::API::Internal::Person)

See Also:



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?

Parameters:

  • key (Integer, Hash, Ecoportal::API::V1::Person, Ecoportal::API::Internal::Person)

Returns:

  • (Boolean)

    true if input entry key has an associated Ecoportal::API::Common::BatchResponse



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

Parameters:

  • key (Integer, Hash, Ecoportal::API::V1::Person, Ecoportal::API::Internal::Person)

Returns:

  • (Boolean)

    true if input entry key has not had a server Error during the query



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

Parameters:

  • key (Integer, Hash, Ecoportal::API::V1::Person, Ecoportal::API::Internal::Person)

Returns:

  • (Integer)

    the index that key has in the final queue



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

Returns:

  • (Integer)

    the actual index



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