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

#api, #config, #environment, #file_manager, #logger, #session

Get-specific helpers collapse

Class Method Summary collapse

Instance Method Summary collapse

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.

Parameters:



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

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

#methodObject (readonly)

Returns the value of attribute method.



19
20
21
# File 'lib/eco/api/session/batch/status.rb', line 19

def method
  @method
end

#modeSymbol

the mode that the batch was run with

Returns:

  • (Symbol)

    the current value of mode



15
16
17
# File 'lib/eco/api/session/batch/status.rb', line 15

def mode
  @mode
end

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

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

Returns:



15
16
17
# File 'lib/eco/api/session/batch/status.rb', line 15

def queue
  @queue
end

#rootEco::API::Session::Job

the job that launched the batch

Returns:

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

    the current value of root



15
16
17
# File 'lib/eco/api/session/batch/status.rb', line 15

def root
  @root
end

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

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

Returns:



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

Returns:

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

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)


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

Parameters:

See Also:



95
96
97
# File 'lib/eco/api/session/batch/status.rb', line 95

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

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

Returns errors object helper.

Returns:



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.

Returns:

  • (Boolean)

    true if there were Server errors, false otherwise

See Also:



77
78
79
# File 'lib/eco/api/session/batch/status.rb', line 77

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:

Raises:

  • (Exception)

    when the method of the batch operation was other than get



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

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



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

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



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?

Parameters:

Returns:

  • (Boolean)

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



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

Parameters:

Returns:

  • (Boolean)

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



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

Parameters:

Returns:

  • (Integer)

    the index that key has in the final queue



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

Returns:

  • (Integer)

    the actual index



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