https://github.com/podlove/hal
Elixir library to generate JSON HAL responses
https://github.com/podlove/hal
elixir elixir-lang elixir-library
Last synced: 11 months ago
JSON representation
Elixir library to generate JSON HAL responses
- Host: GitHub
- URL: https://github.com/podlove/hal
- Owner: podlove
- License: mit
- Created: 2019-02-12T08:47:39.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-02-13T04:11:31.000Z (over 7 years ago)
- Last Synced: 2024-10-30T05:43:01.049Z (over 1 year ago)
- Topics: elixir, elixir-lang, elixir-library
- Language: Elixir
- Size: 12.7 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# HAL
[](https://travis-ci.org/podlove/hal)
Generate JSON in [HAL](http://stateless.co/hal_specification.html) format for REST APIs.
## Installation
The package can be installed by adding `hal` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[
{:hal, "~> 1.0.0"}
]
end
```
## Basic Usage
Build a `HAL.Document`:
```elixir
alias HAL.{Document, Link, Embed}
document =
%Document{}
|> Document.add_link(%Link{rel: "self", href: "/foo"})
|> Document.add_property(:foo, 42)
|> Document.add_property(:bar, "baz")
|> Document.add_embed(%Embed{resource: "rad:podcast", embed: %HAL.Document{properties: %{id: 18}}})
```
Then use `Jason` to encode it to JSON:
```elixir
Jason.encode!(document)
```
```json
{
"bar": "baz",
"foo": 42,
"_embedded": {
"rad:podcast": {
"id": 18
}
},
"_links": {
"self": {
"href": "/foo"
}
}
}
```
Instead of using the Document builder, you can handwrite the `HAL.Document`:
```elixir
%HAL.Document{
embeds: [
%HAL.Embed{
embed: %HAL.Document{embeds: [], links: [], properties: %{id: 18}},
resource: "rad:podcast"
}
],
links: [%HAL.Link{href: "/foo", rel: "self", title: nil}],
properties: %{bar: "baz", foo: 42}
}
|> Jason.encode!()
```
## License
HAL is released under the MIT License - see the [LICENSE](https://github.com/podlove/hal/blob/master/LICENSE) file.