Class: Jacoat::Document::ResourceIdentifier

Inherits:
Object
  • Object
show all
Defined in:
lib/jacoat/document/resource_identifier.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, type) ⇒ ResourceIdentifier

Returns a new instance of ResourceIdentifier.



17
18
19
20
# File 'lib/jacoat/document/resource_identifier.rb', line 17

def initialize(id, type)
  @id = id
  @type = type
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



4
5
6
# File 'lib/jacoat/document/resource_identifier.rb', line 4

def id
  @id
end

#relationshipsObject

Returns the value of attribute relationships.



4
5
6
# File 'lib/jacoat/document/resource_identifier.rb', line 4

def relationships
  @relationships
end

#typeObject

Returns the value of attribute type.



4
5
6
# File 'lib/jacoat/document/resource_identifier.rb', line 4

def type
  @type
end

Class Method Details

.from_jsonapi(body) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/jacoat/document/resource_identifier.rb', line 5

def self.from_jsonapi(body)
  if body.is_a?(Array)
    resources = []
    body.each do |item|
      resources << ResourceIdentifier.new(item[:id], item[:type]).process_jsonapi(item)
    end
  else
    resources = ResourceIdentifier.new(body[:id], body[:type]).process_jsonapi(body)
  end
  resources
end

Instance Method Details

#create_relationships(hash) ⇒ Object



27
28
29
30
31
32
# File 'lib/jacoat/document/resource_identifier.rb', line 27

def create_relationships(hash)
  @relationships = []
  hash.each_pair do |k, v|
    @relationships << Document::Relationship.new(k).process_jsonapi(v)
  end
end

#process_jsonapi(arguments) ⇒ Object



22
23
24
25
# File 'lib/jacoat/document/resource_identifier.rb', line 22

def process_jsonapi(arguments)
  create_relationships(arguments[:relationships]) if arguments.has_key?(:relationships)
  self
end

#relationship_hashObject



43
44
45
46
47
48
49
# File 'lib/jacoat/document/resource_identifier.rb', line 43

def relationship_hash
  hash = {}
  @relationships.each do |relationship|
    hash.merge!(relationship.to_hash)
  end
  hash
end

#to_hashObject



34
35
36
37
38
39
40
41
# File 'lib/jacoat/document/resource_identifier.rb', line 34

def to_hash
  hash = {
    type: @type,
    id: @id
  }
  hash.merge!(relationships: relationship_hash) if @relationships
  hash
end