Fixed minor code issues caught by jslint. Added more code comments to app.js.

master
kts of kettek 2015-12-19 22:39:29 -08:00
parent 3c1a783bca
commit 01458f1e2e
1 changed files with 33 additions and 14 deletions

41
app.js
View File

@ -77,21 +77,28 @@ These are:
*/ */
function Newsletter(data) { function Newsletter(data) {
this.temp = {}; this.temp = {}; // temporary Letter object (html, text, subject)
this.repo = {}; this.repo = {};
this.subscribers = []; this.subscribers = []; // array of email addresses
this.options = {}; this.options = {}; // key=>value pairs used during building
this.transport_options = { this.transport = null; // future Transport object
this.transport_options = { // options to be passed to the Transport object
host: 'localhost', host: 'localhost',
port: 25 port: 25
}; };
this.transport = null;
// merge passed Object into Newsletter // merge passed Object into Newsletter
for (attr in data) { for (var attr in data) {
if (this[attr]) this[attr] = mergeObjects(this[attr], data[attr]); if (this[attr]) this[attr] = mergeObjects(this[attr], data[attr]);
else this[attr] = data[attr]; else this[attr] = data[attr];
} }
} }
/* Method: Newsletter.build(file, options, cb)
````````````````````````````````
This method is the first step during the sending of a newsletter. It attempts
to read in the given file name, optionally replacing particular keys in the file
with the variables stored in the options object, and eventually calling cb once
finished.
*/
Newsletter.prototype.build = function(file, options, cb) { Newsletter.prototype.build = function(file, options, cb) {
// clear potential old newsletter // clear potential old newsletter
this.temp = {}; this.temp = {};
@ -104,6 +111,13 @@ Newsletter.prototype.build = function(file, options, cb) {
this.temp.text = content; this.temp.text = content;
this.temp.html = content; this.temp.html = content;
}; };
/* Method: Newsletter.send()
````````````````````````````````
This method is the second step of sending a newsletter. It takes the data built
during the `build` method, as stored in the `temp` property, and attempts to
send it to all the emails stored in the `subscribers` property through the
nodemailer Transport as stored in the `transport` property.
*/
Newsletter.prototype.send = function() { Newsletter.prototype.send = function() {
var i = 0; var i = 0;
var len = this.subscribers.length; var len = this.subscribers.length;
@ -141,6 +155,11 @@ Newsletter.prototype.send = function() {
html: (this.temp.html ? this.temp.html : '') html: (this.temp.html ? this.temp.html : '')
}); });
}; };
/* Method: archive(letter, file)
````````````````````````````````
This method is the third step of sending a newsletter. It simply archives the
given file in the 'archive/' directory of the provided newsletter name.
*/
Newsletter.prototype.archive = function(letter, file) { Newsletter.prototype.archive = function(letter, file) {
fs.writeFileSync('./newsletters/'+letter+'/archive/'+path.basename(file), fs.readFileSync(file)); fs.writeFileSync('./newsletters/'+letter+'/archive/'+path.basename(file), fs.readFileSync(file));
}; };
@ -155,7 +174,7 @@ var mergeObjects = function(obj1, obj2) {
else obj3 = {}; else obj3 = {};
for (var attr in obj1) { obj3[attr] = obj1[attr]; } for (var attr in obj1) { obj3[attr] = obj1[attr]; }
for (var attr in obj2) { obj3[attr] = obj2[attr]; } for (attr in obj2) { obj3[attr] = obj2[attr]; }
return obj3; return obj3;
}; };
@ -163,9 +182,9 @@ exports.mergeObjects = mergeObjects;
/* Function: sendNewsletter(newsletter object, options, letter, file) /* Function: sendNewsletter(newsletter object, options, letter, file)
```````````````````````````````` ````````````````````````````````
General function that iteratively calls: General function that iteratively calls:
build newsletter.build
send newsletter.send
archive newsletter.archive
*/ */
var sendNewsletter = function(newsletter, options, letter, file) { var sendNewsletter = function(newsletter, options, letter, file) {
newsletter.build(file, options, function(err) {}); newsletter.build(file, options, function(err) {});
@ -195,7 +214,7 @@ if (require.main == module) { // run as an app only if this is the main module
if (nl.archive == Newsletter.prototype.archive) { if (nl.archive == Newsletter.prototype.archive) {
console.log('I: Newsletter is using default `archive` step'); console.log('I: Newsletter is using default `archive` step');
} }
if (nl.transport == null) { if (nl.transport === null) {
// create default transport // create default transport
console.log('I: Newsletter has no custom transport, creating default'); console.log('I: Newsletter has no custom transport, creating default');
nl.transport = nodemailer.createTransport(nl.transport_options); nl.transport = nodemailer.createTransport(nl.transport_options);