Last active
August 29, 2015 14:00
-
-
Save lanthaler/11155856 to your computer and use it in GitHub Desktop.
APItools middleware to convert apibunny.com responses to JSON-LD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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