Class: JSONAPI::Document::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/easy/jsonapi/document/resource.rb,
lib/easy/jsonapi/document/resource/attributes.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

A jsonapi resource object

Defined Under Namespace

Classes: Attributes, Relationships

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(members_hash) ⇒ Resource

Returns a new instance of Resource.

Parameters:

  • members_hash (Hash)

    The members to initialize a resource with



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/easy/jsonapi/document/resource.rb', line 15

def initialize(members_hash)
  unless members_hash.is_a? Hash
    raise 'A JSONAPI::Document::Resource must be initialized with a Hash'
  end
  @type = members_hash[:type].to_s unless members_hash[:type].nil?
  @id = members_hash[:id].to_s unless members_hash[:id].nil?
  @attributes = members_hash[:attributes]
  @relationships = members_hash[:relationships]
  @links = members_hash[:links]
  @meta = members_hash[:meta]
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



12
13
14
# File 'lib/easy/jsonapi/document/resource.rb', line 12

def attributes
  @attributes
end

#idObject

Returns the value of attribute id.



12
13
14
# File 'lib/easy/jsonapi/document/resource.rb', line 12

def id
  @id
end

Returns the value of attribute links.



12
13
14
# File 'lib/easy/jsonapi/document/resource.rb', line 12

def links
  @links
end

#metaObject

Returns the value of attribute meta.



12
13
14
# File 'lib/easy/jsonapi/document/resource.rb', line 12

def meta
  @meta
end

#relationshipsObject

Returns the value of attribute relationships.



12
13
14
# File 'lib/easy/jsonapi/document/resource.rb', line 12

def relationships
  @relationships
end

#typeObject

Returns the value of attribute type.



12
13
14
# File 'lib/easy/jsonapi/document/resource.rb', line 12

def type
  @type
end

Instance Method Details

#to_hHash

Hash representation of a jsonapi resource

Returns:

  • (Hash)

    The jsonapi representation of the resource



44
45
46
47
48
49
50
51
52
53
# File 'lib/easy/jsonapi/document/resource.rb', line 44

def to_h
  to_return = {}
  JSONAPI::Utility.to_h_member(to_return, @type, :type)
  JSONAPI::Utility.to_h_member(to_return, @id, :id)
  JSONAPI::Utility.to_h_member(to_return, @attributes, :attributes)
  JSONAPI::Utility.to_h_member(to_return, @relationships, :relationships)
  JSONAPI::Utility.to_h_member(to_return, @links, :links)
  JSONAPI::Utility.to_h_member(to_return, @meta, :meta)
  to_return
end

#to_sString

String representation of Document that is JSON parsable

If any document memeber is nil, it does not include it
in the returned string.

Returns:

  • (String)

    The string representation of a JSONAPI Document



31
32
33
34
35
36
37
38
39
40
# File 'lib/easy/jsonapi/document/resource.rb', line 31

def to_s
  '{ ' \
    "#{JSONAPI::Utility.member_to_s('type', @type, first_member: true)}" \
    "#{JSONAPI::Utility.member_to_s('id', @id)}" \
    "#{JSONAPI::Utility.member_to_s('attributes', @attributes)}" \
    "#{JSONAPI::Utility.member_to_s('relationships', @relationships)}" \
    "#{JSONAPI::Utility.member_to_s('links', @links)}" \
    "#{JSONAPI::Utility.member_to_s('meta', @meta)}" \
  ' }'
end