UNPKG

notificator

Version:

Library for handling custom notifications, emails, APS, GCM...

196 lines (167 loc) 6.21 kB
// Generated by CoffeeScript 1.10.0 (function() { var ChannelTemplate, Destination, NotificatorChannel, assert, swig, util; swig = require('swig'); util = require('util'); assert = require('assert'); Destination = (function() { function Destination(destination1, language1) { this.destination = destination1; this.language = language1 != null ? language1 : null; } Destination.prototype.toString = function() { return this.destination + (this.language ? "(" + this.language + ")" : ""); }; return Destination; })(); ChannelTemplate = (function() { function ChannelTemplate() {} ChannelTemplate.prototype.getMessage = function(data) { var _data; _data = JSON.parse(JSON.stringify(data)); _data._data = JSON.parse(JSON.stringify(data)); return this.parseObjectValues(this, _data); }; ChannelTemplate.prototype.parseObjectValues = function(object, data) { var key, result, value; result = {}; for (key in object) { value = object[key]; if (value === null || !value) { result[key] = null; } else if (typeof value === 'object') { result[key] = this.parseObjectValues(value, data); } else if (typeof value === 'string') { value = swig.render(value, { locals: data }); result[key] = value; } else if (typeof value !== 'function') { result[key] = value; } } return result; }; return ChannelTemplate; })(); NotificatorChannel = (function() { function NotificatorChannel(options) { this.options = options != null ? options : {}; if (!this.options.getDestinations) { this.options.getDestinations = function(obj, callback) { return callback(new Error('options.getDestinations not specified')); }; } if (!this.options.getTemplates) { this.options.getTemplates = function(obj, language, callback) { return callback(new Error('options.getTemplates not specified')); }; } } NotificatorChannel.prototype.wrappedDestination = function(destination) { if (!(destination instanceof Destination)) { if (typeof destination !== 'string') { destination = new Destination(destination.destination, destination.language || destination.lang); } else { destination = new Destination(destination); } } return destination; }; NotificatorChannel.prototype.getDestinations = function(receiver, callback) { return this.options.getDestinations(receiver, (function(_this) { return function(err, _destinations) { var destination, destinations, error, i, len; if (err) { return callback(err); } try { destinations = []; for (i = 0, len = _destinations.length; i < len; i++) { destination = _destinations[i]; if (typeof destination === 'object') { destination = Object.create(destination); } if (destination) { destination = _this.wrappedDestination(destination); } _this.validateDestination(destination); destinations.push(destination); } return callback(null, destinations); } catch (error) { err = error; return callback(err); } }; })(this)); }; NotificatorChannel.prototype.getTemplates = function(info, callback) { assert.ok(info.event, 'event must be specified when getting templates'); return this.options.getTemplates(info, (function(_this) { return function(err, templates) { var error, i, j, len, len1, template, transformedTemplates; if (err) { return callback(err); } templates = templates || []; if (templates && !Array.isArray(templates)) { templates = [templates]; } templates = templates.filter(function(x) { return x; }); if (templates.length === 0 && _this.options.defaultTemplate) { templates = [_this.options.defaultTemplate]; } try { transformedTemplates = []; for (i = 0, len = templates.length; i < len; i++) { template = templates[i]; transformedTemplates.push(_this.transformTemplate(template)); } for (j = 0, len1 = transformedTemplates.length; j < len1; j++) { template = transformedTemplates[j]; _this.validateTemplate(template); } _this.debug('gottemplates', transformedTemplates); return callback(null, transformedTemplates); } catch (error) { err = error; return callback(err); } }; })(this)); }; NotificatorChannel.prototype.transformTemplate = function(template) { if (!(template instanceof ChannelTemplate)) { throw new Error('template must be type of ChannelTemplate and transformTemplate wasn\'t used'); } return template; }; NotificatorChannel.prototype.sendMessage = function(message, destination, callback) { return callback(new Error('sendMessage not implemented')); }; NotificatorChannel.prototype.validateDestination = function(destination) { if (!destination) { throw new Error(util.format(destination) + ' is not a valid destination'); } return true; }; NotificatorChannel.prototype.validateTemplate = function(template) { return true; }; NotificatorChannel.prototype.debug = function() { if (this.options.debug) { return console.log.apply(console, arguments); } }; NotificatorChannel.prototype.name = function() { return 'unknown'; }; return NotificatorChannel; })(); NotificatorChannel.ChannelTemplate = ChannelTemplate; NotificatorChannel.Destination = Destination; module.exports = NotificatorChannel; }).call(this);