Skip to content

Commit

Permalink
Web and SMTP api have optional callbacks.
Browse files Browse the repository at this point in the history
In case you don't care about the callback for `send` or `smtp`,
it is now optional to supply a callback function.
  • Loading branch information
theycallmeswift committed Oct 13, 2012
1 parent bf5558f commit 66cdb6d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
18 changes: 11 additions & 7 deletions lib/sendgrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ function SendGrid(api_user, api_key) {
* @param {Email|Object} email An email object or a hash that has
* the values for the email to be sent.
* @param {Function} callback A function to call when the processing is done.
* This parameter is optional.
*/
SendGrid.prototype.send = function(email, callback) {
var self = this;
var self = this
, cb = callback || function() { };

var boundary = Math.random();

Expand Down Expand Up @@ -71,7 +73,7 @@ SendGrid.prototype.send = function(email, callback) {
});
res.on('end', function() {
var json = JSON.parse(content);
callback(json.message == 'success', json.errors);
cb(json.message == 'success', json.errors);
});
});

Expand All @@ -93,7 +95,7 @@ SendGrid.prototype.send = function(email, callback) {
if (success) {
send_rest();
} else {
callback(false, message);
cb(false, message);
}
});
} else {
Expand All @@ -108,10 +110,12 @@ SendGrid.prototype.send = function(email, callback) {
* @param {Email|Object} email An email object or a hash that has
* the values for the email to be sent.
* @param {Function} callback A function to call when the processing is done.
* This parameter is optional.
*/
SendGrid.prototype.smtp = function(email, callback) {
var self = this
, smtpTransport;
, smtpTransport
, cb = callback || function() { };

// SMTP settings
smtpTransport = nodemailer.createTransport("SMTP", {
Expand All @@ -126,9 +130,9 @@ SendGrid.prototype.smtp = function(email, callback) {
smtpTransport.sendMail(email.toSmtpFormat(), function(error, response) {
smtpTransport.close();
if(error) {
return callback(false, response);
return cb(false, response);
}
return callback(true, response);
return cb(true, response);
});
}

Expand All @@ -141,7 +145,7 @@ SendGrid.prototype.smtp = function(email, callback) {
if (success) {
send_smtp();
} else {
callback(false, message);
cb(false, message);
}
});
} else {
Expand Down
20 changes: 20 additions & 0 deletions test/integration/sendgrid.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,16 @@ describe('SendGrid', function () {
done();
});
});

it('has an optional callback', function(done) {
var mail = new Email(text_params)

expect(function() {
sendgrid.send(mail);
}).to.not.throw(Error);

done();
});
});

describe('Smtp Api', function() {
Expand Down Expand Up @@ -220,6 +230,16 @@ describe('SendGrid', function () {
done();
});
});

it('has an optional callback', function(done) {
var mail = new Email(text_params)

expect(function() {
sendgrid.smtp(mail);
}).to.not.throw(Error);

done();
});
});

describe('x-smtpapi', function(done) {
Expand Down

0 comments on commit 66cdb6d

Please sign in to comment.