From ec18ec63a418dc5f22f3b864942c82f0e3542083 Mon Sep 17 00:00:00 2001 From: "Aydrian J. Howard" Date: Thu, 6 Aug 2015 12:28:13 -0400 Subject: [PATCH] Added Recipient List Update method, Docs, and Examples --- docs/resources/recipientLists.md | 6 +++ .../recipientLists/update_recipientList.js | 35 +++++++++++++++ lib/recipientLists.js | 23 ++++++++++ test/spec/recipientLists.spec.js | 45 +++++++++++++++++++ 4 files changed, 109 insertions(+) create mode 100644 examples/recipientLists/update_recipientList.js diff --git a/docs/resources/recipientLists.md b/docs/resources/recipientLists.md index 92d5a3d..5fa4ca6 100644 --- a/docs/resources/recipientLists.md +++ b/docs/resources/recipientLists.md @@ -19,6 +19,12 @@ This library provides easy access to the [Recipient Lists](https://www.sparkpost * `options.recipients` - an array of recipients to add to the list **required** * `options.num_rcpt_errors` - limit the number of recipient errors returned * `callback` - see all function +* **update(options, callback)** + Update an existing recipient list + * `options.id` - the id of the recipient list you want to update **required** + * `options.recipients` - an array of recipients to add to the list **required** + * `options.num_rcpt_errors` - limit the number of recipient errors returned + * `callback` - see all function * **delete(id, callback)** Delete an existing recipient list * `id` - the id of the recipient list you want to delete **required** diff --git a/examples/recipientLists/update_recipientList.js b/examples/recipientLists/update_recipientList.js new file mode 100644 index 0000000..9e4087e --- /dev/null +++ b/examples/recipientLists/update_recipientList.js @@ -0,0 +1,35 @@ +'use strict'; + +var key = 'YOURAPIKEY' + , SparkPost = require('sparkpost') + , client = new SparkPost(key) + , options = { + id: 'EXISTING_TEST_ID' + , name: 'Test Recipient List' + , recipients: [ + { + address: { + email: 'test1@test.com' + } + } + , { + address: { + email: 'test2@test.com' + } + } + , { + address: { + email: 'test3@test.com' + } + } + ] + }; + +client.recipientLists.update(options, function(err, res) { + if (err) { + console.log(err); + } else { + console.log(res.body); + console.log('Congrats you can use our SDK!'); + } +}); diff --git a/lib/recipientLists.js b/lib/recipientLists.js index 0e656ac..cfa80e4 100644 --- a/lib/recipientLists.js +++ b/lib/recipientLists.js @@ -37,6 +37,7 @@ module.exports = function(client) { callback(new Error('recipients list is required')); return; } + var reqOpts = { uri: api }; @@ -50,6 +51,28 @@ module.exports = function(client) { reqOpts.json = toApiFormat(options); client.post(reqOpts, callback); + }, + update: function(options, callback) { + options = options || {}; + + if(!options.id) { + callback(new Error('recipients list id is required')); + return; + } + + var reqOpts = { + uri: api + '/' + options.id + }; + + if (options.num_rcpt_errors) { + reqOpts.qs = reqOpts.qs || {}; + reqOpts.qs.num_rcpt_errors = options.num_rcpt_errors; + delete options.num_rcpt_errors; + } + + reqOpts.json = toApiFormat(options); + + client.put(reqOpts, callback); } }; diff --git a/test/spec/recipientLists.spec.js b/test/spec/recipientLists.spec.js index 11fe5a7..670c2be 100644 --- a/test/spec/recipientLists.spec.js +++ b/test/spec/recipientLists.spec.js @@ -102,6 +102,51 @@ describe('Recipient Lists Library', function() { }); }); + describe('update Method', function() { + var test_list = [ + { + address: { + email: 'test@test.com', + name: 'test' + } + } + ]; + + it('should call client put method with the appropriate uri', function(done) { + var options = { + id: 'test_list', + recipients: test_list + }; + + recipientLists.update(options, function(err, data) { + expect(client.put.firstCall.args[0].uri).to.equal('recipient-lists/' + options.id); + done(); + }); + }); + + it('should throw an error if id is missing', function(done) { + recipientLists.update(null, function(err) { + expect(err.message).to.equal('recipients list id is required'); + expect(client.put).not.to.have.been.called; + done(); + }); + }); + + it('should allow num_rcpt_errors to be set in options', function(done) { + var options = { + id: 'test_list', + recipients: test_list, + num_rcpt_errors: 3 + }; + + recipientLists.update(options, function(err, data) { + expect(client.put.firstCall.args[0].qs).to.deep.equal({num_rcpt_errors: 3}); + done(); + }); + }); + + }); + describe('delete Method', function() { it('should call client delete method with the appropriate uri', function(done) { recipientLists.delete('test', function(err, data) {