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. Its aim is to manage the errors of the batch status.

Defined Under Namespace

Classes: ErrorCache

Instance Attribute Summary collapse

Status object shortcuts collapse

Pure errors helper methods collapse

Messaging methods collapse

Instance Method Summary collapse

Constructor Details

#initialize(status:) ⇒ Errors

Returns a new instance of Errors.

Parameters:



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

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



51
52
53
# File 'lib/eco/api/session/batch/errors.rb', line 51

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

#by_typeHash

Groups entries with error type

Returns:

  • (Hash)

    where each key is a type error and each value is an Array of entries that got that error



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

def by_type
  errors.group_by do |e|
    e.type
  end.transform_values do |arr|
    arr.map {|e| e.entry}
  end
end

#countInteger

Returns the number of entries that got error.

Returns:

  • (Integer)

    the number of entries that got error.



56
57
58
# File 'lib/eco/api/session/batch/errors.rb', line 56

def count
  entries.length
end

#errorsArray<Hash>

For all the entries with errors generates a Hash object

Returns:

  • (Array<Hash>)

    where each Hash has

    1. :type -> the error type
    2. :err -> the error class of that :type
    3. :entry -> the entry that generated the error


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/eco/api/session/batch/errors.rb', line 65

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

#loggerObject



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

def logger
  status.logger
end

#messageObject



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

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]


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

def method
  status.method
end

#person_ref(entry) ⇒ Object



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

def person_ref(entry)
  row_str = (row = get_row(entry)) ? "(row: #{row}) " : nil
  "#{row_str}(id: '#{get_attr(entry, :id)}') '#{get_attr(entry, :name)}' ('#{get_attr(entry, :external_id)}': '#{get_attr(entry, :email)}')"
end


104
105
106
107
108
109
110
111
# File 'lib/eco/api/session/batch/errors.rb', line 104

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

#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]


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

def queue
  status.queue
end

#sessionEco::API::Session

Returns currently active session.

Returns:



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

def session
  status.session
end

#to_index(*args) ⇒ Object

Parameters:

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]


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

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