Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@lanthaler
Last active August 29, 2015 14:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lanthaler/11155856 to your computer and use it in GitHub Desktop.
Save lanthaler/11155856 to your computer and use it in GitHub Desktop.
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