Skip to content
Browse files

Web and SMTP api have optional callbacks.

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...
1 parent bf5558f commit 66cdb6d85e1d168ca9e77e37cc8cbf4b7c7ed61d @theycallmeswift theycallmeswift committed Oct 13, 2012
Showing with 31 additions and 7 deletions.
  1. +11 −7 lib/sendgrid.js
  2. +20 −0 test/integration/sendgrid.test.js
View
18 lib/sendgrid.js
@@ -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();
@@ -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);
});
});
@@ -93,7 +95,7 @@ SendGrid.prototype.send = function(email, callback) {
if (success) {
send_rest();
} else {
- callback(false, message);
+ cb(false, message);
}
});
} else {
@@ -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", {
@@ -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);
});
}
@@ -141,7 +145,7 @@ SendGrid.prototype.smtp = function(email, callback) {
if (success) {
send_smtp();
} else {
- callback(false, message);
+ cb(false, message);
}
});
} else {
View
20 test/integration/sendgrid.test.js
@@ -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() {
@@ -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) {

0 comments on commit 66cdb6d

Please sign in to comment.
Something went wrong with that request. Please try again.