Commit da71b20c by Ben Drucker

Apply new resource ctor to APIs

parent 186ef9d7
...@@ -15,9 +15,9 @@ function ClearbitClient (config) { ...@@ -15,9 +15,9 @@ function ClearbitClient (config) {
assert(!!config.key, 'An API key must be provided'); assert(!!config.key, 'An API key must be provided');
this.key = config.key; this.key = config.key;
this.Person = require('./person')(this); this.Person = require('./person').Person(this);
this.PersonCompany = require('./person').PersonCompany(this);
this.Company = require('./company')(this); this.Company = require('./company')(this);
this.PersonCompany = require('./person_company')(this);
} }
var base = 'https://%s%s.clearbit.co/v%s'; var base = 'https://%s%s.clearbit.co/v%s';
...@@ -48,7 +48,7 @@ function generateQuery () { ...@@ -48,7 +48,7 @@ function generateQuery () {
} }
ClearbitClient.prototype.request = function (options) { ClearbitClient.prototype.request = function (options) {
options = _.defaults(options || /* istanbul ignore next */ {}, { options = _.defaults(options, {
method: 'get', method: 'get',
query: {} query: {}
}); });
......
'use strict'; 'use strict';
var assert = require('assert'); var assert = require('assert');
var _ = require('lodash'); var resource = require('./resource');
var Promise = require('bluebird');
var utils = require('./utils');
module.exports = function (client) { module.exports = resource.create('Company', {
function Company (data) {
_.extend(this, data);
}
Company.find = Promise.method(function (options) {
assert(options && options.domain, 'A domain must be provided');
return this.client.request(_.extend({
api: 'company', api: 'company',
path: '/companies/domain/' + options.domain path: '/companies/domain/<%= domain %>'
}, options)) })
.bind(this) .on('preFind', function (options) {
.then(utils.cast) assert(options.domain, 'A domain must be provided');
.catch(utils.isQueued, function () { });
throw new this.QueuedError('Company lookup queued');
})
.catch(utils.isUnknownRecord, function () {
throw new this.NotFoundError('Company not found');
});
});
Company.prototype.client = Company.client = client;
Company.NotFoundError = utils.NotFoundError;
Company.QueuedError = utils.QueuedError;
return Company;
};
'use strict'; 'use strict';
var assert = require('assert'); var assert = require('assert');
var _ = require('lodash'); var resource = require('./resource');
var Promise = require('bluebird');
var utils = require('./utils');
module.exports = function (client) { function requireEmail (options) {
function Person (data) { assert(options.email, 'An email must be provided');
_.extend(this, data); }
}
Person.find = Promise.method(function (options) { exports.Person = resource.create('Person', {
assert(options && options.email, 'An email must be provided');
return this.client.request(_.extend({
api: 'person', api: 'person',
path: '/people/email/' + options.email, path: '/people/email/<%= email %>',
query: _.pick(options, 'subscribe', 'company') queryKeys: 'subscribe'
}, options)) })
.bind(this) .on('preFind', requireEmail);
.then(utils.cast)
.catch(utils.isQueued, function () {
throw new this.QueuedError('Person lookup queued');
})
.catch(utils.isUnknownRecord, function () {
throw new this.NotFoundError('Person not found');
});
});
Person.prototype.client = Person.client = client; exports.PersonCompany = resource.create('PersonCompany', {
Person.NotFoundError = utils.NotFoundError; api: 'person',
Person.QueuedError = utils.QueuedError; path: '/combined/email/<%= email %>',
queryKeys: 'subscribe'
return Person; })
}; .on('preFind', requireEmail);
'use strict';
var assert = require('assert');
var _ = require('lodash');
var Promise = require('bluebird');
var utils = require('./utils');
module.exports = function (client) {
function PersonCompany (data) {
_.extend(this, data);
}
PersonCompany.find = Promise.method(function (options) {
assert(options && options.email, 'An email must be provided');
return this.client.request(_.extend({
api: 'person',
path: '/combined/email/' + options.email,
query: _.pick(options, 'subscribe', 'webhook_id')
}, options))
.bind(this)
.then(utils.cast)
.catch(utils.isQueued, function () {
throw new this.QueuedError('PersonCompany lookup queued');
})
.catch(utils.isUnknownRecord, function () {
throw new this.NotFoundError('PersonCompany not found');
});
});
PersonCompany.prototype.client = PersonCompany.client = client;
PersonCompany.NotFoundError = utils.NotFoundError;
PersonCompany.QueuedError = utils.QueuedError;
return PersonCompany;
};
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
var expect = require('chai').use(require('chai-as-promised')).expect; var expect = require('chai').use(require('chai-as-promised')).expect;
var nock = require('nock'); var nock = require('nock');
var Person = require('../')('k').Person; var Person = require('../')('k').Person;
var PersonCompany = require('../')('k').PersonCompany;
describe('Person', function () { describe('Person', function () {
...@@ -15,9 +16,10 @@ describe('Person', function () { ...@@ -15,9 +16,10 @@ describe('Person', function () {
mock.done(); mock.done();
}); });
describe('Person#find', function () {
var alex = require('./fixtures/person'); var alex = require('./fixtures/person');
var company = require('./fixtures/company');
describe('Person#find', function () {
it('can find a person by email', function () { it('can find a person by email', function () {
mock mock
...@@ -38,13 +40,6 @@ describe('Person', function () { ...@@ -38,13 +40,6 @@ describe('Person', function () {
return Person.find({email: 'alex@alexmaccaw.com', subscribe: true}); return Person.find({email: 'alex@alexmaccaw.com', subscribe: true});
}); });
it('can override the company setting', function () {
mock
.get('/v1/people/email/alex@alexmaccaw.com?company=false')
.reply(200, alex);
return Person.find({email: 'alex@alexmaccaw.com', company: false});
});
it('can handle queued requests', function () { it('can handle queued requests', function () {
mock mock
.get('/v1/people/email/alex@alexmaccaw.com') .get('/v1/people/email/alex@alexmaccaw.com')
...@@ -71,4 +66,37 @@ describe('Person', function () { ...@@ -71,4 +66,37 @@ describe('Person', function () {
}); });
describe('PersonCompany#find', function () {
it('can find a person by email', function () {
mock
.get('/v1/combined/email/alex@alexmaccaw.com')
.reply(200, {
person: alex,
company: company
});
return PersonCompany.find({email: 'alex@alexmaccaw.com'})
.then(function (personCompany) {
expect(personCompany)
.to.be.an.instanceOf(PersonCompany)
.and.have.have.keys('person', 'company')
.and.have.deep.property('person.id', alex.id);
});
});
it('can handle queued requests', function () {
mock
.get('/v1/combined/email/alex@alexmaccaw.com')
.reply(202, {
error: {
type: 'queued'
}
});
return expect(PersonCompany.find({email: 'alex@alexmaccaw.com'}))
.to.be.rejectedWith(PersonCompany.QueuedError);
});
});
}); });
'use strict';
var expect = require('chai').use(require('chai-as-promised')).expect;
var nock = require('nock');
var PersonCompany = require('../')('k').PersonCompany;
describe('PersonCompany', function () {
var mock;
before(function () {
mock = nock('https://person.clearbit.co');
});
after(nock.cleanAll);
afterEach(function () {
mock.done();
});
describe('PersonCompany#find', function () {
var alex = require('./fixtures/person');
it('can find a person by email', function () {
mock
.get('/v1/combined/email/alex@alexmaccaw.com')
.reply(200, alex);
return PersonCompany.find({email: 'alex@alexmaccaw.com'})
.then(function (person) {
expect(person)
.to.be.an.instanceOf(PersonCompany)
.and.have.property('id', alex.id);
});
});
it('can handle queued requests', function () {
mock
.get('/v1/combined/email/alex@alexmaccaw.com')
.reply(202, {
error: {
type: 'queued'
}
});
return expect(PersonCompany.find({email: 'alex@alexmaccaw.com'}))
.to.be.rejectedWith(PersonCompany.QueuedError);
});
});
});
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