Sha256: c1ad4c07f8f3459319e373de6f9dc92535ebba9a3a3ca339cf6f9a352c6dc686

Contents?: true

Size: 1.08 KB

Versions: 6

Compression:

Stored size: 1.08 KB

Contents

require_relative './format'

module RDStation
  class Error
    class Formatter
      def initialize(error_response)
        @error_response = error_response
      end

      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

      def from_flat_hash
        [errors]
      end

      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

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

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

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rdstation-ruby-client-2.2.0 lib/rdstation/error/formatter.rb
rdstation-ruby-client-2.1.0 lib/rdstation/error/formatter.rb
rdstation-ruby-client-2.0.0 lib/rdstation/error/formatter.rb
rdstation-ruby-client-1.2.1 lib/rdstation/error/formatter.rb
rdstation-ruby-client-1.2.0 lib/rdstation/error/formatter.rb
rdstation-ruby-client-1.1.0 lib/rdstation/error/formatter.rb