Skip to content

Instantly share code, notes, and snippets.

@m-o-e
Created April 10, 2022 18:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m-o-e/6bf70d390635a644b298a5584cce4ff8 to your computer and use it in GitHub Desktop.
Save m-o-e/6bf70d390635a644b298a5584cce4ff8 to your computer and use it in GitHub Desktop.
lux peek
Lux.post "/cars" do
description "Create a Car"
body_param type, String, eq: "Car"
body_param attributes, Car
json_root "data"
response 409, AlreadyExists, JsonApi::Error
response 201, Success, JsonApi::Success, { data: Array({ type: String, attributes: Car }) }
handle do
if (rand(1.0) > 0.5)
Success.new([{ type: "Car", attributes: b_attributes }])
else
AlreadyExists.new("This car already exists!")
end
end
end
➜ http -j POST ':1313/cars' data:='{"type": "Car", "attributes": { "plate": "123", "brand": "mercedes", "color": "red" } }' content-type:"application/vnd.api+json" 'accept:application/json'
HTTP/1.1 409 Conflict
Connection: keep-alive
Content-Encoding: gzip
Content-Length: 131
Content-Type: application/vnd.api+json
Request-Id: c373346f-ea6c-414d-97d4-9e03e0711361
Server: Lux
{
"errors": [
{
"code": "already_exists",
"id": "c373346f-ea6c-414d-97d4-9e03e0711361",
"status": "409",
"title": "This car already exists!"
}
]
}
➜ http -j POST ':1313/cars' data:='{"type": "Car", "attributes": { "plate": "123", "brand": "mercedes", "color": "red" } }' content-type:"application/vnd.api+json" 'accept:application/json'
HTTP/1.1 201 Created
Connection: keep-alive
Content-Encoding: gzip
Content-Length: 131
Content-Type: application/json
Request-Id: 7d6b91ef-7d9a-479a-a31b-0bad7f3e3f92
Server: Lux
{
"data": [
{
"attributes": {
"brand": "mercedes",
"color": "red",
"id": "eafdfd44-f417-40cc-94c5-ffb875b19ad6",
"plate": "123"
},
"type": "Car"
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment