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.



6
7
8
# File 'lib/rdstation/error/formatter.rb', line 6

def initialize(error_response)
  @error_response = error_response
end

Instance Method Details

#error_formatObject



37
38
39
# File 'lib/rdstation/error/formatter.rb', line 37

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

#errorsObject



41
42
43
# File 'lib/rdstation/error/formatter.rb', line 41

def errors
  @errors ||= @error_response['errors']
end

#from_flat_hashObject



23
24
25
# File 'lib/rdstation/error/formatter.rb', line 23

def from_flat_hash
  [errors]
end

#from_hash_of_arraysObject



27
28
29
30
31
32
33
34
35
# File 'lib/rdstation/error/formatter.rb', line 27

def from_hash_of_arrays
  errors.each_with_object([]) do |errors, array_of_errors|
    attribute_name = errors.first
    attribute_errors = errors.last
    path = { 'path' => "body.#{attribute_name}" }
    errors = attribute_errors.map { |error| error.merge(path) }
    array_of_errors.push(*errors)
  end
end

#to_arrayObject



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/rdstation/error/formatter.rb', line 10

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

  case error_format.format
  when RDStation::Error::Format::FLAT_HASH
    return from_flat_hash
  when RDStation::Error::Format::HASH_OF_ARRAYS
    return from_hash_of_arrays
  end

  errors
end