Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
APItools middleware to convert apibunny.com responses to JSON-LD
return function(request, next_middleware)
local response = next_middleware()
if (response.headers['content-type'] == 'application/json') then
local data = json.decode(response.body)
if (data.mazes) then
data['@context'] = 'http://www.hydra-cg.com/examples/maze'
data['id'] = data.mazes[1].id
data['@type'] = 'Maze'
data['name'] = data.mazes[1].name
for relation, value in pairs(data.mazes[1].links) do
data[relation] = '/cells/' .. value
end
data.mazes = nil
data.links = nil
elseif (data.cells) then
data['@context'] = 'http://www.hydra-cg.com/examples/maze'
data['id'] = data.cells[1].id
data['name'] = data.cells[1].name
data['@type'] = data.cells[1].type:gsub("^%l", string.upper)
for relation, value in pairs(data.cells[1].links) do
if (relation == 'maze') then
data[relation] = '/mazes/' .. value
else
data[relation] = value
end
end
if (data.cells[1].exit_link) then
data.exit = '/users/'
end
data.cells = nil
data.links = nil
end
response.body = json.encode(data)
response.headers['content-type'] = 'application/ld+json'
end
return response
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.