Skip to content

Commit

Permalink
Merge pull request #85 from aydrian/ISSUE-50
Browse files Browse the repository at this point in the history
Added getDocumentation and getSamples functions to Webhooks resource.
  • Loading branch information
aydrian committed Aug 6, 2015
2 parents 9874d3f + d8d6789 commit beb21ce
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 0 deletions.
7 changes: 7 additions & 0 deletions docs/resources/webhooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ This library provides easy access to the [Webhooks](https://www.sparkpost.com/ap
* `options.id` - the id of the webhook you want to get status on **required**
* `options.limit` - `number` maximum number of results to return Default: `1000`
* `callback` - see all function
* **getDocumentation(callback)**
Lists descriptions of the events, event types, and event fields that could be included in a Webhooks post to your target URL.
* `callback` - see all function
* **getSamples(options, callback)**
List an example of the event data that will be posted by a Webhook for the specified events.
* `options.events` - `String` event types for which to get a sample payload Defaults to all event types
* `callback` - see all function

## Examples

Expand Down
14 changes: 14 additions & 0 deletions examples/webhooks/getDocumentation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

var key = 'YOURAPIKEY'
, SparkPost = require('sparkpost')
, client = new SparkPost(key);

client.webhooks.getDocumentation(function(err, res) {
if (err) {
console.log(err);
} else {
console.log(res.body);
console.log('Congrats you can use our SDK!');
}
});
17 changes: 17 additions & 0 deletions examples/webhooks/getSamples.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

var key = 'YOURAPIKEY'
, SparkPost = require('sparkpost')
, client = new SparkPost(key)
, options = {
events: 'bounce'
};

client.webhooks.getSamples(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/webhooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,29 @@ module.exports = function(client) {
reqOpts.qs.limit = options.limit;
}

client.get(reqOpts, callback);
},
getDocumentation: function(callback) {
var reqOpts = {
uri: api + '/events/documentation'
};
client.get(reqOpts, callback);
},
getSamples: function(options, callback){
var reqOpts = {
uri: api + '/events/samples'
};

if (typeof options === 'function') {
callback = options;
options = {};
}

if (options.events) {
reqOpts.qs = reqOpts.qs || {};
reqOpts.qs.events = options.events;
}

client.get(reqOpts, callback);
}
};
Expand Down
37 changes: 37 additions & 0 deletions test/spec/webhooks.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,41 @@ describe('Webhooks Library', function() {
});
});
});

describe('getDocumentation Method', function() {
it('should call client get method with the appropriate uri', function(done) {
var options = {
id: 'test'
};

webhooks.getDocumentation(function(err, data) {
expect(client.get.firstCall.args[0].uri).to.equal('webhooks/events/documentation');
done();
});
});
});

describe('getSamples Method', function() {
it('should call client get method with the appropriate uri', function(done) {
var options = {
id: 'test'
};

webhooks.getSamples(function(err, data) {
expect(client.get.firstCall.args[0].uri).to.equal('webhooks/events/samples');
done();
});
});

it('should allow events to be set in options', function(done) {
var options = {
events: 'bounces'
};

webhooks.getSamples(options, function(err, data) {
expect(client.get.firstCall.args[0].qs).to.deep.equal({events: 'bounces'});
done();
});
});
});
});

0 comments on commit beb21ce

Please sign in to comment.