Module: ActiveModelSerializers::Adapter::JsonApi::Deserialization
- Defined in:
- lib/active_model_serializers/adapter/json_api/deserialization.rb
Overview
NOTE(Experimental): This is an experimental feature. Both the interface and internals could be subject to changes.
Constant Summary collapse
- InvalidDocument =
Class.new(ArgumentError)
Class Method Summary collapse
- .field_key(field, options) ⇒ Object private
- .filter_fields(fields, options) ⇒ Object private
-
.parse(document, options = {}) ⇒ Object
Same as parse!, but returns an empty hash instead of raising InvalidDocument on invalid payloads.
-
.parse!(document, options = {}) ⇒ Hash
Transform a JSON API document, containing a single data object, into a hash that is ready for ActiveRecord::Base.new() and such.
- .parse_attributes(attributes, options) ⇒ Object private
-
.parse_relationship(assoc_name, assoc_data, options) ⇒ Hash{Symbol, Object}
private
Given an association name, and a relationship data attribute, build a hash mapping the corresponding ActiveRecord attribute to the corresponding value.
- .parse_relationships(relationships, options) ⇒ Object private
- .transform_keys(hash, options) ⇒ Object private
-
.validate_payload(payload) ⇒ Object
private
Checks whether a payload is compliant with the JSON API spec.
Class Method Details
.field_key(field, options) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
154 155 156 |
# File 'lib/active_model_serializers/adapter/json_api/deserialization.rb', line 154 def field_key(field, ) ([:keys] || {}).fetch(field.to_sym, field).to_sym end |
.filter_fields(fields, options) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
145 146 147 148 149 150 151 |
# File 'lib/active_model_serializers/adapter/json_api/deserialization.rb', line 145 def filter_fields(fields, ) if (only = [:only]) fields.slice!(*Array(only).map(&:to_s)) elsif (except = [:except]) fields.except!(*Array(except).map(&:to_s)) end end |
.parse(document, options = {}) ⇒ Object
Same as parse!, but returns an empty hash instead of raising InvalidDocument on invalid payloads.
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/active_model_serializers/adapter/json_api/deserialization.rb', line 85 def parse(document, = {}) document = document.dup.permit!.to_h if document.is_a?(ActionController::Parameters) validate_payload(document) do |invalid_document, reason| yield invalid_document, reason if block_given? return {} end primary_data = document['data'] attributes = primary_data['attributes'] || {} attributes['id'] = primary_data['id'] if primary_data['id'] relationships = primary_data['relationships'] || {} filter_fields(attributes, ) filter_fields(relationships, ) hash = {} hash.merge!(parse_attributes(attributes, )) hash.merge!(parse_relationships(relationships, )) hash end |
.parse!(document, options = {}) ⇒ Hash
Transform a JSON API document, containing a single data object, into a hash that is ready for ActiveRecord::Base.new() and such. Raises InvalidDocument if the payload is not properly formatted.
77 78 79 80 81 |
# File 'lib/active_model_serializers/adapter/json_api/deserialization.rb', line 77 def parse!(document, = {}) parse(document, ) do |invalid_payload, reason| fail InvalidDocument, "Invalid payload (#{reason}): #{invalid_payload}" end end |
.parse_attributes(attributes, options) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
159 160 161 162 163 |
# File 'lib/active_model_serializers/adapter/json_api/deserialization.rb', line 159 def parse_attributes(attributes, ) transform_keys(attributes, ) .map { |(k, v)| { field_key(k, ) => v } } .reduce({}, :merge) end |
.parse_relationship(assoc_name, assoc_data, options) ⇒ Hash{Symbol, Object}
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Given an association name, and a relationship data attribute, build a hash mapping the corresponding ActiveRecord attribute to the corresponding value.
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/active_model_serializers/adapter/json_api/deserialization.rb', line 183 def parse_relationship(assoc_name, assoc_data, ) prefix_key = field_key(assoc_name, ).to_s.singularize hash = if assoc_data.is_a?(Array) { "#{prefix_key}_ids".to_sym => assoc_data.map { |ri| ri['id'] } } else { "#{prefix_key}_id".to_sym => assoc_data ? assoc_data['id'] : nil } end polymorphic = ([:polymorphic] || []).include?(assoc_name.to_sym) if polymorphic hash["#{prefix_key}_type".to_sym] = assoc_data.present? ? assoc_data['type'].classify : nil end hash end |
.parse_relationships(relationships, options) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
201 202 203 204 205 |
# File 'lib/active_model_serializers/adapter/json_api/deserialization.rb', line 201 def parse_relationships(relationships, ) transform_keys(relationships, ) .map { |(k, v)| parse_relationship(k, v['data'], ) } .reduce({}, :merge) end |
.transform_keys(hash, options) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
208 209 210 211 |
# File 'lib/active_model_serializers/adapter/json_api/deserialization.rb', line 208 def transform_keys(hash, ) transform = [:key_transform] || :underscore CaseTransform.send(transform, hash) end |
.validate_payload(payload) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Checks whether a payload is compliant with the JSON API spec.
rubocop:disable Metrics/CyclomaticComplexity
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/active_model_serializers/adapter/json_api/deserialization.rb', line 112 def validate_payload(payload) unless payload.is_a?(Hash) yield payload, 'Expected hash' return end primary_data = payload['data'] unless primary_data.is_a?(Hash) yield payload, { data: 'Expected hash' } return end attributes = primary_data['attributes'] || {} unless attributes.is_a?(Hash) yield payload, { data: { attributes: 'Expected hash or nil' } } return end relationships = primary_data['relationships'] || {} unless relationships.is_a?(Hash) yield payload, { data: { relationships: 'Expected hash or nil' } } return end relationships.each do |(key, value)| unless value.is_a?(Hash) && value.key?('data') yield payload, { data: { relationships: { key => 'Expected hash with :data key' } } } end end end |