Skip to content

Instantly share code, notes, and snippets.

@dreki
Last active August 29, 2015 14:19
Show Gist options
  • Save dreki/28f784e67410c6fcdc69 to your computer and use it in GitHub Desktop.
Save dreki/28f784e67410c6fcdc69 to your computer and use it in GitHub Desktop.
Feature-driven
// Feature definitions:
var features = {
'incoming email': [
when('email with link arrives', function() {
doThese([
action('extract article from link'),
action('send article as HTML attachment to Kindle')
]);
}),
when('email with no link arrives', function() {
doThese([
action('send email as HTML attachment to Kindle')
]);
})
]
};
// ...
// Incoming Express route that includes the details of a new email
router.post('/document/new', function (req, res, next) {
var emailBody = req.body['body-plain'];
var matches = /https?[\w:/.-]+/.exec(emailBody);
if (matches) {
vent.emit('email with link arrives',
{link: matches[0]});
}
else {
vent.emit('email with no link arrives',
{emailBody: req.body['body-html']});
}
res.send('thank you');
});
// ...
// Action definitions:
var actions = {
'extract article from link': function() {
var link = stash.pop('link');
var deferred = Q.defer();
request(link, function (err, res, body) {
if (!err) {
var article = articleExtractor.extractArticle(body);
stash.put('article', article);
deferred.resolve();
}
else {
deferred.reject();
}
});
return deferred.promise;
},
'send article as HTML attachment to Kindle': function() {
console.log('Time to send the article! (see below)');
console.log(stash.pop('article'));
// Init the library/service responsible for sending things
// to someone's Kindle
// ...
// Tell the library/service to send the article
// ...
},
'send email as HTML attachment to Kindle': function() {
console.log('Time to send the email! (see below)');
console.log(stash.pop('emailBody'));
// Init the library/service responsible for sending things
// to someone's Kindle
// ...
// Tell the library/service to send the email contents
// ...
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment