Class: Agave::JsonApiSerializer
- Inherits:
-
Object
- Object
- Agave::JsonApiSerializer
- Defined in:
- lib/agave/json_api_serializer.rb
Instance Attribute Summary collapse
-
#link ⇒ Object
readonly
Returns the value of attribute link.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #attributes(resource) ⇒ Object
-
#initialize(type, link) ⇒ JsonApiSerializer
constructor
A new instance of JsonApiSerializer.
- #link_attributes ⇒ Object
- #link_relationships ⇒ Object
- #relationships ⇒ Object
- #required_attributes ⇒ Object
- #required_relationships ⇒ Object
- #serialize(resource, id = nil) ⇒ Object
- #serialized_attributes(resource) ⇒ Object
- #serialized_relationships(resource) ⇒ Object
Constructor Details
#initialize(type, link) ⇒ JsonApiSerializer
Returns a new instance of JsonApiSerializer.
6 7 8 9 |
# File 'lib/agave/json_api_serializer.rb', line 6 def initialize(type, link) @link = link @type = type end |
Instance Attribute Details
#link ⇒ Object (readonly)
Returns the value of attribute link.
4 5 6 |
# File 'lib/agave/json_api_serializer.rb', line 4 def link @link end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
4 5 6 |
# File 'lib/agave/json_api_serializer.rb', line 4 def type @type end |
Instance Method Details
#attributes(resource) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/agave/json_api_serializer.rb', line 67 def attributes(resource) if type == "item" return resource.keys.map(&:to_sym) - [ :item_type, :id, :created_at, :updated_at, :is_valid, :published_version, :current_version ] end link_attributes["properties"].keys.map(&:to_sym) end |
#link_attributes ⇒ Object
137 138 139 |
# File 'lib/agave/json_api_serializer.rb', line 137 def link_attributes link.schema.properties["data"].properties["attributes"] end |
#link_relationships ⇒ Object
141 142 143 |
# File 'lib/agave/json_api_serializer.rb', line 141 def link_relationships link.schema.properties["data"].properties["relationships"] end |
#relationships ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/agave/json_api_serializer.rb', line 91 def relationships if type == "item" if link.rel == :create return { item_type: { collection: false, type: 'item_type' } } else {} end end if !link_relationships return {} end link_relationships.properties.reduce({}) do |acc, (relationship, schema)| is_collection = schema.properties["data"].type.first == 'array' definition = if is_collection schema.properties['data'].items elsif schema.properties['data'].type.first == 'object' schema.properties['data'] else schema.properties['data'].any_of.find do |option| option.type.first == 'object' end end type = definition.properties['type'] .pattern.source.gsub(/(^\^|\$$)/, '') acc[relationship.to_sym] = { collection: is_collection, type: type, } acc end end |
#required_attributes ⇒ Object
83 84 85 86 87 88 89 |
# File 'lib/agave/json_api_serializer.rb', line 83 def required_attributes if type == "item" return [] end (link_attributes.required || []).map(&:to_sym) end |
#required_relationships ⇒ Object
129 130 131 132 133 134 135 |
# File 'lib/agave/json_api_serializer.rb', line 129 def required_relationships if type == "item" return %i(item_type) end (link_relationships.required || []).map(&:to_sym) end |
#serialize(resource, id = nil) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/agave/json_api_serializer.rb', line 11 def serialize(resource, id = nil) resource = resource.with_indifferent_access data = {} data[:id] = id || resource[:id] if id || resource[:id] data[:type] = type data[:attributes] = serialized_attributes(resource) if relationships.any? data[:relationships] = serialized_relationships(resource) end { data: data } end |
#serialized_attributes(resource) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/agave/json_api_serializer.rb', line 27 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
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/agave/json_api_serializer.rb', line 41 def serialized_relationships(resource) result = {} relationships.each do |relationship, | if resource.key? relationship value = resource[relationship] data = if value if [:collection] value.map do |id| { type: [:type], id: id.to_s } end else { type: [:type], id: value.to_s } end end result[relationship] = { data: data } elsif required_relationships.include?(relationship) throw "Required attribute: #{relationship}" end end result end |