Skip to content

Commit

Permalink
Merge pull request #91 from aydrian/ISSUE-86
Browse files Browse the repository at this point in the history
Added Recipient List Update method, Docs, and Examples
  • Loading branch information
aydrian committed Aug 6, 2015
2 parents beb21ce + ec18ec6 commit e97eb18
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 0 deletions.
6 changes: 6 additions & 0 deletions docs/resources/recipientLists.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**
Expand Down
35 changes: 35 additions & 0 deletions examples/recipientLists/update_recipientList.js
Original file line number Diff line number Diff line change
@@ -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: '[email protected]'
}
}
, {
address: {
email: '[email protected]'
}
}
, {
address: {
email: '[email protected]'
}
}
]
};

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!');
}
});
23 changes: 23 additions & 0 deletions lib/recipientLists.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ module.exports = function(client) {
callback(new Error('recipients list is required'));
return;
}

var reqOpts = {
uri: api
};
Expand All @@ -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);
}
};

Expand Down
45 changes: 45 additions & 0 deletions test/spec/recipientLists.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,51 @@ describe('Recipient Lists Library', function() {
});
});

describe('update Method', function() {
var test_list = [
{
address: {
email: '[email protected]',
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) {
Expand Down

0 comments on commit e97eb18

Please sign in to comment.