Skip to content

Instantly share code, notes, and snippets.

View Marak's full-sized avatar

Marak

View GitHub Profile
@Marak
Marak / spellcheckHook.js
Last active August 29, 2015 14:23
hook.io microservice for spellchecking
module['exports'] = function echoHttp (hook) {
var path = require('path');
var SpellCheck = require('spellcheck'),
base = __dirname + "../../../../modules/builds/hunspell/en_US/";
base = path.resolve(base);
base = base + "/";
spell = new SpellCheck(base + 'en_US.aff', base + 'en_US.dic');
// If the hook is not currently streaming, req has already been fully buffered
@Marak
Marak / geoipHook.js
Last active August 29, 2015 14:23
hook.io microservice for geoip
module['exports'] = function geoipHook (hook) {
var geoip = require('geoip-lite');
var ip = hook.params.ip;
if (typeof ip === "undefined" || ip.length === 0) {
ip = hook.req.headers['x-forwarded-for'];
}
console.log('attempting to lookup ' + ip);
var geo = geoip.lookup(ip);
if (geo === null) {
return hook.res.end(JSON.stringify({ message: "invalid ip " + ip.toString(), error: true }, true, 2));
@Marak
Marak / markdownHook.js
Last active August 29, 2015 14:23
hook.io microservice for rendering markdown to HTML
module['exports'] = function markdownHook (hook) {
var marked = require('marked');
// TODO: expose parser options through schema
marked.setOptions({
renderer: new marked.Renderer(),
gfm: true,
tables: true,
breaks: false,
pedantic: false,
sanitize: true,
@Marak
Marak / coinHook.js
Last active August 29, 2015 14:23
hook.io microservice for getting crypto-coin price data from coinmarketcap.com
module['exports'] = function getCoin (hook) {
var request = require('request');
var url = 'http://coinmarketcap-nexuist.rhcloud.com/api/' + hook.params.coin;
request(url, { json: true }, function (err, response, body) {
if (hook.params.currency !== 'all') {
// if a specific currency is selected, filter the results
return hook.res.end(body['price'][hook.params.currency]);
} else {
// else, send all coin data back
hook.res.end(JSON.stringify(body));
@Marak
Marak / test.js
Last active August 29, 2015 14:24
testGist
module['exports'] = function echoHttp (hook) {
hook.debug("test messages are sent to the debug console");
hook.debug(hook.params);
hook.debug(hook.req.path);
hook.debug(hook.req.method);
@Marak
Marak / proxy.js
Created July 14, 2015 04:12
hook.io microservice for proxying http requests
module['exports'] = function proxyHook (hook) {
hook.res.end('proxied');
};
@Marak
Marak / badTimeOutHook.js
Last active August 29, 2015 14:24
hook.io hook with timeout error ( never ends )
module['exports'] = function badTimeOutHook (hook) {
hook.res.write('hello');
// never calls hook.res.end()
};
@Marak
Marak / gatewayHook.js
Last active August 29, 2015 14:25
hook.io microservice for executing arbitrary functions without an account
// gateway hook for running microservices on hook.io in real-time without an account
// example of deploying a js file to the hook.io cloud from the command line using cURL
// cat echo.js | curl --data-urlencode source@- http://gateway.hook.io
module['exports'] = function gatewayHook (hook) {
var mschema = require('mschema');
var params = hook.params;
var source = params.source;
@Marak
Marak / testHook.js
Created July 23, 2015 03:41
My first hook.io microservice
module['exports'] = function echo (hook) {
// Access Node.js HTTP Request and Response objects
var req = hook.req,
res = hook.res;
// Use NPM Modules
var faker = require('faker');
res.write('<strong>BYE BILLY</strong> ');
res.write(req.headers["x-forwarded-for"] + '<br/>');
res.end(faker.hacker.phrase());
};
@Marak
Marak / composeHook.js
Created July 24, 2015 06:19
hook.io example microservice for composing multiple services
module['exports'] = function (hook) {
hook.res.end('ended');
};
module['exports'].inputs = ['echo', 'echo', 'echo'];
module['exports'].outputs = ['echo', 'echo', 'echo'];