Class: JSONAPI::Ruby::Deserializer::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/jsonapi-ruby-deserializer/document.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document, link_data: true) ⇒ Document

Returns a new instance of Document.



9
10
11
12
13
14
15
16
17
# File 'lib/jsonapi-ruby-deserializer/document.rb', line 9

def initialize(document, link_data: true)
  @included = parse_resource!(document['included']) if document['included']
  @index = create_index!
  @data = parse_resource!(document['data'])
  @links = parse_links!(document['links'])
  @meta = parse_meta!(document['meta'])
  @errors = parse_errors!(document['errors'])
  link_data! if link_data
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



7
8
9
# File 'lib/jsonapi-ruby-deserializer/document.rb', line 7

def data
  @data
end

#errorsObject

Returns the value of attribute errors.



7
8
9
# File 'lib/jsonapi-ruby-deserializer/document.rb', line 7

def errors
  @errors
end

#includedObject

Returns the value of attribute included.



7
8
9
# File 'lib/jsonapi-ruby-deserializer/document.rb', line 7

def included
  @included
end

#indexObject

Returns the value of attribute index.



7
8
9
# File 'lib/jsonapi-ruby-deserializer/document.rb', line 7

def index
  @index
end

Returns the value of attribute links.



7
8
9
# File 'lib/jsonapi-ruby-deserializer/document.rb', line 7

def links
  @links
end

#metaObject

Returns the value of attribute meta.



7
8
9
# File 'lib/jsonapi-ruby-deserializer/document.rb', line 7

def meta
  @meta
end

Instance Method Details

#create_index!Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/jsonapi-ruby-deserializer/document.rb', line 37

def create_index!
  return if @included.nil?

  {}.tap do |h|
    @included.each do |resource|
      resource_identifier = [resource.type, resource.id]
      h[resource_identifier] = resource
    end
  end
end


54
55
56
57
58
59
60
61
62
63
64
# File 'lib/jsonapi-ruby-deserializer/document.rb', line 54

def link_data!
  return if @included.nil?

  (Array(@data) + @included).each do |resource|
    next if resource.relationships.nil?

    resource.relationships.each do |relation|
      resource.send(relation.to_sym).data = merge_relations!(resource.send(relation.to_sym).data)
    end
  end
end

#merge_relations!(data) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/jsonapi-ruby-deserializer/document.rb', line 66

def merge_relations!(data)
  if data.kind_of?(Array)
    data.map { |element| index[[element.type, element.id]] }
  else
    index[[data.type, data.id]]
  end
end

#parse_errors!(data) ⇒ Object



48
49
50
51
52
# File 'lib/jsonapi-ruby-deserializer/document.rb', line 48

def parse_errors!(data)
  return if data.nil?

  data.kind_of?(Array) ? data.map! { |h| Errors.new(h) } : Errors.new(data)
end

#parse_links!(data) ⇒ Object



25
26
27
28
29
# File 'lib/jsonapi-ruby-deserializer/document.rb', line 25

def parse_links!(data)
  return if data.nil?

  Links.new(data)
end

#parse_meta!(data) ⇒ Object



31
32
33
34
35
# File 'lib/jsonapi-ruby-deserializer/document.rb', line 31

def parse_meta!(data)
  return if data.nil?

  Meta.new(data)
end

#parse_resource!(data) ⇒ Object



19
20
21
22
23
# File 'lib/jsonapi-ruby-deserializer/document.rb', line 19

def parse_resource!(data)
  return if data.nil?

  data.kind_of?(Array) ? data.map! { |h| Resource.new(h) } : Resource.new(data)
end