项目作者: jsonapi-rb

项目描述 :
Efficiently render JSON API documents.
高级语言: Ruby
项目地址: git://github.com/jsonapi-rb/jsonapi-renderer.git
创建时间: 2016-10-26T01:47:48Z
项目社区:https://github.com/jsonapi-rb/jsonapi-renderer

开源协议:MIT License

下载


jsonapi-renderer

Ruby gem for rendering JSON API documents.

Status

Gem Version
Build Status
codecov
Gitter chat

Resources

Installation

  1. # In Gemfile
  2. gem 'jsonapi-renderer'

then

  1. $ bundle

or manually via

  1. $ gem install jsonapi-renderer

Usage

First, require the gem:

  1. require 'jsonapi/renderer'

Rendering resources

A resource here is any class that implements the following interface:

  1. class ResourceInterface
  2. # Returns the type of the resource.
  3. # @return [String]
  4. def jsonapi_type; end
  5. # Returns the id of the resource.
  6. # @return [String]
  7. def jsonapi_id; end
  8. # Returns a hash containing, for each included relationship, an array of the
  9. # resources to be included from that one.
  10. # @param included_relationships [Array<Symbol>] The keys of the relationships
  11. # to be included.
  12. # @return [Hash{Symbol => Array<#ResourceInterface>}]
  13. def jsonapi_related(included_relationships); end
  14. # Returns a JSON API-compliant representation of the resource as a hash.
  15. # @param options [Hash]
  16. # @option fields [Set<Symbol>, Nil] The requested fields, or nil.
  17. # @option include [Set<Symbol>] The requested relationships to
  18. # include (defaults to []).
  19. # @return [Hash]
  20. def as_jsonapi(options = {}); end
  21. end

Rendering a single resource

  1. JSONAPI.render(data: resource,
  2. include: include_string,
  3. fields: fields_hash,
  4. meta: meta_hash,
  5. links: links_hash)

This returns a JSON API compliant hash representing the described document.

Rendering a collection of resources

  1. JSONAPI.render(data: resources,
  2. include: include_string,
  3. fields: fields_hash,
  4. meta: meta_hash,
  5. links: links_hash)

This returns a JSON API compliant hash representing the described document.

Rendering a relationship

  1. JSONAPI.render(data: resource,
  2. relationship: :posts,
  3. include: include_string,
  4. fields: fields_hash,
  5. meta: meta_hash,
  6. links: links_hash)

This returns a JSON API compliant hash representing the described document.

Rendering errors

  1. JSONAPI.render_errors(errors: errors,
  2. meta: meta_hash,
  3. 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.

Caching

The generated JSON fragments can be cached in any cache implementation
supporting the fetch_multi method.

When using caching, the serializable resources must implement an
additional jsonapi_cache_key method:

  1. # Returns a cache key for the resource, parameterized by the `include` and
  2. # `fields` options.
  3. # @param options [Hash]
  4. # @option fields [Set<Symbol>, Nil] The requested fields, or nil.
  5. # @option include [Set<Symbol>] The requested relationships to
  6. # include (defaults to []).
  7. # @return [String]
  8. def jsonapi_cache_key(options = {}); end

The cache instance must be passed to the renderer as follows:

  1. JSONAPI.render(data: resources,
  2. include: include_string,
  3. fields: fields_hash,
  4. cache: cache_instance)

License

jsonapi-renderer is released under the MIT License.