Skip to content

sseletskyy/notifier

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

93 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Notifier

HTTP API that receives the event and turning that event into corresponding notification.

API

The entry point of application responsible for initializing the notifier.

var notifier = require('./source/notifier');

notifier.listen(5050);

To initialize the notifier you should create 3 entities - actions, resovers and executors.

Receiving an event

notifier exposes .action() call to initialize particular action. The action callback is called then server receives event with defined type.

notifier.action('user-registered', function (event, actions, callback) {
	actions.create('send-welcome-email', {user: event.user}, callback);
});

You can define as many actions as you need for same event.

notifier.actions('user-payment-recieved', function (event, actions, callback) {
	actions.create('send-invoice-email', {user: event.user, payment: event.amount}, callback);
});

notifier.actions('user-payment-recieved', function (event, actions, callback) {
	actions.create('notify-developers-sms', {user: event.user}, callback);
});

Resolving an action

To resolve an action, notifier should define resolved. Usually resolve calls database or other service for additional data.

notifier.resolve('user-registered', function (action, callback) {
	db.user.findOne({email: action.email}, function (err, user) {
		if (err) {
			return callback(err);
		}

		var data = {
			email: user.email,
			firstName: user.firstName,
			secondName: user.secondName,
			registered: user.registered
		};

		callback(null, action, data);
	});
});

Executing action

Once action got resolve, it's ready to be executed.

notifier.execute('user-registered', function (action, transport, callback) {
		var vars = [
			{name: 'FIRST_NAME', content: action.data.firstName}
			{name: 'SECOND_NAME', content: action.data.secondName}
			{name: 'REGISTERED_DATE', content: action.data.registered}
		];

		transport.mandrill.sendTemplate(action.email, vars, 'welcome-email', callback);
});

The callback should receive (err, action, data) - error, same action and resolved data.

Transports

Mandrill, SendGrid, MailGun, Twillio etc..

TDB.

How to use?

Clone repo,

$ git clone git@github.com:likeastore/notifier.git

Create app.js file,

var notifier = require('./source/notifier');

// initialize actions, resolvers and executors
notifier
	.action('incoming-event', function () { /* ... */ })
	.resolve('created-action', function () { /* ... */ })
	.execute('created-action', function () { /* ... */ });

// start the server
notifier.listen(process.env.PORT);

Update development.config.js and production.config.js configuration. For now, configuration requires connection string to MongoDB, accessToken (shared secret) to access service, mandrill and logentries tokens.

Commit the changes and deploy (heroku, dokku, aws).

$ git push master heroku

Check the server deployed fine,

$ curl http://notifier.likeastore.com/
{"app":"notifier","env":"production","version":"0.0.5","apiUrl":"/api"}%

Send first notification,

$ echo '{"event": "incoming-event"}' | curl -d @- http://notifier.likeastore.com/api/events?access_token=ACCESS_TOKEN

License (MIT)

Copyright (c) 2014, Likeastore.com info@likeastore.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

Recieved email from Likeastore.com? Now you know, it's me responsible for that.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%