Last active
August 29, 2015 14:23
-
-
Save Marak/9198ae386d392677ee8a to your computer and use it in GitHub Desktop.
hook.io microservice for adding colors to text
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
var _colors = ["random", "red", "green", "blue", "yellow", "cyan", "rainbow", "zalgo"]; | |
module['exports'] = function colorsHook (hook) { | |
var colors = require('colors'), | |
color = hook.params.color; | |
var text = hook.params.text || ""; | |
// if random color is selected, return a random color | |
if (color === "random") { | |
color = _colors[Math.floor(Math.random()*_colors.length+1)-1] | |
} | |
if (text.length === 0) { | |
text = color; | |
// hook.res.end(color); | |
} | |
// If the hook is not currently streaming, req has already been fully buffered | |
if (!hook.streaming) { | |
// | |
// To test a streaming Hook you can use Curl: | |
// | |
// echo "foo" | curl --header "content-type: application/octet-stream" --data-binary @- http://hook.io/Marak/transform | |
// | |
return hook.res.end(colors[color](text)); | |
} | |
hook.req.on('end', function(){ | |
hook.res.end('request completed'); | |
}); | |
hook.req.on('data', function(chunk){ | |
hook.res.write(colors[color](chunk.toString())) | |
}); | |
}; | |
module['exports'].schema = { | |
"text": { | |
"type": "string" | |
}, | |
"color": { | |
"type": "string", | |
"enum": _colors, | |
"default": "random" | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment