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
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of JSONAPI.



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

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



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

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?

  hash
end

#dataObject



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

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



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/simple_ams/adapters/jsonapi.rb', line 73

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

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


91
92
93
94
95
96
97
# File 'lib/simple_ams/adapters/jsonapi.rb', line 91

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

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

#fieldsObject



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

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

#formsObject



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

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

#includedObject



119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/simple_ams/adapters/jsonapi.rb', line 119

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

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

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


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

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

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

#metasObject



107
108
109
110
111
# File 'lib/simple_ams/adapters/jsonapi.rb', line 107

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

#relationshipsObject



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/simple_ams/adapters/jsonapi.rb', line 59

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

  @relationships ||= document.relations.each_with_object({}) do |relation, hash|
    embedded_relation_data = embedded_relation_data_for(relation)
    hash.merge!(data: embedded_relation_data_for(relation)) unless embedded_relation_data.empty?

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

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

#transform_key(key) ⇒ Object



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

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