Skip to content

Commit d368d74

Browse files
author
Jessica Martin
committed
Added helper method for url construction and changed storedTemplate to template based on code review feedback
1 parent b75604e commit d368d74

File tree

4 files changed

+25
-15
lines changed

4 files changed

+25
-15
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,15 @@ sparkpost.transmission.send(trans, function(err, res) {
6161
| replyTo | no | Field for specifying the email address that should be used when a recipient hits the reply button | String |
6262
| subject | yes | Field for setting the subject line of a given transmission | String |
6363
| from | yes | Field for setting the from line of a given transmission | String or Object |
64-
| html | yes** | Field for setting the HTML content of a given transmission | String |
65-
| text | yes** | Field for setting the Plain Text content of a given transmission | String |
66-
| rfc822 | no** | Field for setting the RFC-822 encoded content of a given transmission | String |
67-
| storedTemplate | no** | Field for specifying the Template ID of a stored template to be used when sending a given transmission | String |
64+
| html | yes** | Field for setting the HTML content of a given transmission | String |
65+
| text | yes** | Field for setting the Plain Text content of a given transmission | String |
66+
| rfc822 | no** | Field for setting the RFC-822 encoded content of a given transmission | String |
67+
| template | no** | Field for specifying the Template ID of a stored template to be used when sending a given transmission | String |
6868
| customHeaders | no | Field for specifying additional headers to be applied to a given transmission (other than Subject, From, To, and Reply-To) | Object (Simple) |
69-
| recipients | yes** | Field for specifying who a given transmission should be sent to | Array of Objects |
70-
| recipientList | no** | Field for specifying a stored recipient list ID to be used for a given transmission | String |
69+
| recipients | yes** | Field for specifying who a given transmission should be sent to | Array of Objects |
70+
| recipientList | no** | Field for specifying a stored recipient list ID to be used for a given transmission | String |
7171

72-
** - If using inline content then html or text are required. If using RFC-822 Inline Content, then rfc822 is required. If using a stored recipient list, then recipientList is required. If using a stored template, then storedTemplate is required.
72+
** - If using inline content then html or text are required. If using RFC-822 Inline Content, then rfc822 is required. If using a stored recipient list, then recipientList is required. If using a stored template, then template is required.
7373

7474
## Tips and Tricks
7575
### General

examples/transmission/stored_recipients_stored_content.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ var trans = {
77
from: 'From Envelope <[email protected]>',
88
subject: 'Example Email for Stored List and Template',
99
recipientList: 'example-list',
10-
storedTemplate: 'my-template',
10+
template: 'my-template',
1111
recipients: [{ address: { email: '[email protected]' } }]
1212
};
1313

examples/transmission/stored_template_send.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ var key = 'YOURAPIKEY'
44
, sparkpost = require('sparkpost')({ key: key });
55

66
var trans = {
7-
storedTemplate: 'my-template',
7+
template: 'my-template',
88
from: 'From Envelope <[email protected]>',
99
subject: 'Example Email for Stored Template',
10-
recipients = [{ address: { email: '[email protected]' } }]
10+
recipients: [{ address: { email: '[email protected]' } }]
1111
};
1212

1313
sparkpost.transmission.send(trans, function(err, res) {

lib/transmission.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ var request = require('request')
1717
var toApiFormat = function(input) {
1818
var model = {
1919
content: {},
20-
options: {}
20+
options: {},
21+
recipients: {}
2122
};
2223

2324
model.description = input.description;
@@ -36,15 +37,24 @@ var toApiFormat = function(input) {
3637
model.content.html = input.html;
3738
model.content.text = input.text;
3839
model.content.email_rfc822 = input.rfc822;
39-
model.content.template_id = input.storedTemplate;
40+
model.content.template_id = input.template;
4041
model.content.headers = input.customHeaders;
4142

42-
model.recipients = input.recipients;
4343
model.recipients.list_id = input.recipientList;
44+
model.recipients = input.recipients;
4445

4546
return model;
4647
};
4748

49+
/**
50+
* Private Method for putting URL construction logic into one place
51+
*
52+
* @returns string Fully constructed URL for contacting the Transmissions API
53+
*/
54+
var constructURL = function () {
55+
return config.options.protocol + '://' + config.options.host + (config.options.port ? ':' + config.options.port: '') + '/api/' + config.options.version + '/transmissions';
56+
};
57+
4858
/**
4959
* Private Method for issuing GET request to Transmissions API
5060
*
@@ -59,7 +69,7 @@ var toApiFormat = function(input) {
5969
var fetch = function(transmissionID, callback) {
6070
var cb = callback
6171
, options = {
62-
url: config.options.protocol + '://' + config.options.host + (config.options.port ? ':' + config.options.port: '') + '/api/' + config.options.version + '/transmissions',
72+
url: constructURL(),
6373
headers: {
6474
authorization: config.options.key
6575
},
@@ -100,7 +110,7 @@ module.exports = {
100110
var mappedInput = toApiFormat(transmissionBody);
101111

102112
var options = {
103-
url: config.options.protocol + '://' + config.options.host + (config.options.port ? ':' + config.options.port: '') + '/api/' + config.options.version + '/transmissions',
113+
url: constructURL(),
104114
json: mappedInput,
105115
headers: {
106116
authorization: config.options.key

0 commit comments

Comments
 (0)