Class: RDStation::Error::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/rdstation/error/formatter.rb

Instance Method Summary collapse

Constructor Details

#initialize(error_response) ⇒ Formatter

Returns a new instance of Formatter.



8
9
10
# File 'lib/rdstation/error/formatter.rb', line 8

def initialize(error_response)
  @error_response = error_response
end

Instance Method Details

#error_formatObject



73
74
75
# File 'lib/rdstation/error/formatter.rb', line 73

def error_format
  @error_format ||= RDStation::Error::Format.new(errors)
end

#errorsObject



77
78
79
# File 'lib/rdstation/error/formatter.rb', line 77

def errors
  @errors ||= @error_response.fetch('errors', @error_response)
end

#from_flat_hashObject



44
45
46
# File 'lib/rdstation/error/formatter.rb', line 44

def from_flat_hash
  [errors]
end

#from_hash_of_hashesObject



63
64
65
66
67
68
69
70
71
# File 'lib/rdstation/error/formatter.rb', line 63

def from_hash_of_hashes
  array_of_errors = []
  errors.each do |attribute_name, errors|
    result = build_error_from_multilingual_hash(attribute_name, errors)
    array_of_errors.push(*result)
  end

  array_of_errors
end

#from_hash_of_multiple_typesObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rdstation/error/formatter.rb', line 48

def from_hash_of_multiple_types
  array_of_errors = []
  errors.each do |attribute_name, errors|
    if errors.is_a? Array
      result = build_error_from_array(attribute_name, errors)
    end
    if errors.is_a? Hash
      result = build_error_from_multilingual_hash(attribute_name, errors)
    end
    array_of_errors.push(*result)
  end

  array_of_errors
end

#from_single_hashObject



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rdstation/error/formatter.rb', line 31

def from_single_hash
  error_hash = @error_response.dup
  error_message = error_hash.delete('error')

  [
    {
      'error_type' => 'TOO_MANY_REQUESTS',
      'error_message' => error_message,
      'details' => error_hash
    }
  ]
end

#to_arrayObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rdstation/error/formatter.rb', line 12

def to_array
  return @error_response unless @error_response.is_a?(Hash)

  case error_format.format
  when RDStation::Error::Format::SINGLE_HASH
    return from_single_hash
  when RDStation::Error::Format::FLAT_HASH
    return from_flat_hash
  when RDStation::Error::Format::HASH_OF_ARRAYS
    return from_hash_of_arrays
  when RDStation::Error::Format::HASH_OF_HASHES
    return from_hash_of_hashes
  when RDStation::Error::Format::HASH_OF_MULTIPLE_TYPES
    return from_hash_of_multiple_types
  end

  errors
end