diff options
-rw-r--r-- | lib/email.js | 6 | ||||
-rw-r--r-- | lib/sendgrid.js | 2 | ||||
-rw-r--r-- | lib/smtpapi_headers.js | 97 | ||||
-rw-r--r-- | npm-shrinkwrap.json | 31 | ||||
-rw-r--r-- | package.json | 3 | ||||
-rw-r--r-- | test/lib/sendgrid.test.js | 16 | ||||
-rw-r--r-- | test/lib/smtpapi_header.test.js | 212 |
7 files changed, 22 insertions, 345 deletions
diff --git a/lib/email.js b/lib/email.js index d84e8a1..c895de4 100644 --- a/lib/email.js +++ b/lib/email.js @@ -1,7 +1,7 @@ "use strict"; -var SmtpapiHeaders = require('./smtpapi_headers'); var FileHandler = require('./file_handler'); +var xsmtpapi = require('xsmtpapi'); var _ = require('lodash'); var request = require('request'); var fs = require('fs'); @@ -28,7 +28,7 @@ function Email(params) { this.to = params.to || []; this.from = params.from || ''; - this.smtpapi = params.smtpapi || new SmtpapiHeaders(); + this.smtpapi = params.smtpapi || new xsmtpapi.Header(); this.subject = params.subject || ''; this.text = params.text || ''; this.html = params.html || ''; @@ -202,7 +202,7 @@ Email.prototype.toWebFormat = function() { var web = { to : this.to, from : this.from, - 'x-smtpapi' : this.smtpapi.toJson(), + 'x-smtpapi' : this.smtpapi.toJsonString(), subject : this.subject, text : this.text, html : this.html, diff --git a/lib/sendgrid.js b/lib/sendgrid.js index 96126b9..7b5f59b 100644 --- a/lib/sendgrid.js +++ b/lib/sendgrid.js @@ -4,7 +4,6 @@ var package_json = require('./../package.json'); var _ = require('lodash'); var request = require('request'); var Email = require('./email'); -var SmtpapiHeaders = require('./smtpapi_headers'); var Sendgrid = function(api_user, api_key, options) { if( !(this instanceof Sendgrid) ) { @@ -106,7 +105,6 @@ var Sendgrid = function(api_user, api_key, options) { */ this.version = package_json.version; this.Email = Email; - this.SmtpapiHeaders = SmtpapiHeaders; this.api_user = api_user; this.api_key = api_key; this.web = web; diff --git a/lib/smtpapi_headers.js b/lib/smtpapi_headers.js deleted file mode 100644 index 410fe0b..0000000 --- a/lib/smtpapi_headers.js +++ /dev/null @@ -1,97 +0,0 @@ -"use strict"; - -var _ = require('lodash'); - -function SmtpapiHeaders() { - this.to = []; - this.sub = {}; - this.unique_args = {}; - this.category = []; - this.filters = {}; - this.section = {}; -} - -SmtpapiHeaders.prototype.addTo = function(to) { - if (_.isArray(to)) { - this.to = this.to.concat(to); - } else { - this.to.push(to); - } -}; - -SmtpapiHeaders.prototype.addSubVal = function(key, val) { - if (_.isArray(val)) { - this.sub[key] = val; - } else { - this.sub[key] = [val]; - } -}; - -SmtpapiHeaders.prototype.setUniqueArgs = function(val) { - if (_.isObject(val)) { - this.unique_args = val; - } -}; - -SmtpapiHeaders.prototype.addUniqueArgs = function(val) { - if (_.isObject(val)) { - _.extend(this.unique_args, val); - } -}; - -SmtpapiHeaders.prototype.setCategory = function(val) { - if (_.isArray(val)) { - this.category = val; - } else { - this.category = [val]; - } -}; - -SmtpapiHeaders.prototype.addCategory = function(val) { - if (_.isArray(val)) { - this.category.concat(val); - } else { - this.category.push(val); - } -}; - -SmtpapiHeaders.prototype.setSection = function(val){ - if (_.isObject(val)) { - this.section = val; - } -}; - -SmtpapiHeaders.prototype.addSection = function(val){ - if (_.isObject(val)) { - _.extend(this.section, val); - } -}; - -SmtpapiHeaders.prototype.setFilterSetting = function(filters) { - this.filters = filters; -}; - -SmtpapiHeaders.prototype.addFilterSetting = function(filter, setting, val) { - if (!this.filters[filter]) { - this.filters[filter] = {}; - } - - if (!this.filters[filter]['settings']) { - this.filters[filter]['settings'] = {}; - } - - this.filters[filter]['settings'][setting] = val; -}; - -SmtpapiHeaders.prototype.toJson = function() { - var data = _.clone(this); - _.each(data, function(v, k, list) { - if (_.isEmpty(v)) { - delete list[k]; - } - }); - var json = JSON.stringify(data); - return json.replace(/(["\]}])([,:])(["\[{])/, '$1$2 $3'); -}; - -module.exports = SmtpapiHeaders; diff --git a/npm-shrinkwrap.json b/npm-shrinkwrap.json index bf84dcd..1390b41 100644 --- a/npm-shrinkwrap.json +++ b/npm-shrinkwrap.json @@ -4,13 +4,11 @@ "dependencies": { "mime": { "version": "1.2.9", - "from": "mime@1.2.9", - "resolved": "https://registry.npmjs.org/mime/-/mime-1.2.9.tgz" + "from": "mime@1.2.9" }, "request": { "version": "2.22.0", "from": "request@2.22.0", - "resolved": "https://registry.npmjs.org/request/-/request-2.22.0.tgz", "dependencies": { "qs": { "version": "0.6.6", @@ -34,18 +32,15 @@ "dependencies": { "assert-plus": { "version": "0.1.2", - "from": "assert-plus@0.1.2", - "resolved": "https://registry.npmjs.org/assert-plus/-/assert-plus-0.1.2.tgz" + "from": "assert-plus@0.1.2" }, "asn1": { "version": "0.1.11", - "from": "asn1@0.1.11", - "resolved": "https://registry.npmjs.org/asn1/-/asn1-0.1.11.tgz" + "from": "asn1@0.1.11" }, "ctype": { "version": "0.5.2", - "from": "ctype@0.5.2", - "resolved": "https://registry.npmjs.org/ctype/-/ctype-0.5.2.tgz" + "from": "ctype@0.5.2" } } }, @@ -102,7 +97,6 @@ "form-data": { "version": "0.0.8", "from": "form-data@0.0.8", - "resolved": "https://registry.npmjs.org/form-data/-/form-data-0.0.8.tgz", "dependencies": { "combined-stream": { "version": "0.0.4", @@ -110,8 +104,7 @@ "dependencies": { "delayed-stream": { "version": "0.0.5", - "from": "delayed-stream@0.0.5", - "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-0.0.5.tgz" + "from": "delayed-stream@0.0.5" } } }, @@ -125,8 +118,18 @@ }, "lodash": { "version": "2.0.0", - "from": "lodash@2.0.0", - "resolved": "https://registry.npmjs.org/lodash/-/lodash-2.0.0.tgz" + "from": "lodash@2.0.0" + }, + "xsmtpapi": { + "version": "0.0.1", + "from": "xsmtpapi@0.0.1", + "resolved": "https://registry.npmjs.org/xsmtpapi/-/xsmtpapi-0.0.1.tgz", + "dependencies": { + "lodash": { + "version": "2.4.1", + "from": "lodash@2.4.1" + } + } } } } diff --git a/package.json b/package.json index d3f5fc1..db6b221 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,8 @@ "dependencies": { "mime": "1.2.9", "request": "2.22.0", - "lodash": "2.0.0" + "lodash": "2.0.0", + "xsmtpapi": "0.0.1" }, "devDependencies": { "dotenv": "0.0.2", diff --git a/test/lib/sendgrid.test.js b/test/lib/sendgrid.test.js index bd8c2a1..6061683 100644 --- a/test/lib/sendgrid.test.js +++ b/test/lib/sendgrid.test.js @@ -14,10 +14,6 @@ describe('SendGrid', function () { expect(sendgrid.Email).to.not.be.undefined; }); - it('should export the SmtpapiHeaders object', function() { - expect(sendgrid.SmtpapiHeaders).to.not.be.undefined; - }); - it('version should be set', function() { expect(sendgrid.version).to.equal("1.0.0"); }); @@ -32,18 +28,6 @@ describe('SendGrid', function () { expect(sendgrid2.options.web.pool).to.equal(http.globalAgent); }); - it('port should be set and settable', function() { - expect(sendgrid.port).to.equal(587); - sendgrid.port = 465; - expect(sendgrid.port).to.equal(465); - }); - - it('options.port should be set and settable', function() { - expect(sendgrid.options.port).to.equal(587); - var sendgrid2 = require('../../lib/sendgrid')('username', 'password', {api: 'smtp', port: 465}); - expect(sendgrid2.options.port).to.equal(465); - }); - describe('#send', function() { var payload, mock, webApi, postParams, postParamsString; diff --git a/test/lib/smtpapi_header.test.js b/test/lib/smtpapi_header.test.js deleted file mode 100644 index a72597f..0000000 --- a/test/lib/smtpapi_header.test.js +++ /dev/null @@ -1,212 +0,0 @@ -var SmtpapiHeaders = require('../../lib/smtpapi_headers'); -var _ = require('lodash'); - -describe('SmtpapiHeader', function() { - var header; - beforeEach(function() { - header = new SmtpapiHeaders(); - }); - - it('should allow multiple to addresses', function() { - header.addTo('kyle.partridge@sendgrid.com'); - expect(header.to).to.eql(['kyle.partridge@sendgrid.com']); - header.addTo('david.tomberlin@sendgrid.com'); - expect(header.to).to.eql(['kyle.partridge@sendgrid.com', 'david.tomberlin@sendgrid.com']); - }); - - describe('sub', function() { - var sub = {}; - - it('should allow adding a single sub value', function() { - header.addSubVal('keep', 'secret'); - expect(header.sub).to.eql({keep: ['secret']}); - }); - - it('should override a value if using the same key', function() { - header.addSubVal('keep', 'secret'); - header.addSubVal('keep', 'two'); - expect(header.sub).to.eql({keep: ['two']}); - }); - - it('should allow adding multiple sub values if different keys', function() { - header.addSubVal('keep', 'secret'); - header.addSubVal('other', ['one', 'two']); - expect(header.sub).to.eql({keep: ['secret'], other: ['one', 'two']}); - }); - }); - - describe('unique_args', function() { - var unique_args = { foo: 'bar', apple: 'sauce' }; - it('should allow setting unique args', function() { - header.setUniqueArgs(unique_args); - expect(header.unique_args).to.eql(unique_args); - }); - - it('should allow adding unique_args one at a time', function() { - for (var key in unique_args) { - var args = {}; - args[key] = unique_args[key]; - header.addUniqueArgs(args); - } - expect(header.unique_args).to.eql(unique_args); - header.addUniqueArgs({secret_test: 'rawr'}); - expect(header.unique_args.secret_test).to.eql('rawr'); - }); - - it('should overwrite when calling addUniqueArgs with a value that already exists', function() { - header.addUniqueArgs(unique_args); - expect(header.unique_args).to.eql(unique_args); - header.addUniqueArgs({apple: 'pie'}); - expect(header.unique_args).not.to.eql(unique_args); - expect(header.unique_args.apple).to.eql('pie'); - }); - }); - - describe('categories', function() { - var categories = ['azure', 'dreams']; - - it('should allow setting a single category', function() { - header.setCategory(categories[0]); - expect(header.category).to.eql([categories[0]]); - }); - - it('should allow adding categories one at a time', function() { - header.addCategory(categories[0]); - header.addCategory(categories[1]); - expect(header.category).to.eql(categories); - }); - - it('should allow setting categories', function() { - var categories = ['azure', 'dreams']; - header.setCategory(categories); - expect(header.category).to.eql(categories); - }); - }); - - describe('sections', function() { - var section_args = {'-sectionName1-': 'text 1', '-sectionName2-': 'text 2'}; - it('should allow setting a single section', function() { - header.setSection(section_args); - expect(header.section).to.eql(section_args); - }); - - it('should allow adding section args one at a time', function() { - for (var key in section_args) { - var args = {}; - args[key] = section_args[key]; - header.addSection(args); - } - expect(header.section).to.eql(section_args); - header.addSection({cow: 'moo'}); - expect(header.section).not.to.eql(section_args); - expect(header.section.cow).to.eql('moo'); - }); - - it('should overwrite section when calling addSection with a value that already exists', function() { - header.addSection(section_args); - expect(header.section).to.eql(section_args); - header.addSection({'-sectionName1-': 'cows are nice'}); - expect(header.section).to.not.eql(section_args); - expect(header.section['-sectionName1-']).to.eql('cows are nice'); - }); - }); - - describe('filters', function() { - it('should be able to set basic filters', function() { - header.addFilterSetting('footer', 'enable', 1); - header.addFilterSetting('footer', 'text/html', '<b>boo</b>'); - expect(header.filters).to.eql({ - 'footer': { - 'settings': { - 'enable': 1, - 'text/html': '<b>boo</b>' - } - } - }); - }); - - it('should accept filter settings as a straight up object literal', function() { - var filters = { - 'footer': { - 'setting': { - 'enable': 1, - 'text/plain': 'You can haz footers!' - } - } - } - header.setFilterSetting(filters); - expect(header.filters).to.eql(filters); - }); - }); - - describe('json', function() { - it('should produce valid json', function() { - header.addTo('kyle.partridge@sendgrid.com'); - header.addTo(['david.tomberlin@sendgrid.com']); - header.addUniqueArgs({foo: 'bar'}); - header.addFilterSetting('footer', 'enable', 1); - header.addFilterSetting('footer', 'text/html', '<b>boo</b>'); - - var parse = JSON.parse(header.toJson()); - expect(parse.to).to.eql(header.to); - expect(parse.unique_args).to.eql(header.unique_args); - expect(parse.filters).to.eql(header.filters); - }); - - it('should not include the "to" parameter when there are none', function() { - header.setCategory('nothing'); - var json = header.toJson(); - expect(_.isUndefined(JSON.parse(json).to)).to.be.true; - }); - - it('should not include the "sub" parameter when there are none', function() { - header.setCategory('nothing'); - var json = header.toJson(); - - expect(_.isUndefined(JSON.parse(json).sub)).to.be.true; - }); - - it('should not include the "unique_args" parameter when there are none', function() { - header.setCategory('nothing'); - var json = header.toJson(); - - expect(_.isUndefined(JSON.parse(json).unique_args)).to.be.true; - }); - - it('should not include the "category" parameter when there are none', function() { - header.addUniqueArgs({food: 'bar'}); - var json = header.toJson(); - - expect(_.isUndefined(JSON.parse(json).category)).to.be.true; - }); - - it('should not include the "filters" parameter when there are none', function() { - header.addUniqueArgs({food: 'bar'}); - var json = header.toJson(); - - expect(_.isUndefined(JSON.parse(json).filters)).to.be.true; - }); - - it('should not remove any parameters on this object', function() { - header.addTo('kyle.partridge@sendgrid.com'); - header.addTo(['david.tomberlin@sendgrid.com']); - header.addUniqueArgs({foo: 'bar'}); - header.addFilterSetting('footer', 'enable', 1); - header.addFilterSetting('footer', 'text/html', '<b>boo</b>'); - - // call the json method to test if it removed anything - header.toJson(); - - expect(header.to).to.eql(['kyle.partridge@sendgrid.com', 'david.tomberlin@sendgrid.com']); - expect(header.unique_args).to.eql({foo: 'bar'}); - expect(header.filters).to.eql({ - footer: { - settings: { - enable: 1, - 'text/html': '<b>boo</b>' - } - } - }); - }); - }); -}); |