Sha256: 8259dc8b01289e611dc3c1bff922bcece105eb12a11dbbed5e2d42b68bd10ec3

Contents?: true

Size: 1.33 KB

Versions: 6

Compression:

Stored size: 1.33 KB

Contents

module Spree
  class ErrorSerializer
    attr_accessor :resource, :options

    def initialize(resource, options = {})
      @resource = resource
      @options = options
    end

    def as_json
      { errors: error_messages }.to_json
    end

    private

    def resource_error_objects(messages)
      messages.map do |title, detail|
        if title.is_a?(Hash) && title[:title]
          error_object title[:title], title[:detail]
        else
          pointer = "data/attributes/#{title.to_s.split('.').join('/')}"
          error_object title.to_s.gsub('.', ' '), detail.to_sentence, pointer: pointer
        end
      end
    end

    def error_messages
      case resource
      when Symbol
        title_translation = Spree.t("api.errors.#{resource}.title")
        detail_translation = Spree.t("api.errors.#{resource}.detail")
        [error_object(title_translation, detail_translation)]
      when Array
        resource.map do |errors|
          resource_error_objects errors
        end.flatten
      else
        resource_error_objects resource.errors.messages
      end
    end

    def error_object(title, detail, pointer: '')
      error = {
        detail: detail.gsub("\n", ' ').strip,
        meta: options,
        title: title.to_s.titleize
      }

      pointer.present? ? error.merge(pointer: pointer) : error
    end
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
spree_api_v2-0.2.2 app/serializers/spree/error_serializer.rb
solidus_api_v2-0.2.2 app/serializers/spree/error_serializer.rb
solidus_api_v2-0.2.1 app/serializers/spree/error_serializer.rb
spree_api_v2-0.2.1 app/serializers/spree/error_serializer.rb
spree_api_v2-0.2.0 app/serializers/spree/error_serializer.rb
solidus_api_v2-0.2.0 app/serializers/spree/error_serializer.rb