Skip to content

Commit 4e1cf4d

Browse files
author
Tim Noel
committed
Added comments, replaced mergeObjects with lodash function
1 parent 99f5cef commit 4e1cf4d

File tree

2 files changed

+69
-13
lines changed

2 files changed

+69
-13
lines changed

lib/SendGridCompatibility/Email.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
'use strict';
22

3+
/**
4+
* Email object constructor
5+
*
6+
* @param options object that contains initial values
7+
*/
38
function Email(options){
49
for (var option in options) {
510
this[option] = options[option];
611
}
712
}
813

14+
/**
15+
* Sendgrid email compatibility functions
16+
*
17+
* @param Most have a single value to map
18+
* Add functions will often contain a key / value pair
19+
*/
920
Email.prototype.addTo = function (address){
1021
if (this.to === undefined){
1122
this.to = address;
@@ -59,24 +70,31 @@ Email.prototype.addSection = function (key, value){
5970
Email.prototype.setSections = function (sections){
6071
this.section = sections;
6172
};
73+
// Sparkpost doesn't currently support addUniqueArg, throw an error
6274
Email.prototype.addUniqueArg = function (){
6375
throw new Error('Unique Argument compatibility is not supported.');
6476
};
77+
// Sparkpost doesn't currently support setUniqueArgs, throw an error
6578
Email.prototype.setUniqueArgs = function (){
6679
throw new Error('Unique Argument compatibility is not supported.');
6780
};
81+
// Sparkpost doesn't currently support addCategory, throw an error
6882
Email.prototype.addCategory = function (){
6983
throw new Error('Category compatibility is not supported.');
7084
};
85+
// Sparkpost doesn't currently support setCategories, throw an error
7186
Email.prototype.setCategories = function (){
7287
throw new Error('Category compatibility is not supported.');
7388
};
89+
// Sparkpost doesn't currently support addFilter, throw an error
7490
Email.prototype.addFilter = function (){
7591
throw new Error('Filter compatibility is not supported.');
7692
};
93+
// Sparkpost doesn't currently support setFilters, throw an error
7794
Email.prototype.setFilters = function (){
7895
throw new Error('Filter compatibility is not supported.');
7996
};
97+
// Sparkpost doesn't currently support addFile, throw an error
8098
Email.prototype.addFile = function (){
8199
throw new Error('File compatibility is not supported.');
82100
};

lib/SendGridCompatibility/index.js

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
'use strict';
2-
2+
var _ = require('lodash');
33
var transmission = require('../transmission');
44
var configuration = require('../configuration');
55

6+
/**
7+
* Sendgrid compatibility constructor
8+
* Responsible for taking the api key and config options and
9+
* translating them into a format compatible with sparkpost's
10+
* configuration.setConfig function.
11+
*
12+
* @param username: dropped sendgrid username string
13+
* @param apiKey: api key string
14+
* @param options: optional additional options object
15+
*/
616
var sendgrid = function(username, apiKey, options) {
717
var configOptions = options;
818
if (configOptions === undefined) {
@@ -14,6 +24,13 @@ var sendgrid = function(username, apiKey, options) {
1424
configuration.setConfig(configOptions);
1525
};
1626

27+
/**
28+
* Private Method for translating a user's sendgrid config options
29+
* to a sparkpost compatible set by dropping unnecessary data
30+
*
31+
* @param payload: sendgrid formatted config options object
32+
* @returns object: config options with incompatible entries removed
33+
*/
1734
var translateConfig = function(options) {
1835
var translated = {};
1936
if (options.host !== undefined) {
@@ -28,21 +45,18 @@ var translateConfig = function(options) {
2845
return translated;
2946
};
3047

31-
var mergeObjects = function(obj1, obj2){
32-
var merged = {};
33-
for (var key1 in obj1) {
34-
merged[key1] = obj1[key1];
35-
}
36-
for (var key2 in obj2) {
37-
merged[key2] = obj2[key2];
38-
}
39-
return merged;
40-
};
41-
48+
/**
49+
* Private Method for translating a user's sendgrid substitutions and sections
50+
* to a consolidated sparkpost substitutionData object
51+
*
52+
* @param payload: sendgrid formatted object or sendgrid Email object to
53+
* be translated into a sparkpost payload
54+
* @returns object: substitutionData object as per sparkpost payload format
55+
*/
4256
var consolidateSubstitutionData = function(payload) {
4357
var substitutionData = {};
4458
if (payload.sub !== undefined && payload.section !== undefined){
45-
substitutionData = mergeObjects(payload.sub, payload.section);
59+
substitutionData = _.merge(payload.sub, payload.section);
4660
} else if (payload.sub !== undefined){
4761
substitutionData = payload.sub;
4862
} else if (payload.section !== undefined){
@@ -51,6 +65,14 @@ var consolidateSubstitutionData = function(payload) {
5165
return substitutionData;
5266
};
5367

68+
/**
69+
* Private Method for translating a user's sendgrid to and toname to a recipients
70+
* array that the transmissions "Class" can understand
71+
*
72+
* @param payload: sendgrid formatted object or sendgrid Email object to
73+
* be translated into a sparkpost payload
74+
* @returns array: recipients array as per sparkpost payload format
75+
*/
5476
var parseTo = function(payload){
5577
var recipients = [];
5678
if (typeof payload.to === 'string'){
@@ -70,6 +92,14 @@ var parseTo = function(payload){
7092
return recipients;
7193
};
7294

95+
/**
96+
* Private Method for translating a user's sendgrid payload to a format
97+
* that the transmissions "Class" can understand
98+
*
99+
* @param payload: sendgrid formatted object or sendgrid Email object to
100+
* be translated into a sparkpost payload
101+
* @returns object: translation from sendgrid payload to sparkpost payload
102+
*/
73103
var translatePayload = function(payload) {
74104
var sub = consolidateSubstitutionData(payload)
75105
, input = {
@@ -97,6 +127,14 @@ var translatePayload = function(payload) {
97127
return input;
98128
};
99129

130+
/**
131+
* An exposed function of SendGridCompatibility that calls upon
132+
* private helper methods to translate an Email or inline object
133+
* and then pass that content to the transmissions send function.
134+
*
135+
* @param payload: sendgrid formatted object or sendgrid Email object to
136+
* be translated into a sparkpost payload and sent
137+
*/
100138
sendgrid.prototype.send = function(payload, callback) {
101139
var translated = translatePayload(payload);
102140
transmission.send(translated, function (err, res){

0 commit comments

Comments
 (0)