Class: Dato::JsonApiSerializer

Inherits:
Object
  • Object
show all
Defined in:
lib/dato/json_api_serializer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type: nil, link: nil) ⇒ JsonApiSerializer

Returns a new instance of JsonApiSerializer.



10
11
12
13
# File 'lib/dato/json_api_serializer.rb', line 10

def initialize(type: nil, link: nil)
  @link = link
  @type = type || type_from_schema
end

Instance Attribute Details

Returns the value of attribute link.



8
9
10
# File 'lib/dato/json_api_serializer.rb', line 8

def link
  @link
end

#typeObject (readonly)

Returns the value of attribute type.



8
9
10
# File 'lib/dato/json_api_serializer.rb', line 8

def type
  @type
end

Instance Method Details

#attributes(resource) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/dato/json_api_serializer.rb', line 95

def attributes(resource)
  if type == "item"
    return resource.keys.reject do |key|
      %i[
        item_type
        id
        created_at
        updated_at
        creator
      ].include?(key.to_sym)
    end
  end

  link_attributes["properties"].keys.map(&:to_sym)
end


129
130
131
# File 'lib/dato/json_api_serializer.rb', line 129

def link_attributes
  link.schema.properties["data"].properties["attributes"]
end


133
134
135
# File 'lib/dato/json_api_serializer.rb', line 133

def link_relationships
  link.schema.properties["data"].properties["relationships"]
end

#relationshipsObject



117
118
119
# File 'lib/dato/json_api_serializer.rb', line 117

def relationships
  @relationships ||= JsonSchemaRelationships.new(link.schema).relationships
end

#required_attributesObject



111
112
113
114
115
# File 'lib/dato/json_api_serializer.rb', line 111

def required_attributes
  return [] if type == "item"

  (link_attributes.required || []).map(&:to_sym)
end

#required_relationshipsObject



121
122
123
124
125
126
127
# File 'lib/dato/json_api_serializer.rb', line 121

def required_relationships
  if link.schema.properties["data"].required.include?("relationships")
    (link_relationships.required || []).map(&:to_sym)
  else
    []
  end
end

#serialize(resource, id = nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/dato/json_api_serializer.rb', line 15

def serialize(resource, id = nil)
  resource = resource.with_indifferent_access
  data = {}

  data[:id] = id || resource[:id] if id || resource[:id]

  resource.delete(:meta) if resource.key?(:meta)

  data[:type] = type

  if link.schema &&
     link.schema.properties["data"] &&
     link.schema.properties["data"].properties.keys.include?("attributes")

    serialized_resource_attributes = serialized_attributes(resource)
    data[:attributes] = serialized_resource_attributes
  end

  serialized_relationships = serialized_relationships(resource)

  data[:relationships] = serialized_relationships if serialized_relationships

  { data: data }
end

#serialized_attributes(resource) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/dato/json_api_serializer.rb', line 40

def serialized_attributes(resource)
  result = {}

  attributes(resource).each do |attribute|
    if resource.key? attribute
      result[attribute] = resource[attribute]
    elsif required_attributes.include? attribute
      throw "Required attribute: #{attribute}"
    end
  end

  result
end

#serialized_relationships(resource) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/dato/json_api_serializer.rb', line 54

def serialized_relationships(resource)
  result = {}

  relationships.each do |relationship, meta|
    if resource.key? relationship
      value = resource[relationship]

      data = if value
               if meta[:types].length > 1
                 if meta[:collection]
                   value.map(&:symbolize_keys)
                 else
                   value.symbolize_keys
                 end
               else
                 meta_type = meta[:types].first
                 if meta[:collection]
                   value.map do |id|
                     {
                       type: meta_type,
                       id: id.to_s,
                     }
                   end
                 else
                   {
                     type: meta_type,
                     id: value.to_s,
                   }
                 end
               end
             end

      result[relationship] = { data: data }
    elsif required_relationships.include?(relationship)
      throw "Required attribute: #{relationship}"
    end
  end

  result.empty? ? nil : result
end

#type_from_schemaObject



137
138
139
# File 'lib/dato/json_api_serializer.rb', line 137

def type_from_schema
  Dato::JsonSchemaType.new(link.schema).call
end