Module: Roar::JSON::HAL

Defined in:
lib/roar/json/hal.rb

Overview

Including the JSON::HAL module in your representer will render and parse documents following the HAL specification: http://stateless.co/hal_specification.html Links will be embedded using the _links key, nested resources with the _embedded key.

Embedded resources can be specified when calling #property or +collection using the :embedded => true option.

Link arrays can be defined using ::links.

CURIEs are specified with the - surprise - ::curie class method.

Example:

module OrderRepresenter
include Roar::JSON::HAL

property :id
collection :items, :class => Item, :extend => ItemRepresenter, :embedded => true

link :self do
  "http://orders/#{id}"
end

links :self do
  [{:lang => "en", :href => "http://en.hit"},
   {:lang => "de", :href => "http://de.hit"}]
end

curies do
  [{:name => :doc,
    :href => "//docs/{rel}",
    :templated => true}
  ]
end
end

Renders to

"{\"id\":1,\"_embedded\":{\"items\":[{\"value\":\"Beer\",\"_links\":{\"self\":{\"href\":\"http://items/Beer\"}}}]},\"_links\":{\"self\":{\"href\":\"http://orders/1\"}}}"

Defined Under Namespace

Modules: Links, LinksReader, Resources

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/roar/json/hal.rb', line 47

def self.included(base)
  base.class_eval do
    include Roar::JSON
    include Links       # overwrites #links_definition_options.
    include Resources
    include LinksReader # gives us Decorator#links => {self=>< >}
  end
end