class JSONAPI::Document

Contains all objects relating to a JSONAPI Document

Attributes

data[R]
errors[R]
included[R]
jsonapi[R]
meta[R]

Public Class Methods

new(document = {}) click to toggle source

@param document [Hash] A hash of the different possible document members

with the values being clases associated with those members
@data is either a JSONAPI::Document::Resource or a Array<JSONAPI::Document::Resource>
              or a JSONAPI::Document::ResourceId or a Array<JSONAPI::Document::ResourceId>
@meta is JSONAPI::Document::Meta
@links is JSONAPI::Document::Links
@included is an Array<JSONAPI::Document::Resource>
@errors is an Array<JSONAPI::Document::Error>
@jsonapi is JSONAPI::Document::Jsonapi

@raise RuntimeError A document must be initialized with a hash of its members.

# File lib/easy/jsonapi/document.rb, line 31
def initialize(document = {})
  raise 'JSONAPI::Document parameter must be a Hash' unless document.is_a? Hash
  @data = document[:data]
  @meta = document[:meta]
  @links = document[:links] # software generated?
  @included = document[:included]
  @errors = document[:errors]
  @jsonapi = document[:jsonapi] # online documentation
end

Public Instance Methods

to_h() click to toggle source

Represent as a hash mimicing the JSONAPI format

# File lib/easy/jsonapi/document.rb, line 54
def to_h
  to_return = {}
  JSONAPI::Utility.to_h_member(to_return, @data, :data)
  JSONAPI::Utility.to_h_member(to_return, @meta, :meta)
  JSONAPI::Utility.to_h_member(to_return, @links, :links)
  JSONAPI::Utility.to_h_member(to_return, @included, :included)
  JSONAPI::Utility.to_h_member(to_return, @errors, :errors)
  JSONAPI::Utility.to_h_member(to_return, @jsonapi, :jsonapi)
  to_return
end
to_s() click to toggle source

Represent as a string mimicing the JSONAPI format

# File lib/easy/jsonapi/document.rb, line 42
def to_s
  '{ ' \
    "#{JSONAPI::Utility.member_to_s('data', @data, first_member: true)}" \
    "#{JSONAPI::Utility.member_to_s('meta', @meta)}" \
    "#{JSONAPI::Utility.member_to_s('links', @links)}" \
    "#{JSONAPI::Utility.member_to_s('included', @included)}" \
    "#{JSONAPI::Utility.member_to_s('errors', @errors)}" \
    "#{JSONAPI::Utility.member_to_s('jsonapi', @jsonapi)}" \
  ' }'
end
validate() click to toggle source

Check if the document is JSONAPI compliant @raise If the document's to_h does not comply

# File lib/easy/jsonapi/document.rb, line 67
def validate
  JSONAPI::Exceptions::DocumentExceptions.check_compliance(to_h)
end