Class: JSONAPI::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/easy/jsonapi/document.rb,
lib/easy/jsonapi/document/meta.rb,
lib/easy/jsonapi/document/error.rb,
lib/easy/jsonapi/document/links.rb,
lib/easy/jsonapi/document/jsonapi.rb,
lib/easy/jsonapi/document/resource.rb,
lib/easy/jsonapi/document/links/link.rb,
lib/easy/jsonapi/document/resource_id.rb,
lib/easy/jsonapi/document/meta/meta_member.rb,
lib/easy/jsonapi/document/error/error_member.rb,
lib/easy/jsonapi/document/resource/attributes.rb,
lib/easy/jsonapi/document/jsonapi/jsonapi_member.rb,
lib/easy/jsonapi/document/resource/relationships.rb,
lib/easy/jsonapi/document/resource/attributes/attribute.rb,
lib/easy/jsonapi/document/resource/relationships/relationship.rb

Overview

Contains all objects relating to a JSONAPI Document

Defined Under Namespace

Classes: Error, Jsonapi, Links, Meta, Resource, ResourceId

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document = {}) ⇒ Document

Returns a new instance of Document.

Parameters:

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

    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

Raises:

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



31
32
33
34
35
36
37
38
39
# 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

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



19
20
21
# File 'lib/easy/jsonapi/document.rb', line 19

def data
  @data
end

#errorsObject (readonly)

Returns the value of attribute errors.



19
20
21
# File 'lib/easy/jsonapi/document.rb', line 19

def errors
  @errors
end

#includedObject (readonly)

Returns the value of attribute included.



19
20
21
# File 'lib/easy/jsonapi/document.rb', line 19

def included
  @included
end

#jsonapiObject (readonly)

Returns the value of attribute jsonapi.



19
20
21
# File 'lib/easy/jsonapi/document.rb', line 19

def jsonapi
  @jsonapi
end

Returns the value of attribute links.



19
20
21
# File 'lib/easy/jsonapi/document.rb', line 19

def links
  @links
end

#metaObject (readonly)

Returns the value of attribute meta.



19
20
21
# File 'lib/easy/jsonapi/document.rb', line 19

def meta
  @meta
end

Instance Method Details

#to_hObject

Represent as a hash mimicing the JSONAPI format



54
55
56
57
58
59
60
61
62
63
# 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_sObject

Represent as a string mimicing the JSONAPI format



42
43
44
45
46
47
48
49
50
51
# 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

#validateObject

Check if the document is JSONAPI compliant

Raises:

  • If the document’s to_h does not comply



67
68
69
# File 'lib/easy/jsonapi/document.rb', line 67

def validate
  JSONAPI::Exceptions::DocumentExceptions.check_compliance(to_h)
end