Class: RDStation::Error::Formatter
- Inherits:
-
Object
- Object
- RDStation::Error::Formatter
- Defined in:
- lib/rdstation/error/formatter.rb
Instance Method Summary collapse
- #error_format ⇒ Object
- #errors ⇒ Object
- #from_flat_hash ⇒ Object
- #from_hash_of_arrays ⇒ Object
-
#initialize(error_response) ⇒ Formatter
constructor
A new instance of Formatter.
- #to_array ⇒ Object
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_format ⇒ Object
37 38 39 |
# File 'lib/rdstation/error/formatter.rb', line 37 def error_format @error_format ||= RDStation::Error::Format.new(errors) end |
#errors ⇒ Object
41 42 43 |
# File 'lib/rdstation/error/formatter.rb', line 41 def errors @errors ||= @error_response['errors'] end |
#from_flat_hash ⇒ Object
23 24 25 |
# File 'lib/rdstation/error/formatter.rb', line 23 def from_flat_hash [errors] end |
#from_hash_of_arrays ⇒ Object
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_array ⇒ Object
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 |