drip-nodejs
Advanced tools
Comparing version 0.1.0 to 0.2.0
@@ -82,2 +82,3 @@ var Accounts = require('./accounts') | ||
Client.prototype.deleteSubscriber = Subscribers.deleteSubscriber; | ||
Client.prototype.updateBatchSubscribers = Subscribers.updateBatchSubscribers; | ||
@@ -84,0 +85,0 @@ // Tag methods |
@@ -75,5 +75,47 @@ 'use strict'; | ||
) | ||
}, | ||
updateBatchSubscribers: function (accountId, payload, callback) { | ||
var subscribers = payload && payload.batches && payload.batches[0].subscribers || []; | ||
var batchSize = 1000; | ||
var batches = []; | ||
var errors = [], responses = [], bodies = []; | ||
var headers = this.headers; | ||
var done = 0; | ||
var hasError = false; | ||
// Break the payload into batch-sized chunks | ||
for(var i = 0, j = subscribers.length; i < j; i += batchSize) { | ||
batches.push(subscribers.slice(i, batchSize)); | ||
} | ||
batches.forEach(function(batch, batchIndex) { | ||
request.post( | ||
{ | ||
url: helper.subscribersUrl(accountId) + "batches", | ||
headers: headers, | ||
json: true, | ||
body: { | ||
batches: [{ | ||
subscribers: batch | ||
}] | ||
} | ||
}, | ||
function(error, response, body) { | ||
errors[batchIndex] = error; | ||
responses[batchIndex] = response; | ||
bodies[batchIndex] = body; | ||
hasError = hasError || error; | ||
done += 1; | ||
if(done === batches.length) { | ||
// All batches complete; call back | ||
callback( | ||
hasError ? errors : null, | ||
responses, | ||
bodies); | ||
} | ||
} | ||
) | ||
}) | ||
} | ||
} | ||
// Handle batch subscriber updates |
{ | ||
"name": "drip-nodejs", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "A complete NodeJS wrapper for connecting to the Drip v2 REST API", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
@@ -93,3 +93,4 @@ [![Build Status](https://travis-ci.org/samudary/drip-nodejs.svg?branch=master)](https://travis-ci.org/samudary/drip-nodejs) | ||
| Unsubscribe from all mailings | `client.unsubscribeFromAllMailings(accountId, emailOrId, callback)` | | ||
| Delete s subscriber | `client.deleteSubscriber(accountId, emailOrId, callback)` | | ||
| Delete a subscriber | `client.deleteSubscriber(accountId, emailOrId, callback)` | | ||
| Update a batch of subscribers | `client.updateBatchSubscribers(accountId, payload, callback)` | | ||
@@ -153,2 +154,33 @@ ### Tags | ||
### Updating a batch of subscribers | ||
The `updateBatchSubscribers` method takes a batch object for the payload and is most suitable for sending thousands of subscriber updates. | ||
Because Drip's batch APIs support a maximum of 1000 records, this method breaks the payload into *N* "batches" and calls the API *N* times. The callback is invoked only after all batches' API calls have returned, and receives *N*-sized arrays for values (i.e. `errors`, `responses`, and `bodies`). | ||
It is the responsibility of the caller to interpret these values and handle any errors. | ||
```javascript | ||
var batch = { | ||
"batches": [{ | ||
"subscribers": [ | ||
{ | ||
"email": "john@acme.com", | ||
"tags": "Dog Person" | ||
}, | ||
{ | ||
"email": "joe@acme.com", | ||
"tags": "Cat Person" | ||
} | ||
// Lots more subscribers... | ||
] | ||
}] | ||
} | ||
client.updateBatchSubscribers(2271521, batch, function (errors, responses, bodies) { | ||
// Do stuff | ||
} | ||
) | ||
``` | ||
### Sending a batch of events | ||
@@ -155,0 +187,0 @@ |
@@ -13,84 +13,118 @@ 'use strict'; | ||
beforeEach(function () { | ||
sinon.stub(request, 'get') | ||
.yields(null, { statusCode: 200 }, { accounts : {} } | ||
); | ||
sinon.stub(request, 'post') | ||
.yields(null, { statusCode: 204 }, {} | ||
); | ||
sinon.stub(request, 'del') | ||
.yields(null, { statusCode: 204 }, {} | ||
); | ||
}); | ||
describe('non-batch functions', function() { | ||
beforeEach(function () { | ||
sinon.stub(request, 'get') | ||
.yields(null, { statusCode: 200 }, { accounts : {} } | ||
); | ||
sinon.stub(request, 'post') | ||
.yields(null, { statusCode: 204 }, {} | ||
); | ||
sinon.stub(request, 'del') | ||
.yields(null, { statusCode: 204 }, {} | ||
); | ||
}); | ||
afterEach(function () { | ||
request.get.restore(); | ||
request.post.restore(); | ||
request.del.restore(); | ||
}); | ||
afterEach(function () { | ||
request.get.restore(); | ||
request.post.restore(); | ||
request.del.restore(); | ||
}); | ||
it('should provide the correct base URL', function () { | ||
expect(helper.subscribersUrl(accountId)) | ||
.toBe('https://api.getdrip.com/v2/123/subscribers/') | ||
}) | ||
it('should provide the correct base URL', function () { | ||
expect(helper.subscribersUrl(accountId)) | ||
.toBe('https://api.getdrip.com/v2/123/subscribers/') | ||
}) | ||
it('should list all subscribers and call request with get', function (done) { | ||
expect(typeof client.listSubscribers).toEqual('function'); | ||
it('should list all subscribers and call request with get', function (done) { | ||
expect(typeof client.listSubscribers).toEqual('function'); | ||
client.listSubscribers(accountId, function(error, response, body) { | ||
expect(response.statusCode).toBe(200); | ||
expect(request.get.callCount).toBe(1); | ||
client.listSubscribers(accountId, function(error, response, body) { | ||
expect(response.statusCode).toBe(200); | ||
expect(request.get.callCount).toBe(1); | ||
}); | ||
done(); | ||
}); | ||
done(); | ||
}); | ||
it('should update a subscriber and call request with post', function (done) { | ||
expect(typeof client.updateSubscriber).toEqual('function'); | ||
it('should update a subscriber and call request with post', function (done) { | ||
expect(typeof client.updateSubscriber).toEqual('function'); | ||
client.updateSubscriber(accountId, { "test_field": "value" }, function (error, response, body) { | ||
expect(response.statusCode).toBe(204); | ||
expect(request.post.callCount).toBe(1); | ||
client.updateSubscriber(accountId, { "test_field": "value" }, function (error, response, body) { | ||
expect(response.statusCode).toBe(204); | ||
expect(request.post.callCount).toBe(1); | ||
}); | ||
done(); | ||
}); | ||
done(); | ||
}); | ||
it('should fetch a specific subscriber and call request with get', function (done) { | ||
expect(typeof client.fetchSubscriber).toEqual('function'); | ||
it('should fetch a specific subscriber and call request with get', function (done) { | ||
expect(typeof client.fetchSubscriber).toEqual('function'); | ||
client.fetchSubscriber(accountId, email, function (error, response, body) { | ||
expect(response.statusCode).toBe(200); | ||
expect(request.get.callCount).toBe(1); | ||
client.fetchSubscriber(accountId, email, function (error, response, body) { | ||
expect(response.statusCode).toBe(200); | ||
expect(request.get.callCount).toBe(1); | ||
}); | ||
done(); | ||
}); | ||
done(); | ||
}); | ||
it('should unsubscribe someone from a campaign and call request with post', function (done) { | ||
expect(typeof client.unsubscribeFromCampaign).toEqual('function'); | ||
it('should unsubscribe someone from a campaign and call request with post', function (done) { | ||
expect(typeof client.unsubscribeFromCampaign).toEqual('function'); | ||
client.unsubscribeFromCampaign(accountId, email, campaignId, function (error, response, body) { | ||
expect(response.statusCode).toBe(204); | ||
expect(request.post.callCount).toBe(1); | ||
client.unsubscribeFromCampaign(accountId, email, campaignId, function (error, response, body) { | ||
expect(response.statusCode).toBe(204); | ||
expect(request.post.callCount).toBe(1); | ||
}); | ||
done(); | ||
}); | ||
done(); | ||
}); | ||
it('should unsubscribe someone from all mailings and call request with post', function (done) { | ||
expect(typeof client.unsubscribeFromAllMailings).toEqual('function'); | ||
it('should unsubscribe someone from all mailings and call request with post', function (done) { | ||
expect(typeof client.unsubscribeFromAllMailings).toEqual('function'); | ||
client.unsubscribeFromAllMailings(accountId, email, function (error, response, body) { | ||
expect(response.statusCode).toBe(204); | ||
expect(request.post.callCount).toBe(1); | ||
client.unsubscribeFromAllMailings(accountId, email, function (error, response, body) { | ||
expect(response.statusCode).toBe(204); | ||
expect(request.post.callCount).toBe(1); | ||
}); | ||
done(); | ||
}); | ||
done(); | ||
it('should delete a subscriber and call request with delete', function (done) { | ||
expect(typeof client.deleteSubscriber).toEqual('function'); | ||
client.deleteSubscriber(accountId, email, function (error, response, body) { | ||
expect(response.statusCode).toBe(204); | ||
expect(request.del.callCount).toBe(1); | ||
}); | ||
done(); | ||
}); | ||
}); | ||
it('should delete a subscriber and call request with delete', function (done) { | ||
expect(typeof client.deleteSubscriber).toEqual('function'); | ||
describe('batch functions', function() { | ||
var payload = { | ||
batches: [{ | ||
subscribers: new Array(1001) | ||
}] | ||
}; | ||
client.deleteSubscriber(accountId, email, function (error, response, body) { | ||
expect(response.statusCode).toBe(204); | ||
expect(request.del.callCount).toBe(1); | ||
beforeEach(function () { | ||
sinon.stub(request, 'post') | ||
.yields(null, { statusCode: 201 }, {} | ||
); | ||
}); | ||
done(); | ||
afterEach(function () { | ||
request.post.restore(); | ||
}); | ||
it('should post batches of subscribers and call request with post', function (done) { | ||
expect(typeof client.updateBatchSubscribers).toEqual('function'); | ||
client.updateBatchSubscribers(accountId, payload, function (errors, responses, bodies) { | ||
expect(errors).toBe(null); | ||
expect(responses.length).toBe(2); | ||
expect(responses[0].statusCode).toBe(201); | ||
expect(responses[1].statusCode).toBe(201); | ||
expect(bodies).toEqual([{}, {}]); | ||
expect(request.post.callCount).toBe(2); | ||
}); | ||
done(); | ||
}); | ||
}); | ||
}); |
72145
1598
220