Skip to content

Instantly share code, notes, and snippets.

@monkbroc

monkbroc/button.js

Last active Nov 6, 2015
Embed
What would you like to do?
Button webhook
module['exports'] = function counter (hook) {
var expectedEvent = "magicButton";
function authorized() {
return hook.params.event === expectedEvent;
}
function increment(key, callback) {
var datastore = hook.datastore;
datastore.get(key, function (err, counter) {
if(err) {
callback(err);
} else {
counter = (counter || 0) + 1;
datastore.set(key, counter, function (err) {
if(err) {
callback(err);
} else {
callback(null, counter);
}
});
}
});
}
function outputResult(err, result) {
var data;
if(err) {
data = { error: err.message };
} else {
data = { result: result };
}
hook.res.end(JSON.stringify(data, true, 2));
}
// check that the event name in the hook matches the expected name
if(!authorized()) {
outputResult({ message: 'Unexpected event'});
return;
}
// increment a key in the cloud datastore
increment('counter', outputResult);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.