Sha256: 3b2481cc3924234750dbf3c646cfde6c8c6b6b825e2c5146dedf0a3ee8ff500c

Contents?: true

Size: 712 Bytes

Versions: 1

Compression:

Stored size: 712 Bytes

Contents

require 'jsonapi/serializer'

module JSONAPI
  # A simple error serializer
  class ErrorSerializer
    include JSONAPI::Serializer

    set_type :error

    # Object/Hash attribute helpers.
    [:status, :source, :title, :detail, :code].each do |attr_name|
      attribute attr_name do |object|
        object.try(attr_name) || object.try(:fetch, attr_name, nil)
      end
    end

    # Overwrite the ID extraction method, to skip validations
    #
    # @return [NilClass]
    def self.id_from_record(_record, _params)
    end

    # Remap the root key to `errors`
    #
    # @return [Hash]
    def serializable_hash
      { errors: (super[:data] || []).map { |error| error[:attributes] } }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jsonapi.rb-2.1.1 lib/jsonapi/error_serializer.rb