Skip to content

Instantly share code, notes, and snippets.

@Marak
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Marak/9198ae386d392677ee8a to your computer and use it in GitHub Desktop.
Save Marak/9198ae386d392677ee8a to your computer and use it in GitHub Desktop.
hook.io microservice for adding colors to text
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