Git Product home page Git Product logo

jsonapi-renderer's Introduction

jsonapi-renderer

Ruby gem for rendering JSON API documents.

Status

Gem Version Build Status codecov Gitter chat

Resources

Installation

# In Gemfile
gem 'jsonapi-renderer'

then

$ bundle

or manually via

$ gem install jsonapi-renderer

Usage

First, require the gem:

require 'jsonapi/renderer'

Rendering resources

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

Rendering a single resource

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.

Rendering a collection of resources

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.

Rendering errors

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.

License

jsonapi-renderer is released under the MIT License.

jsonapi-renderer's People

Contributors

beauby avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.