Sha256: 35e7aa213e2e410884362ba5fc6dc868202c3ac72b10673d30ae440286e93013
Contents?: true
Size: 1.68 KB
Versions: 3
Compression:
Stored size: 1.68 KB
Contents
# -*- coding: utf-8 -*- module Yaks class Serializer class Hal < self Serializer.register self, :hal, 'application/hal+json' protected def serialize_resource(resource) # The HAL spec doesn't say explicitly how to deal missing values, # looking at client behavior (Hyperagent) it seems safer to return an empty # resource. # # return nil if resource.is_a? NullResource result = resource.attributes result = result.merge(:_links => serialize_links(resource.links)) unless resource.links.empty? result = result.merge(:_embedded => serialize_embedded(resource.subresources)) unless resource.subresources.empty? result end def serialize_links(links) links.reduce({}, &method(:serialize_link)) end def serialize_link(memo, link) hal_link = {href: link.uri} hal_link.merge!(link.options.reject{|k,_| k==:templated}) hal_link.merge!(templated: true) if link.templated? memo[link.rel] = if singular?(link.rel) hal_link else Array(memo[link.rel]) + [hal_link] end memo end def singular?(rel) !options.fetch(:plural_links) { [] }.include?(rel) end def serialize_embedded(subresources) subresources.each_with_object({}) do |(rel, resources), memo| memo[rel] = if resources.collection? resources.map( &method(:serialize_resource) ) else serialize_resource(resources) end end end end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
yaks-0.4.1 | lib/yaks/serializer/hal.rb |
yaks-0.4.0 | lib/yaks/serializer/hal.rb |
yaks-0.4.0.rc1 | lib/yaks/serializer/hal.rb |