Commit da71b20c by Ben Drucker

Apply new resource ctor to APIs

parent 186ef9d7
......@@ -15,9 +15,9 @@ function ClearbitClient (config) {
assert(!!config.key, 'An API key must be provided');
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.PersonCompany = require('./person_company')(this);
}
var base = 'https://%s%s.clearbit.co/v%s';
......@@ -48,7 +48,7 @@ function generateQuery () {
}
ClearbitClient.prototype.request = function (options) {
options = _.defaults(options || /* istanbul ignore next */ {}, {
options = _.defaults(options, {
method: 'get',
query: {}
});
......
'use strict';
var assert = require('assert');
var _ = require('lodash');
var Promise = require('bluebird');
var utils = require('./utils');
module.exports = function (client) {
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',
path: '/companies/domain/' + options.domain
}, options))
.bind(this)
.then(utils.cast)
.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;
};
var assert = require('assert');
var resource = require('./resource');
module.exports = resource.create('Company', {
api: 'company',
path: '/companies/domain/<%= domain %>'
})
.on('preFind', function (options) {
assert(options.domain, 'A domain must be provided');
});
'use strict';
var assert = require('assert');
var _ = require('lodash');
var Promise = require('bluebird');
var utils = require('./utils');
var assert = require('assert');
var resource = require('./resource');
module.exports = function (client) {
function Person (data) {
_.extend(this, data);
}
function requireEmail (options) {
assert(options.email, 'An email must be provided');
}
Person.find = Promise.method(function (options) {
assert(options && options.email, 'An email must be provided');
return this.client.request(_.extend({
api: 'person',
path: '/people/email/' + options.email,
query: _.pick(options, 'subscribe', 'company')
}, options))
.bind(this)
.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');
});
});
exports.Person = resource.create('Person', {
api: 'person',
path: '/people/email/<%= email %>',
queryKeys: 'subscribe'
})
.on('preFind', requireEmail);
Person.prototype.client = Person.client = client;
Person.NotFoundError = utils.NotFoundError;
Person.QueuedError = utils.QueuedError;
return Person;
};
exports.PersonCompany = resource.create('PersonCompany', {
api: 'person',
path: '/combined/email/<%= email %>',
queryKeys: 'subscribe'
})
.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;
};
'use strict';
var expect = require('chai').use(require('chai-as-promised')).expect;
var nock = require('nock');
var Person = require('../')('k').Person;
var expect = require('chai').use(require('chai-as-promised')).expect;
var nock = require('nock');
var Person = require('../')('k').Person;
var PersonCompany = require('../')('k').PersonCompany;
describe('Person', function () {
......@@ -15,9 +16,10 @@ describe('Person', function () {
mock.done();
});
describe('Person#find', function () {
var alex = require('./fixtures/person');
var company = require('./fixtures/company');
var alex = require('./fixtures/person');
describe('Person#find', function () {
it('can find a person by email', function () {
mock
......@@ -38,13 +40,6 @@ describe('Person', function () {
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 () {
mock
.get('/v1/people/email/alex@alexmaccaw.com')
......@@ -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