Class: SimpleAMS::Adapters::JSONAPI

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_ams/adapters/jsonapi.rb

Direct Known Subclasses

Collection

Defined Under Namespace

Classes: Collection

Constant Summary collapse

DEFAULT_OPTIONS =
{
  skip_id_in_attributes: false,
  key_transform: nil
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document, options = {}) ⇒ JSONAPI

Returns a new instance of JSONAPI.



10
11
12
13
# File 'lib/simple_ams/adapters/jsonapi.rb', line 10

def initialize(document, options = {})
  @document = document
  @options = DEFAULT_OPTIONS.merge(options)
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



9
10
11
# File 'lib/simple_ams/adapters/jsonapi.rb', line 9

def document
  @document
end

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/simple_ams/adapters/jsonapi.rb', line 9

def options
  @options
end

Instance Method Details

#as_jsonObject



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

def as_json
  hash = {
    data: data,
  }

  hash.merge!(links: links) unless links.empty?
  hash.merge!(metas: metas) unless metas.empty?
  hash.merge!(included: included) unless included.empty?

  return hash
end

#dataObject



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/simple_ams/adapters/jsonapi.rb', line 27

def data
  data = {
    transform_key(document.primary_id.name) => document.primary_id.value.to_s,
    type: document.type.value,
    attributes: fields,
  }

  data.merge!(relationships: relationships) unless relationships.empty?

  data
end

#embedded_relation_data_for(relation) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/simple_ams/adapters/jsonapi.rb', line 82

def embedded_relation_data_for(relation)
  return {} if relation.embedded.generics[:skip_data]&.value

  if relation.folder?
    value = relation.each.map{|doc|
      {
        doc.primary_id.name => doc.primary_id.value.to_s,
        type: doc.type.name
      }
    }
  else
    value = {
      relation.primary_id.name => relation.primary_id.value.to_s,
      type: relation.type.name
    }
  end
end


100
101
102
103
104
105
106
107
# File 'lib/simple_ams/adapters/jsonapi.rb', line 100

def embedded_relation_links_for(relation)
  return {} if relation.embedded.links.empty?

  relation.embedded.links.inject({}){ |hash, link|
    hash[link.name] = link.value
    hash
  }
end

#fieldsObject



39
40
41
42
43
44
45
46
# File 'lib/simple_ams/adapters/jsonapi.rb', line 39

def fields
  @fields ||= document.fields.inject({}){ |hash, field|
    unless options[:skip_id_in_attributes] && field_is_primary_id?(field)
      hash[transform_key(field.key)] = field.value
    end
    hash
  }
end

#formsObject



126
127
128
129
130
131
# File 'lib/simple_ams/adapters/jsonapi.rb', line 126

def forms
  @forms ||= document.forms.inject({}){ |hash, form|
    hash[form.name] = form.value
    hash
  }
end

#includedObject



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/simple_ams/adapters/jsonapi.rb', line 133

def included
  return @included ||= [] if document.relations.available.empty?

  @included ||= document.relations.available.inject([]){ |array, relation|
    next array if relation.embedded.generics[:skip_data]&.value

    if relation.folder?
      array << relation.map{|doc| self.class.new(doc, options).as_json[:data]}
    else
      array << self.class.new(relation, options).as_json[:data]
    end

    array
  }.flatten
end


110
111
112
113
114
115
116
117
# File 'lib/simple_ams/adapters/jsonapi.rb', line 110

def links
  return @links ||= {} if document.links.empty?

  @links ||= document.links.inject({}){ |hash, link|
    hash[link.name] = link.value
    hash
  }
end

#metasObject



119
120
121
122
123
124
# File 'lib/simple_ams/adapters/jsonapi.rb', line 119

def metas
  @metas ||= document.metas.inject({}){ |hash, meta|
    hash[meta.name] = meta.value
    hash
  }
end

#relationshipsObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/simple_ams/adapters/jsonapi.rb', line 61

def relationships
  return @relationships ||= {} if document.relations.empty?

  @relationships ||= document.relations.inject({}){ |hash, relation|
    _hash = {}

    embedded_relation_data = embedded_relation_data_for(relation)
    unless embedded_relation_data.empty?
      _hash.merge!(data: embedded_relation_data_for(relation))
    end

    embedded_relation_links = embedded_relation_links_for(relation)
    unless embedded_relation_links.empty?
      _hash.merge!(links: embedded_relation_links_for(relation))
    end

    hash.merge!(relation.name => _hash) unless _hash.empty?
    hash
  }
end

#transform_key(key) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/simple_ams/adapters/jsonapi.rb', line 48

def transform_key(key)
  case options[:key_transform]
  when :camel
    key.to_s.split('_').map(&capitalize).join
  when :kebab
    key.to_s.gsub('_','-')
  when :snake
    key
  else
    key
  end
end