Module: JSONAPI::Utils::Response::Formatters

Included in:
JSONAPI::Utils::Response
Defined in:
lib/jsonapi/utils/response/formatters.rb

Instance Method Summary collapse

Instance Method Details

#jsonapi_format(object, options = {}) ⇒ Hash Also known as: jsonapi_serialize

Note:

The return of this method represents what will actually be displayed in the response body.

Note:

It can also be called as #jsonapi_serialize due to backward compatibility issues.

Helper method to format ActiveRecord or Hash objects into JSON API-compliant ones.

Parameters:

  • object (ActiveRecord::Base, ActiveRecord::Relation, Hash, Array<Hash>)

    Object to be formatted into JSON e.g.: User.first, User.all, { data: { id: 1, first_name: ‘Tiago’ } },

    { data: { id: 1, first_name: ‘Tiago’ } }
  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • resource: (JSONAPI::Resource)

    it tells the formatter which resource class to be used rather than use an infered one (default behaviour)

  • source: (JSONAPI::Resource)

    it tells the formatter that this response is from a related resource and the result should be interpreted as a related resources response

  • relationship_type: (String, Symbol)

    it tells that the formatter which relationship the data is from

  • model: (ActiveRecord::Base)

    ActiveRecord model class to be instantiated when a Hash or Array of Hashes is passed as the “object” argument

  • count: (Integer)

    if it’s rendering a collection of resources, the default gem’s counting method can be bypassed by the use of this options. It’s shows then the total records resulting from that request and also calculates the pagination.

Returns:

  • (Hash)


33
34
35
36
37
38
39
40
# File 'lib/jsonapi/utils/response/formatters.rb', line 33

def jsonapi_format(object, options = {})
  if object.is_a?(Hash)
    hash = object.with_indifferent_access
    object = hash_to_active_record(hash[:data], options[:model])
  end
  fix_custom_request_options(object)
  build_response_document(object, options).contents
end

#jsonapi_format_errors(object) ⇒ Array Also known as: jsonapi_serialize_errors

Note:

The return of this method represents what will actually be displayed in the response body.

Note:

It can also be called as #jsonapi_serialize_errors due to backward compatibility issues.

Helper method to format ActiveRecord or any object that responds to #errors into JSON API-compliant error response bodies.

Parameters:

  • object (ActiveRecord::Base or any object that responds to #errors)

    Error object to be serialized into JSON e.g.: User.new(name: nil).tap(&:save), MyErrorDecorator.new(invalid_object)

Returns:

  • (Array)


57
58
59
60
61
62
63
# File 'lib/jsonapi/utils/response/formatters.rb', line 57

def jsonapi_format_errors(object)
  if active_record_obj?(object)
    object = JSONAPI::Utils::Exceptions::ActiveRecord.new(object, @request.resource_klass, context)
  end
  errors = object.respond_to?(:errors) ? object.errors : object
  JSONAPI::Utils::Support::Error.sanitize(errors).uniq
end