Module: Roar::JSON::JSONAPI::Document

Defined in:
lib/roar/json/json_api/document.rb

Overview

Instance method API for JSON API Documents.

Instance Method Summary collapse

Instance Method Details

#to_hash(options = {}) ⇒ Hash{String=>Object}

Render the document as a Ruby Hash

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • include (Array<#to_s>, #to_s, false)

    compound documents to include, specified as a list of relationship paths (Array or comma-separated String) or false, if no compound documents are to be included.

    N.B. this syntax and behaviour for this option is signficantly different to that of the include option implemented in other, non-JSON API Representers.

  • fields (Hash{Symbol=>[Array<String>]})

    fields to returned on a per-type basis.

  • meta (Hash{#to_s}=>Object)

    additional meta information to be rendered in the document.

  • user_options (Hash{Symbol=>Symbol})

    additional arbitary options to be passed to the Representer.

Returns:

  • (Hash{String=>Object})


53
54
55
# File 'lib/roar/json/json_api/document.rb', line 53

def to_hash(options = {})
  super
end

#to_json(options = {}) ⇒ String

Render the document as JSON

Examples:

Simple rendering

representer.to_json

Rendering with compound documents and sparse fieldsets

uri   = Addressable::URI.parse('/articles/1?include=author,comments.author')
query = Rack::Utils.parse_nested_query(uri.query)
# => {"include"=>"author", "fields"=>{"articles"=>"title,body", "people"=>"name"}}

representer.to_json(
  include: query['include'],
  fields:  query['fields']
)

Options Hash (options):

  • include (Array<#to_s>, #to_s, false)

    compound documents to include, specified as a list of relationship paths (Array or comma-separated String) or false, if no compound documents are to be included.

    N.B. this syntax and behaviour for this option is signficantly different to that of the include option implemented in other, non-JSON API Representers.

  • fields (Hash{Symbol=>[Array<String>]})

    fields to returned on a per-type basis.

  • meta (Hash{#to_s}=>Object)

    additional meta information to be rendered in the document.

  • user_options (Hash{Symbol=>Symbol})

    additional arbitary options to be passed to the Representer.

Returns:

  • (String)

    JSON String

See Also:



29
30
31
# File 'lib/roar/json/json_api/document.rb', line 29

def to_json(options = {})
  super
end