module ActiveResource::Formats::JsonAPIFormat
Public Instance Methods
decode(json)
click to toggle source
# File lib/esp/extensions/active_resource/formats/json_api_format.rb, line 41 def decode(json) # pp ActiveSupport::JSON.decode(json), index: false, indent: -2 Formats.remove_root(parse_json_api(ActiveSupport::JSON.decode(json))) end
encode(hash, options = nil)
click to toggle source
# File lib/esp/extensions/active_resource/formats/json_api_format.rb, line 37 def encode(hash, options = nil) ActiveSupport::JSON.encode(hash, options) end
extension()
click to toggle source
# File lib/esp/extensions/active_resource/formats/json_api_format.rb, line 29 def extension "json".freeze end
extract_foreign_keys!(object, assoc, assoc_details)
click to toggle source
# File lib/esp/extensions/active_resource/formats/json_api_format.rb, line 90 def extract_foreign_keys!(object, assoc, assoc_details) data = assoc_details['data'] related_link = assoc_details.fetch('links', {}).fetch('related', {}) if data.present? parse_data(object, assoc, data) elsif related_link.present? parse_related_link(object, assoc, related_link) end end
merge_attributes!(object)
click to toggle source
# File lib/esp/extensions/active_resource/formats/json_api_format.rb, line 84 def merge_attributes!(object) return unless object.is_a? Hash object.merge! object.delete('attributes') unless object['attributes'].blank? end
merge_included_objects!(object, assoc, data, included)
click to toggle source
# File lib/esp/extensions/active_resource/formats/json_api_format.rb, line 121 def merge_included_objects!(object, assoc, data, included) return if included.blank? object[assoc] = case data when Array merge_nested_included_objects(object, data, included) when Hash merge_nested_included_objects(object, [data], included).first end end
merge_nested_included_objects(object, data, included)
click to toggle source
# File lib/esp/extensions/active_resource/formats/json_api_format.rb, line 132 def merge_nested_included_objects(object, data, included) assocs = included.compact.select { |i| data.include?(i.slice('type', 'id')) } # Remove the object from the included array to prevent an infinite loop if one of it's associations relates back to itself. assoc_included = included.dup assoc_included.delete(object) assocs.map { |i| parse_object!(i, assoc_included) } end
mime_type()
click to toggle source
# File lib/esp/extensions/active_resource/formats/json_api_format.rb, line 33 def mime_type "application/vnd.api+json".freeze end
parse_data(object, assoc, data)
click to toggle source
# File lib/esp/extensions/active_resource/formats/json_api_format.rb, line 101 def parse_data(object, assoc, data) if data.is_a? Array object["#{assoc.singularize}_ids"] = data.map { |d| d['id'] } else object["#{assoc}_id"] = data['id'] end end
parse_elements(object)
click to toggle source
# File lib/esp/extensions/active_resource/formats/json_api_format.rb, line 65 def parse_elements(object) object.each_value do |value| if value.is_a? Hash parse_object!(value) elsif value.is_a? Array value.map! { |o| parse_object!(o) } end end end
parse_json_api(elements)
click to toggle source
# File lib/esp/extensions/active_resource/formats/json_api_format.rb, line 46 def parse_json_api(elements) included = elements.delete('included') elements.tap do |e| Array.wrap(e.fetch('data', {})).each do |object| parse_object!(object, included) end end end
parse_object!(object, included = nil)
click to toggle source
# File lib/esp/extensions/active_resource/formats/json_api_format.rb, line 56 def parse_object!(object, included = nil) return object unless object.respond_to?(:each) merge_attributes!(object) parse_elements(object) parse_relationships!(object, included) object end
parse_relationships!(object, included)
click to toggle source
# File lib/esp/extensions/active_resource/formats/json_api_format.rb, line 76 def parse_relationships!(object, included) object.fetch('relationships', {}).each do |assoc, details| extract_foreign_keys!(object, assoc, details) merge_included_objects!(object, assoc, details['data'], included) end end