exhal v8.2.0 ExHal.Transcoder behaviour
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" },
"app:manager": { "href": "http://example.com/people/84" }
}
}
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
deflink "app:manager", param: :manager_id, value_converter: PersonUrlConverter
end
PersonUrlConverter
is a module that has adopted the ExHal.ValueConverter
behavior.
defmodule PersonUrlConverter do
@behaviour ExHal.ValueConveter
def from_hal(person_url) do
to_string(person_url)
|> String.split("/")
|> List.last
end
def to_hal(person_id) do
"http://example.com/people/#{person_id}"
end
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",
manager_id: 84}
iex> PersonTranscoder.encode!(%{name: "Jane Doe", address: "123 Main St", department_url: "http://example.com/dept/42", manager_id: 84}) ~s( { "name": "Jane Doe", "mailingAddress": "123 Main St", "_links": {
"app:department": { "href": "http://example.com/dept/42" },
"app:manager": { "href": "http://example.com/people/84" }
} } )
Link to this section Summary
Functions
Define a link extractor & injector.
Define a link extractor & injector for links that may have more than one item.
Define a property extractor and injector.
Callbacks
Returns a decoded version of HAL document merged with the initial params.
Returns an HAL version of params provided, combined with the initial doc.
Updates an existing object, such as one created by ExHal.Transcoder.decode!
Link to this section Types
t()
t() :: module()
t() :: module()
Link to this section Functions
decode_value(atom, opts)
decode_value(raw_value, converter, opts)
deflink(rel, options \\ []) (macro)
Define a link extractor & injector.
- rel - the rel of the link in HAL
options - Keywords arguments
- :param - the key(s) in the param structure that maps to this link. Required.
- :templated - a boolean that adds a
templated: true
parameter if true - :value_converter - a
ExHal.Transcoder.ValueConverter
with which to convert the link target when en/decoding HAL
deflinks(rel, options \\ []) (macro)
Define a link extractor & injector for links that may have more than one item.
- rel - the rel of the link in HAL
options - Keywords arguments
- :param - the key(s) in the param structure that maps to this link. Required.
- :value_converter - a
ExHal.Transcoder.ValueConverter
with which to convert the link target when en/decoding HAL
defproperty(name, options \\ []) (macro)
Define a property extractor and injector.
- name - the name of the property in HAL
options - Keywords arguments
- :param - the key(s) in the param structure that map to this property. Default is
String.to_atom(name)
. - :value_converter - a
ExHal.Transcoder.ValueConverter
with which to convert the value to and from HAL
- :param - the key(s) in the param structure that map to this property. Default is
encode_value(raw_value, converter, opts)
put_link(atom, doc, _)
put_link(target, doc, rel, templated \\ false)
put_param(value, params, param_names)
put_property(value, doc, prop_name)
Link to this section Callbacks
decode!(arg1)
decode!(ExHal.Document.t()) :: %{}
decode!(ExHal.Document.t()) :: %{}
Returns a decoded version of HAL document merged with the initial params.
initial_params - the initial params with which the newly extracted info should merged. src_doc - the document to interpret opts - options for use by modules adopting ExHal.ValueConverterWithOptions behaviour
decode!(%{}, arg2)
decode!(%{}, ExHal.Document.t()) :: %{}
decode!(ExHal.Document.t(), keyword()) :: %{}
decode!(%{}, ExHal.Document.t()) :: %{}
decode!(ExHal.Document.t(), keyword()) :: %{}
decode!(%{}, arg2)
decode!(%{}, ExHal.Document.t()) :: %{}
decode!(ExHal.Document.t(), keyword()) :: %{}
decode!(%{}, ExHal.Document.t()) :: %{}
decode!(ExHal.Document.t(), keyword()) :: %{}
decode!(%{}, arg2, keyword)
decode!(%{}, ExHal.Document.t(), keyword()) :: %{}
decode!(%{}, ExHal.Document.t(), keyword()) :: %{}
encode!(%{})
encode!(%{}) :: ExHal.Document.t()
encode!(%{}) :: ExHal.Document.t()
Returns an HAL version of params provided, combined with the initial doc.
initial_doc - the initial document with which the newly encoded info should merged. src_params - the params to encoded into HAL opts - options for use by modules adopting ExHal.ValueConverterWithOptions behaviour
encode!(arg1, %{})
encode!(ExHal.Document.t(), %{}) :: ExHal.Document.t()
encode!(%{}, keyword()) :: ExHal.Document.t()
encode!(ExHal.Document.t(), %{}) :: ExHal.Document.t()
encode!(%{}, keyword()) :: ExHal.Document.t()
encode!(arg1, %{})
encode!(ExHal.Document.t(), %{}) :: ExHal.Document.t()
encode!(%{}, keyword()) :: ExHal.Document.t()
encode!(ExHal.Document.t(), %{}) :: ExHal.Document.t()
encode!(%{}, keyword()) :: ExHal.Document.t()
encode!(arg1, %{}, keyword)
encode!(ExHal.Document.t(), %{}, keyword()) :: ExHal.Document.t()
encode!(ExHal.Document.t(), %{}, keyword()) :: ExHal.Document.t()
patch!(%{}, list)
patch!(%{}, [%{}]) :: %{}
patch!(%{}, [%{}]) :: %{}
Updates an existing object, such as one created by ExHal.Transcoder.decode!
initial_object - a map containing properties, links etc patch_ops - a list of JSON-patch operations (https://tools.ietf.org/html/rfc6902)
Supported are "replace" for properties, links, and collections of links
and "add" for collections of links. For example:
[ %{"op => "replace", "path" => "/panicLevel", "value" => 42}, # replace a property
%{"op => "replace, "path" => "/_links/answers", "value" => [ "urn:earth", "urn:universe"]} # replace link collection
%{"op => "add", "path" => "/_links/answers/-", "value" => "urn:everyhing"}] # append to link collection
patch!(%{}, list, keyword)
patch!(%{}, [%{}], keyword()) :: %{}
patch!(%{}, [%{}], keyword()) :: %{}