Sha256: 2e172c8906b9aa1a557850e6629541b67bc906a9fb01679d3bdbce3d6cb5af7a

Contents?: true

Size: 705 Bytes

Versions: 3

Compression:

Stored size: 705 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].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

3 entries across 3 versions & 1 rubygems

Version Path
jsonapi.rb-2.0.1 lib/jsonapi/error_serializer.rb
jsonapi.rb-2.0.0 lib/jsonapi/error_serializer.rb
jsonapi.rb-1.7.0 lib/jsonapi/error_serializer.rb