class Yaks::Format::JsonAPI
Public Instance Methods
call(resource, _env = nil)
click to toggle source
@param [Yaks::Resource] resource @return [Hash]
# File lib/yaks/format/json_api.rb, line 10 def call(resource, _env = nil) output = {} if resource.collection? output[:data] = resource.map(&method(:serialize_resource)) output[:links] = serialize_links(resource.links) if resource.links.any? else output[:data] = serialize_resource(resource) end included = resource.seq.each_with_object([]) do |res, array| serialize_included_subresources(res.subresources, array) end output[:included] = included if included.any? output[:meta] = resource[:meta] if resource[:meta] output end
inverse()
click to toggle source
# File lib/yaks/format/json_api.rb, line 102 def inverse Yaks::Reader::JsonAPI.new end
serialize_included_resources(subresource, included)
click to toggle source
@param [Array] resources @param [Array] included @return [Array]
# File lib/yaks/format/json_api.rb, line 84 def serialize_included_resources(subresource, included) subresource.seq.each_with_object(included) do |resource, memo| serialize_subresource(resource, memo) end end
serialize_included_subresources(subresources, array)
click to toggle source
@param [Hash] subresources @param [Array] array @return [Array]
# File lib/yaks/format/json_api.rb, line 75 def serialize_included_subresources(subresources, array) subresources.each do |resources| serialize_included_resources(resources, array) end end
serialize_links(links)
click to toggle source
@param [Yaks::Resource] resource @return [Hash]
# File lib/yaks/format/json_api.rb, line 29 def serialize_links(links) links.each_with_object({}) do |link, hash| hash[link.rel] = link.uri end end
serialize_relationship(resource)
click to toggle source
@param [Yaks::Resource] resource @return [Array, Hash]
# File lib/yaks/format/json_api.rb, line 63 def serialize_relationship(resource) if resource.collection? data = resource.map { |r| {type: pluralize(r.type), id: r[:id].to_s} } elsif !resource.null_resource? data = {type: pluralize(resource.type), id: resource[:id].to_s} end {data: data} end
serialize_relationships(subresources)
click to toggle source
@param [Array] subresources @return [Hash]
# File lib/yaks/format/json_api.rb, line 55 def serialize_relationships(subresources) subresources.each_with_object({}) do |resource, hsh| hsh[resource.rels.first.sub(/^rel:/, '').to_sym] = serialize_relationship(resource) end end
serialize_resource(resource)
click to toggle source
@param [Yaks::Resource] resource @return [Hash]
# File lib/yaks/format/json_api.rb, line 37 def serialize_resource(resource) result = {} result[:type] = pluralize(resource.type) result[:id] = resource[:id].to_s if resource[:id] attributes = resource.attributes.reject { |k| k.equal?(:id) } result[:attributes] = attributes if attributes.any? relationships = serialize_relationships(resource.subresources) result[:relationships] = relationships unless relationships.empty? links = serialize_links(resource.links) result[:links] = links unless links.empty? result end
serialize_subresource(resource, included)
click to toggle source
{shows => [{id: '3', name: 'foo'}]}
@param [Yaks::Resource] resource @param [Hash] included @return [Hash]
# File lib/yaks/format/json_api.rb, line 95 def serialize_subresource(resource, included) included << serialize_resource(resource) unless included.any? do |item| item[:id].eql?(resource[:id].to_s) && item[:type].eql?(pluralize(resource.type)) end serialize_included_subresources(resource.subresources, included) end