Class: Eco::API::Session::Batch::Errors

Inherits:
Object
  • Object
show all
Defined in:
lib/eco/api/session/batch/errors.rb

Overview

Helper object linked to a batch status

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status:) ⇒ Errors

Returns a new instance of Errors.

Parameters:



12
13
14
15
# File 'lib/eco/api/session/batch/errors.rb', line 12

def initialize(status:)
  "Expected Batch::Status as root. Given: #{status.class}" unless status.is_a?(Eco::API::Session::Batch::Status)
  @status = status
end

Instance Attribute Details

#statusObject (readonly)



9
10
11
# File 'lib/eco/api/session/batch/errors.rb', line 9

def status
  @status
end

Instance Method Details

#any?Boolean

Was there any Sever (reply) error as a result of this batch?

Returns:

  • (Boolean)

    true if any of the queried entries got an unsuccessful Ecoportal::API::Common::BatchResponse



43
44
45
# File 'lib/eco/api/session/batch/errors.rb', line 43

def any?
  queue.any? {|query| !status[query].success?}
end

#by_typeObject



84
85
86
87
88
89
90
# File 'lib/eco/api/session/batch/errors.rb', line 84

def by_type
  errors.group_by do |h|
    h[:type]
  end.transform_values do |arr|
    arr.map {|h| h[:entry]}
  end
end

#countObject



92
93
94
# File 'lib/eco/api/session/batch/errors.rb', line 92

def count
  entries.length
end

#entriesArray<Hash>, ...

Note:

discards those that did not get response from the Server (so those that were not queried)

  • please, observe that this can only happen if there were repeated entries in the source_queue

Input entries that got error response from the Server.

Returns:

Raises:

  • (Exception)

    if there are elements of the final queue that did not get response



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/eco/api/session/batch/errors.rb', line 52

def entries
  queue.each_with_index.map do |query, i|
    unless response = status[i]
      msg  = "Error: query with no response. You might have duplicated entries in your queue.\n"
      msg += "Queue length: #{queue.length}; Queue elements class: #{queue.first.class}\n"
      msg += "Query with no response. Person: #{person_ref(query)}\n"
      queue.map do |entry|
        if [:id, :external_id, :email].any? {|attr| (v = get_attr(entry, attr)) && v == get_attr(query, attr)}
          msg += "It could be this peson entry (idx: #{to_index(entry)}): #{person_ref(entry)}\n"
        end
      end
      raise msg
    end

    response.success?? nil : query
  end.compact
end

#errorsObject



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/eco/api/session/batch/errors.rb', line 70

def errors
  entries.each_with_object([]) do |entry, arr|
    if errs = status[entry].body["errors"]
      errs.each do |msg|
        arr.push({
          type:  klass = Eco::API::Error.get_type(msg),
          err:   klass.new(err_msg: msg, entry: entry, session: session),
          entry: entry
        })
      end
    end
  end
end

#loggerObject



37
38
39
# File 'lib/eco/api/session/batch/errors.rb', line 37

def logger
  status.logger
end

#messageObject



120
121
122
123
124
125
126
127
# File 'lib/eco/api/session/batch/errors.rb', line 120

def message
  msgs = strs
  if msgs.length > 0
    "There were #{msgs.length} errors:\n" + msgs.join("\n")
  else
    "There were no errors for the current batch '#{method}'!! ;)"
  end
end

#methodObject

See Also:

  • Eco::API::Session::Batch::Errors.[Eco[Eco::API[Eco::API::Session[Eco::API::Session::Batch[Eco::API::Session::Batch::Status[Eco::API::Session::Batch::Status#method]


23
24
25
# File 'lib/eco/api/session/batch/errors.rb', line 23

def method
  status.method
end


129
130
131
132
133
134
135
136
# File 'lib/eco/api/session/batch/errors.rb', line 129

def print
  msgs = strs
  if msgs.length > 0
    logger.error("There were #{msgs.length} errors:\n" + msgs.join("\n"))
  else
    logger.info("There were no errors for the current batch '#{method}'!! ;)")
  end
end


114
115
116
117
118
# File 'lib/eco/api/session/batch/errors.rb', line 114

def print_one(key)
  unless status.success?(key)
    logger.error(str(key))
  end
end

#queueObject

See Also:

  • Eco::API::Session::Batch::Errors.[Eco[Eco::API[Eco::API::Session[Eco::API::Session::Batch[Eco::API::Session::Batch::Status[Eco::API::Session::Batch::Status#queue]


18
19
20
# File 'lib/eco/api/session/batch/errors.rb', line 18

def queue
  status.queue
end

#sessionEco::API::Session

Returns currently active session.

Returns:



33
34
35
# File 'lib/eco/api/session/batch/errors.rb', line 33

def session
  status.session
end

#str(key) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/eco/api/session/batch/errors.rb', line 96

def str(key)
  msg = ""
  unless status.success?(key)
    i        = to_index(key)
    entry    = queue.to_a[i]
    response = status[i]
    msg      = "Error #{response.status}: #{response.body}\n"
    msg     += "-- Failed to batch #{method} (entry #{i+1}). Person: #{person_ref(entry)}"
  end
  msg
end

#strsObject



108
109
110
111
112
# File 'lib/eco/api/session/batch/errors.rb', line 108

def strs
  by_type.values.flatten(1).each_with_object([]) do |query, msgs|
    msgs.push(str(query))
  end
end

#to_index(*args) ⇒ Object

See Also:

  • Eco::API::Session::Batch::Errors.[Eco[Eco::API[Eco::API::Session[Eco::API::Session::Batch[Eco::API::Session::Batch::Status[Eco::API::Session::Batch::Status#to_index]


28
29
30
# File 'lib/eco/api/session/batch/errors.rb', line 28

def to_index(*args)
  status.to_index(*args)
end