Ruby gem for rendering JSON API documents.
- Chat: gitter
- Twitter: @jsonapirb
- Docs: jsonapi-rb.org
# In Gemfile
gem 'jsonapi-renderer'
then
$ bundle
or manually via
$ gem install jsonapi-renderer
First, require the gem:
require 'jsonapi/renderer'
A resource here is any class that implements the following interface:
class ResourceInterface
# Returns the type of the resource.
# @return [String]
def jsonapi_type; end
# Returns the id of the resource.
# @return [String]
def jsonapi_id; end
# Returns a hash containing, for each included relationship, an array of the
# resources to be included from that one.
# @param [Array<Symbol>] included_relationships The keys of the relationships
# to be included.
# @return [Hash{Symbol => Array<#ResourceInterface>}]
def jsonapi_related(included_relationships); end
# Returns a JSON API-compliant representation of the resource as a hash.
# @param [Hash] options
# @option [Array<Symbol>, Nil] fields The requested fields, or nil.
# @option [Array<Symbol>] include The requested relationships to
# include (defaults to []).
# @return [Hash]
def as_jsonapi(options = {}); end
end
JSONAPI.render(data: resource,
include: include_string,
fields: fields_hash,
meta: meta_hash,
links: links_hash)
This returns a JSON API compliant hash representing the described document.
JSONAPI.render(data: resources,
include: include_string,
fields: fields_hash,
meta: meta_hash,
links: links_hash)
This returns a JSON API compliant hash representing the described document.
JSONAPI.render_errors(errors: errors,
meta: meta_hash,
links: links_hash)
where errors
is an array of objects implementing the as_jsonapi
method, that
returns a JSON API-compliant representation of the error.
This returns a JSON API compliant hash representing the described document.
jsonapi-renderer is released under the MIT License.