Skip to content

Commit

Permalink
add labels API
Browse files Browse the repository at this point in the history
  • Loading branch information
officert committed Jun 12, 2017
1 parent 4918eeb commit 40053ab
Show file tree
Hide file tree
Showing 6 changed files with 299 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/client/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const getEventsApi = require('./events');
const getLabelsApi = require('./labels');

class SiftScienceClient {
constructor(key) {
Expand All @@ -10,6 +11,10 @@ class SiftScienceClient {
get events() {
return getEventsApi(this._key);
}

get labels() {
return getLabelsApi(this._key);
}
}

module.exports = SiftScienceClient;
36 changes: 36 additions & 0 deletions lib/client/labels.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const Promise = require('bluebird');
const v204 = require('./v204');
const _ = require('lodash');

function getLabelsApi(key) {
return {
/**
* @param {Object} [data]
* https://siftscience.com/developers/docs/curl/labels-api/label-user
*/
createByUserId(userId, data = {}) {
if (!userId) return Promise.reject(new Error('userId is required'));

_.extend(data, {
$api_key: key
});

return v204.post(`/users/${userId}/labels`, data);
},
/**
* @param {Object} [data]
* https://siftscience.com/developers/docs/curl/labels-api/unlabel-user
*/
deleteByUserId(userId, data = {}) {
if (!userId) return Promise.reject(new Error('userId is required'));

_.extend(data, {
$api_key: key
});

return v204.delete(`/users/${userId}/labels`, data);
}
};
}

module.exports = getLabelsApi;
8 changes: 8 additions & 0 deletions lib/client/v204/httpClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ class HttpClient {
params
} : null);
}

delete(path, params) {
if (!path) return Promise.reject(new Error('path is required'));

return this._client.delete(path, params ? {
params
} : null);
}
}

module.exports = new HttpClient();
73 changes: 73 additions & 0 deletions tests/siftScience/client/labels/createByUserId-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
const should = require('should');
const sinon = require('sinon');

let siftScience;
let v204;
let sandbox;

before(() => {
siftScience = require('lib');
v204 = require('lib/client/v204');

sandbox = sinon.sandbox.create();
});

describe('siftScience', () => {
describe('client', () => {
describe('labels', () => {
describe('createByUserId', () => {
afterEach(() => {
sandbox.restore();
});

const key = '123';
let siftScienceClient;

before('create siftScienceClient', () => {
siftScienceClient = siftScience.init(key);
});

describe('when called without userId', () => {
let userId = null;

it('should reject with an error', () => {
return siftScienceClient.labels.createByUserId(userId)
.then(should.not.exist)
.catch(err => {
should.exist(err);
err.should.be.instanceOf(Error);
err.message.should.equal('userId is required');
});
});
});

describe('when called with a userId', () => {
let userId = '123';
let params = {
foo: 'bar'
};
let expectedResponse = {};

let postStub;

before('stub v204._client.post()', () => {
postStub = sandbox.stub(v204._client, 'post')
.returns(Promise.resolve(expectedResponse));
});

it('should call v204._client.post()', () => {
return siftScienceClient.labels.createByUserId(userId, params)
.then(response => {
should.exist(response);
response.should.deepEqual(expectedResponse);

postStub.callCount.should.equal(1);
postStub.args[0][0].should.equal(`/users/${userId}/labels`);
postStub.args[0][1].should.deepEqual(params);
});
});
});
});
});
});
});
75 changes: 75 additions & 0 deletions tests/siftScience/client/labels/deleteByUserId-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
const should = require('should');
const sinon = require('sinon');

let siftScience;
let v204;
let sandbox;

before(() => {
siftScience = require('lib');
v204 = require('lib/client/v204');

sandbox = sinon.sandbox.create();
});

describe('siftScience', () => {
describe('client', () => {
describe('labels', () => {
describe('deleteByUserId', () => {
afterEach(() => {
sandbox.restore();
});

const key = '123';
let siftScienceClient;

before('create siftScienceClient', () => {
siftScienceClient = siftScience.init(key);
});

describe('when called without userId', () => {
let userId = null;

it('should reject with an error', () => {
return siftScienceClient.labels.deleteByUserId(userId)
.then(should.not.exist)
.catch(err => {
should.exist(err);
err.should.be.instanceOf(Error);
err.message.should.equal('userId is required');
});
});
});

describe('when called with a userId', () => {
let userId = '123';
let params = {
foo: 'bar'
};
let expectedResponse = {};

let deleteStub;

before('stub v204._client.delete()', () => {
deleteStub = sandbox.stub(v204._client, 'delete')
.returns(Promise.resolve(expectedResponse));
});

it('should call v204._client.delete()', () => {
return siftScienceClient.labels.deleteByUserId(userId, params)
.then(response => {
should.exist(response);
response.should.deepEqual(expectedResponse);

deleteStub.callCount.should.equal(1);
deleteStub.args[0][0].should.equal(`/users/${userId}/labels`);
deleteStub.args[0][1].should.deepEqual({
params
});
});
});
});
});
});
});
});
102 changes: 102 additions & 0 deletions tests/siftScience/client/v204/httpClient/delete-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
const should = require('should');
const Promise = require('bluebird');
const sinon = require('sinon');

let v204;
let sandbox;

before(() => {
v204 = require('lib/client/v204');

sandbox = sinon.sandbox.create();
});

describe('siftScience', () => {
describe('client', () => {
describe('v204', () => {
describe('httpClient', () => {
describe('delete', () => {
afterEach(() => {
sandbox.restore();
});

describe('when no path is passed', () => {
let path = null;
let expectedResponse = {};

let deleteStub;

before('stub v204.delete()', () => {
deleteStub = sandbox.stub(v204._client, 'delete')
.returns(Promise.resolve(expectedResponse));
});

it('should reject with an error', () => {
return v204.delete(path)
.then(should.not.exist)
.catch(err => {
should.exist(err);
err.should.be.instanceOf(Error);
err.message.should.equal('path is required');

deleteStub.callCount.should.equal(0);
});
});
});

describe('when a path is passed', () => {
let path = '/foobar';
let expectedResponse = {};

let deleteStub;

before('stub v204.delete()', () => {
deleteStub = sandbox.stub(v204._client, 'delete')
.returns(Promise.resolve(expectedResponse));
});

it('should resolve and call v204Client._client.delete()', () => {
return v204.delete(path)
.then(response => {
should.exist(response);
response.should.deepEqual(expectedResponse);

deleteStub.callCount.should.equal(1);
deleteStub.args[0][0].should.equal(path);
});
});
});

describe('when params are passed', () => {
let path = '/foobar';
let params = {
query: 'string'
};
let expectedResponse = {};

let deleteStub;

before('stub v204.delete()', () => {
deleteStub = sandbox.stub(v204._client, 'delete')
.returns(Promise.resolve(expectedResponse));
});

it('should resolve and call v204Client._client.delete()', () => {
return v204.delete(path, params)
.then(response => {
should.exist(response);
response.should.deepEqual(expectedResponse);

deleteStub.callCount.should.equal(1);
deleteStub.args[0][0].should.equal(path);
deleteStub.args[0][1].should.deepEqual({
params
});
});
});
});
});
});
});
});
});

0 comments on commit 40053ab

Please sign in to comment.