Last active
December 22, 2015 10:59
-
-
Save thomasfr/6462467 to your computer and use it in GitHub Desktop.
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
getResponseObject = (context) -> | |
# Later we could extend this to decide which ErrorResponse Object it should use | |
# based on the given error instance | |
if context instanceOf Error | |
object = new ErrorResponse context | |
else | |
object = new SuccessResponse context | |
send = (request, response, context) -> | |
object = getResponseObject context | |
response.statusCode = object.statusCode | |
# later we could do something like: if request.headers.accept = application/json | |
return response.json object | |
sendAsync = (request, response) -> | |
resultCallback = (error, result) -> | |
# is there a shorter coffeescript syntax for this if, else ? | |
if error | |
context = error | |
else | |
context = result | |
return sendResponse request, response, context | |
resultCallback | |
exports.get = (request, response) -> | |
id = parseInt(request.params.id) | |
if _.isNaN(id) | |
return send request, response, new Error 'NoValidId' | |
else | |
labels.get id, sendAsync(request, response) | |
exports.post = (request, response) -> | |
unless _.isObject(request.body) | |
return send request, response, new Error "InvalidParamsName" | |
labels.add request.body, sendAsync(request, response) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment