Commit 0716f356 by Alex MacCaw

Refactor and add watchlist support

parent 001b6908
......@@ -12,23 +12,30 @@ var pkg = require('../package.json');
function ClearbitClient (config) {
config = config || {};
assert(this instanceof ClearbitClient, 'Client must be called with new');
assert(!!config.key, 'An API key must be provided');
this.key = config.key;
this.key = config.key || process.env.CLEARBIT_KEY;
assert(!!this.key, 'An API key must be provided');
this.Company = require('./company').Company(this);
this.Person = require('./person').Person(this);
this.PersonCompany = require('./person').PersonCompany(this);
this.Company = require('./company')(this);
this.Watchlist = require('./watchlist').Watchlist(this);
this.WatchlistEntity = require('./watchlist').WatchlistEntity(this);
this.WatchlistIndividual = require('./watchlist').WatchlistIndividual(this);
}
var base = 'https://%s%s.clearbit.com/v%s';
ClearbitClient.prototype.base = function (options) {
var ENDPOINT = 'https://%s%s.clearbit.com/v%s';
ClearbitClient.prototype.endpoint = function (options) {
options = _.defaults(options, {
version: '1',
stream: false
});
assert(options.api, 'An API must be specified');
return util.format.apply(util, [
base,
ENDPOINT,
options.api,
options.stream ? '-stream' : '',
options.version
......@@ -39,25 +46,19 @@ ClearbitClient.prototype.url = function (options) {
_.defaults(options, {
path: ''
});
return this.base(options) + options.path;
return this.endpoint(options) + options.path;
};
function generateQuery () {
var query = _.omit(_.extend.apply(_, [{}].concat([].slice.apply(arguments))), _.isUndefined);
return _.isEmpty(query) ? undefined : query;
}
ClearbitClient.prototype.request = function (options) {
options = _.defaults(options, {
method: 'get',
query: {}
});
return needle.requestAsync(
options.method,
this.url(options),
generateQuery({
webhook_id: options.webhook_id
}, options.query),
options.query,
{
timeout: options.stream ? 60000 : 10000,
username: this.key,
......
......@@ -4,19 +4,20 @@ var assert = require('assert');
var resource = require('./resource');
var _ = require('lodash');
module.exports = resource.create('Company', {
api: 'company',
path: '/companies/domain/<%= domain %>'
exports.Company = resource.create('Company', {api: 'company'})
.extend({
find: function(options){
options = options || {};
assert(options.domain, 'An domain must be provided');
return this.get(
'/companies/domain/' + options.domain,
_.omit(options, 'domain')
);
}
})
.on('preFind', function (options) {
assert(options.domain, 'A domain must be provided');
}).include({
flag: function(params, options){
return this.client.request(_.extend({
api: this._options.api,
method: 'post',
path: _.template('/companies/<%= id %>/flag', this),
query: params || {}
}, options));
.include({
flag: function(options){
return this.constructor.post('/companies/' + this.id + '/flag', options);
}
});
......@@ -4,30 +4,33 @@ var assert = require('assert');
var resource = require('./resource');
var _ = require('lodash');
function requireEmail (options) {
assert(options.email, 'An email must be provided');
}
exports.Person = resource.create('Person', {api: 'person'})
.extend({
find: function(options){
options = options || {};
assert(options.email, 'An email must be provided');
exports.Person = resource.create('Person', {
api: 'person',
path: '/people/email/<%= email %>',
queryKeys: 'subscribe'
return this.get(
'/people/email/' + options.email,
_.omit(options, 'email')
);
}
})
.on('preFind', requireEmail)
.include({
flag: function(params, options){
return this.client.request(_.extend({
api: this._options.api,
method: 'post',
path: _.template('/people/<%= id %>/flag', this),
query: params || {}
}, options));
flag: function(options){
return this.constructor.post('/people/' + this.id + '/flag', options);
}
});
exports.PersonCompany = resource.create('PersonCompany', {
api: 'person',
path: '/combined/email/<%= email %>',
queryKeys: 'subscribe'
exports.PersonCompany = resource.create('PersonCompany', {api: 'person'})
.extend({
find: function(options){
options = options || {};
assert(options.email, 'An email must be provided');
return this.get(
'/combined/email/' + options.email,
_.omit(options, 'email')
);
}
})
.on('preFind', requireEmail);
......@@ -17,29 +17,42 @@ function ClearbitResource (data) {
_.extend(this, data);
}
ClearbitResource.find = Promise.method(function (options) {
options = options || /* istanbul ignore next */ {};
this.emit('preFind', options);
return this.client.request(_.extend({
api: this._options.api,
path: this._options.template(options),
query: _.pick(options, this._options.queryKeys)
}, options))
.bind(this)
.then(function (data) {
return new this(
_.extend({}, data, {
_options: this._options,
client: this.client
})
);
})
.catch(isQueued, function () {
throw new this.QueuedError(this._name + ' lookup queued');
})
.catch(isUnknownRecord, function () {
throw new this.NotFoundError(this._name + ' not found');
});
ClearbitResource.extractParams = function(params) {
return _.omit(params || {}, 'path', 'method', 'params', 'client', 'api', 'stream');
};
ClearbitResource.get = Promise.method(function (path, options) {
options = _.extend({
path: path,
method: 'get',
params: this.extractParams(options)
}, this.options, options || {});
return this.client.request(options)
.bind(this)
.then(function (data) {
return new this(data);
})
.catch(isQueued, function () {
throw new this.QueuedError(this.name + ' lookup queued');
})
.catch(isUnknownRecord, function () {
throw new this.NotFoundError(this.name + ' not found');
});
});
ClearbitResource.post = Promise.method(function (path, options) {
options = _.extend({
path: path,
method: 'post',
query: this.extractParams(options)
}, this.options, options || {});
return this.client.request(options)
.bind(this)
.catch(isUnknownRecord, function () {
throw new this.NotFoundError(this.name + ' not found');
});
});
function createErrors (name) {
......@@ -54,11 +67,13 @@ exports.create = function (name, options) {
ClearbitResource.apply(this, arguments);
};
_.extend(Resource, new EventEmitter(), EventEmitter.prototype, ClearbitResource, createErrors(name), {
_name: name,
_options: _.extend({}, options, {
template: _.template(options.path)
})
_.extend(Resource,
new EventEmitter(),
EventEmitter.prototype,
ClearbitResource,
createErrors(name), {
name: name,
options: options
});
return _.extend(function (client) {
......@@ -75,6 +90,11 @@ exports.create = function (name, options) {
include: function (props) {
_.extend(Resource.prototype, props);
return this;
},
extend: function (props) {
_.extend(Resource, props);
return this;
}
});
};
'use strict';
var assert = require('assert');
var resource = require('./resource');
var _ = require('lodash');
exports.Watchlist = resource.create('Watchlist', {
api: 'watchlist'
}).extend({
search: function(options) {
return this.post('/search/all', options);
}
})
exports.WatchlistIndividual = resource.create('WatchlistIndividual', {
api: 'watchlist'
}).extend({
search: function(options) {
return this.post('/search/individuals', options);
}
});
exports.WatchlistEntity = resource.create('WatchlistEntity', {
api: 'watchlist'
}).extend({
search: function(options) {
return this.post('/search/entities', options);
}
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment