class Yaks::Format::Hal

Hypertext Application Language (stateless.co/hal_specification.html)

A lightweight JSON Hypermedia message format.

Options: :plural_links In HAL, a single rel can correspond to a single link, or to a list of links. Which rels are singular and which are plural is application-dependant. Yaks assumes all links are singular. If your resource might contain multiple links for the same rel, then configure that rel to be plural. In that case it will always be rendered as a collection, even when the resource only contains a single link.

@example

yaks = Yaks.new do
  format_options :hal, {plural_links: [:related_content]}
end

Public Instance Methods

inverse() click to toggle source
# File lib/yaks/format/hal.rb, line 28
def inverse
  Yaks::Reader::Hal.new
end
transitive?() click to toggle source
# File lib/yaks/format/hal.rb, line 24
def transitive?
  true
end

Protected Instance Methods

serialize_embedded(subresources) click to toggle source

@param [Array] subresources @return [Hash]

# File lib/yaks/format/hal.rb, line 87
def serialize_embedded(subresources)
  subresources.each_with_object({}) do |sub, memo|
    memo[sub.rels.first] = if sub.collection?
                             sub.map(&method(:serialize_resource))
                           elsif sub.null_resource?
                             nil
                           else
                             serialize_resource(sub)
                           end
  end
end
serialize_resource(resource) click to toggle source

@param [Yaks::Resource] resource @return [Hash]

# File lib/yaks/format/hal.rb, line 36
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.
  #
  result = resource.attributes

  if resource.links.any?
    result = result.merge(_links: serialize_links(resource.links))
  end

  if resource.collection?
    result = result.merge(_embedded:
                            serialize_embedded([resource]))
  elsif resource.subresources.any?
    result = result.merge(_embedded:
                            serialize_embedded(resource.subresources))
  end

  result
end
singular?(rel) click to toggle source

@param [String] rel @return [Boolean]

# File lib/yaks/format/hal.rb, line 81
def singular?(rel)
  !options.fetch(:plural_links) { [] }.include?(rel)
end