Skip to content

Commit

Permalink
Fixed paramater merging and added optional values
Browse files Browse the repository at this point in the history
  • Loading branch information
ytanay committed Mar 1, 2014
1 parent 9210d7a commit 9117f32
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 21 deletions.
34 changes: 20 additions & 14 deletions lib/ultrases.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ var util = require('./util');

function UltraSES(config){

if(!(this instanceof UltraSES))
return new UltraSES(config);

util.checkRequiredParams(config);

var AWS = require('aws-sdk');
Expand All @@ -22,9 +25,11 @@ function UltraSES(config){
}

UltraSES.prototype.send = function(data, done) {
data = this._typer(this._merge(data));
console.dir(data);
data = this._merge(data);
console.dir(data);
util.checkRequiredParamsSend(data);
this.ses.sendEmail({
var params = {
Source: data.from,
Destination: {
ToAddresses: (typeof data.to === 'string') ? [data.to] : data.to,
Expand All @@ -44,28 +49,29 @@ UltraSES.prototype.send = function(data, done) {
}
}
},
ReplyToAddresses: data.replyTo,
ReturnPath: data.returnPath
}, done);
};
if(data.replyTo) params.ReplyToAddresses = data.replyTo;
this.ses.sendEmail(params, done);
};

UltraSES.prototype.sendHTML = function(data, html, done) {
data = this._merge(data);
data.html = html;
data.text = require('html-to-text').fromString(html);
this.send(data, done);
};

UltraSES.prototype.sendTemplate = function(data, template, done) {
var compiled = require('jade').compile(template.template)(template.locals);
data.html = compiled;
data.text = require('html-to-text').fromString(compiled);
this.send(data, done);
};
if(!/\.jade$/.test(template.file)){
throw 'template file must be a jade template.';
return;
}

UltraSES.prototype._prep = function(data) {
data = this._merge(data);
data = this._typer(data);
require('fs').readFile(template.file, function(err, templateFile){
var compiled = require('jade').compile(templateFile)(template.locals);
data.html = compiled;
data.text = require('html-to-text').fromString(compiled);
this.send(data, done);
})
};

UltraSES.prototype._merge = function(data, defaults) {
Expand Down
24 changes: 17 additions & 7 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,26 @@ exports.checkRequiredParams = function checkRequiredParams(config) {
};

exports.checkRequiredParamsSend = function checkRequiredParamsSend(params) {
var props = {from: 'string', to: 'array', cc: 'array', bcc: 'array', subject: 'string', text: 'string', html: 'string', replyTo: 'array', returnPath: 'string'};
for(var prop in props){
if(props[prop] === 'array'){
if(!Array.isArray(params[prop])){
throw new TypeError('\'' + prop + '\' must be of array');
var doCheck = function __UltraSES_util_checkRequiredParamsSend_doCheck(prop, type, value, isOptional){
if(isOptional && typeof value === 'undefined') return;
if(type === 'array'){
if(!Array.isArray(value)){
params[prop] = [value];
}
} else {
if(typeof params[prop] !== props[prop]){
throw new TypeError('\'' + prop + '\' must be of ' + props[prop]);
if(typeof value !== type){
if(!isOptional) throw new TypeError('\'' + prop + '\' must be of type ' + type);
}
}
}
console.dir(params);
var required = {from: 'string', to: 'array', cc: 'array', bcc: 'array', subject: 'string', text: 'string', html: 'string'};
var optional = {replyTo: 'array', returnPath: 'string'};
for(var prop in required){
doCheck(prop, required[prop], params[prop]);
}
for(var prop in optional){
doCheck(prop, optional[prop], params[prop], true);
}

};

0 comments on commit 9117f32

Please sign in to comment.