|
| 1 | +# jsonapi-renderer |
| 2 | +Ruby gem for rendering [JSON API](http://jsonapi.org) documents. |
| 3 | + |
| 4 | +## Status |
| 5 | + |
| 6 | +[](https://badge.fury.io/rb/jsonapi-renderer) |
| 7 | +[](http://travis-ci.org/jsonapi-rb/renderer?branch=master) |
| 8 | + |
| 9 | +## Installation |
| 10 | +```ruby |
| 11 | +# In Gemfile |
| 12 | +gem 'jsonapi-renderer' |
| 13 | +``` |
| 14 | +then |
| 15 | +``` |
| 16 | +$ bundle |
| 17 | +``` |
| 18 | +or manually via |
| 19 | +``` |
| 20 | +$ gem install jsonapi-renderer |
| 21 | +``` |
| 22 | + |
| 23 | +## Usage |
| 24 | + |
| 25 | +First, require the gem: |
| 26 | +```ruby |
| 27 | +require 'jsonapi/renderer' |
| 28 | +``` |
| 29 | + |
| 30 | +### Rendering resources |
| 31 | + |
| 32 | +A resource here is any class that implements the following interface: |
| 33 | +```ruby |
| 34 | +class ResourceInterface |
| 35 | + # Returns the type of the resource. |
| 36 | + # @return [String] |
| 37 | + def jsonapi_type; end |
| 38 | + |
| 39 | + # Returns the id of the resource. |
| 40 | + # @return [String] |
| 41 | + def jsonapi_id; end |
| 42 | + |
| 43 | + # Returns a hash containing, for each included relationship, the resource(s) |
| 44 | + # to be included from that one. |
| 45 | + # @param [Array<Symbol>] included_relationships The keys of the relationships |
| 46 | + # to be included. |
| 47 | + # @return [Hash{Symbol => #ResourceInterface, Array<#ResourceInterface>}] |
| 48 | + def jsonapi_related(included_relationships); end |
| 49 | + |
| 50 | + # Returns a JSON API-compliant representation of the resource as a hash. |
| 51 | + # @param [Hash] options |
| 52 | + # @option [Array<Symbol>, Nil] fields The requested fields, or nil. |
| 53 | + # @option [Array<Symbol>, Nil] included The requested included |
| 54 | + # relationships, or nil. |
| 55 | + # @return [Hash] |
| 56 | + def as_jsonapi(options = {}); end |
| 57 | +``` |
| 58 | +
|
| 59 | +#### Rendering a single resource |
| 60 | +```ruby |
| 61 | +JSONAPI.render(resource, |
| 62 | + include: include_string, |
| 63 | + fields: fields_hash, |
| 64 | + meta: meta_hash, |
| 65 | + links: links_hash) |
| 66 | +``` |
| 67 | +
|
| 68 | +This returns a JSON API compliant hash representing the described document. |
| 69 | +
|
| 70 | +#### Rendering a collection of resources |
| 71 | +```ruby |
| 72 | +JSONAPI.render(resources, |
| 73 | + include: include_string, |
| 74 | + fields: fields_hash, |
| 75 | + meta: meta_hash, |
| 76 | + links: links_hash) |
| 77 | +``` |
| 78 | + |
| 79 | +This returns a JSON API compliant hash representing the described document. |
| 80 | + |
| 81 | +### Rendering errors |
| 82 | + |
| 83 | +```ruby |
| 84 | +JSONAPI.render_errors(errors, |
| 85 | + meta: meta_hash, |
| 86 | + links: links_hash) |
| 87 | +``` |
| 88 | + |
| 89 | +where `errors` is an array of objects implementing the `as_jsonapi` method, that |
| 90 | +returns a JSON API-compliant representation of the error. |
| 91 | + |
| 92 | +This returns a JSON API compliant hash representing the described document. |
| 93 | + |
| 94 | +## License |
| 95 | + |
| 96 | +jsonapi-renderer is released under the [MIT License](http://www.opensource.org/licenses/MIT). |
0 commit comments