exhal v4.4.0 ExHal.Transcoder

Helps to build transcoders for HAL documents.

Given a document like

{
  "name": "Jane Doe",
  "mailingAddress": "123 Main St",
  "_links": {
    "app:department": { "href": "http://example.com/dept/42" }
  }
}

We can define an transcoder for it.

defmodule PersonTranscoder do
  use ExHal.Transcoder

  defproperty "name"
  defproperty "mailingAddress", param: :address
  deflink     "app:department", param: :department_url
end

We can use this transcoder to to extract the pertinent parts of the document into a map.

iex> PersonTranscoder.decode!(doc)
%{name: "Jane Doe",
  address: "123 Main St",
  department_url: "http://example.com/dept/42"}

iex> PersonTranscoder.encode!(%{name: “Jane Doe”, address: “123 Main St”, department_url: “http://example.com/dept/42"}) ~s( { “name”: “Jane Doe”, “mailingAddress”: “123 Main St”, “_links”: {

"app:department": { "href": "http://example.com/dept/42" }

} } )


Summary

Macros

Define a link extractor & injector

Define a property extractor and injector

Functions

extract(params, doc, param_name, value_extractor)

Macros

deflink(rel, options \\ [])

Define a link extractor & injector.

  • rel - the rel of the link in HAL
  • options - Keywords arguments
  • :param - the name of the param that maps to this link. Required.
defproperty(name, options \\ [])

Define a property extractor and injector.

  • name - the name of the property in HAL
  • options - Keywords arguments
  • :param - the name of the param that maps to this property. Default is String.to_atom(name).