Commit 9f02dbe4 by Ben Drucker

Use 2 args in #extend in place of #include and handle arrays outside constructor

parent 19b13741
......@@ -6,7 +6,6 @@
"eqeqeq": true,
"immed": true,
"indent": 2,
"latedef": true,
"newcap": true,
"noarg": true,
"quotmark": "single",
......
......@@ -34,12 +34,12 @@ ClearbitClient.prototype.endpoint = function (options) {
assert(options.api, 'An API must be specified');
return util.format.apply(util, [
return util.format(
ENDPOINT,
options.api,
options.stream ? '-stream' : '',
options.version
]);
);
};
ClearbitClient.prototype.url = function (options) {
......
......@@ -5,19 +5,19 @@ var resource = require('./resource');
var _ = require('lodash');
exports.Company = resource.create('Company', {api: 'company'})
.extend({
find: function(options){
options = options || {};
assert(options.domain, 'An domain must be provided');
.extend({
flag: function(options){
return this.constructor.post('/companies/' + this.id + '/flag', options);
}
},
{
find: function (options) {
options = options || {};
assert(options.domain, 'A domain must be provided');
return this.get(
'/companies/domain/' + options.domain,
_.omit(options, 'domain')
);
}
})
.include({
flag: function(options){
return this.constructor.post('/companies/' + this.id + '/flag', options);
}
});
return this.get(
'/companies/domain/' + options.domain,
_.omit(options, 'domain')
);
}
});
......@@ -5,32 +5,32 @@ var resource = require('./resource');
var _ = require('lodash');
exports.Person = resource.create('Person', {api: 'person'})
.extend({
find: function(options){
options = options || {};
assert(options.email, 'An email must be provided');
.extend({
flag: function(options){
return this.constructor.post('/people/' + this.id + '/flag', options);
}
},
{
find: function(options){
options = options || {};
assert(options.email, 'An email must be provided');
return this.get(
'/people/email/' + options.email,
_.omit(options, 'email')
);
}
})
.include({
flag: function(options){
return this.constructor.post('/people/' + this.id + '/flag', options);
}
});
return this.get(
'/people/email/' + options.email,
_.omit(options, 'email')
);
}
});
exports.PersonCompany = resource.create('PersonCompany', {api: 'person'})
.extend({
find: function(options){
options = options || {};
assert(options.email, 'An email must be provided');
.extend(null, {
find: function(options){
options = options || {};
assert(options.email, 'An email must be provided');
return this.get(
'/combined/email/' + options.email,
_.omit(options, 'email')
);
}
});
return this.get(
'/combined/email/' + options.email,
_.omit(options, 'email')
);
}
});
'use strict';
var createError = require('create-error');
var EventEmitter = require('events').EventEmitter;
var _ = require('lodash');
var Promise = require('bluebird');
function isQueued (err) {
return err.type === 'queued';
}
function isUnknownRecord (err) {
return err.type === 'unknown_record';
}
var createError = require('create-error');
var _ = require('lodash');
function ClearbitResource (data) {
_.extend(this, data);
}
ClearbitResource.extractParams = function(options) {
var params = _.omit(options || {},
'path', 'method', 'params',
'client', 'api', 'stream'
);
return _.isEmpty(params) ? null : params;
};
ClearbitResource.get = Promise.method(function (path, options) {
ClearbitResource.get = function (path, options) {
options = _.extend({
path: path,
method: 'get',
query: this.extractParams(options)
}, this.options, options || {});
query: extractParams(options)
}, this.options, options);
return this.client.request(options)
.bind(this)
.then(function (data) {
return new this(data);
})
.then(cast)
.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) {
ClearbitResource.post = function (path, options) {
options = _.extend({
path: path,
method: 'post',
query: this.extractParams(options)
}, this.options, options || {});
query: extractParams(options)
}, this.options, options);
return this.client.request(options)
.bind(this)
.then(function (data) {
return new this(data);
})
.then(cast)
.catch(isUnknownRecord, function () {
throw new this.NotFoundError(this.name + ' not found');
});
});
function createErrors (name) {
return {
NotFoundError: createError(name + 'NotFoundError'),
QueuedError: createError(name + 'QueuedError')
};
}
};
exports.create = function (name, options) {
var Resource = function (data) {
if (_.isArray(data)) {
return data.map(function(item){
return new this.constructor(item);
}.bind(this));
}
var Resource = function () {
ClearbitResource.apply(this, arguments);
};
_.extend(Resource,
new EventEmitter(),
EventEmitter.prototype,
ClearbitResource,
createErrors(name), {
_.extend(Resource, ClearbitResource, createErrors(name), {
name: name,
options: options
});
......@@ -96,19 +56,41 @@ exports.create = function (name, options) {
});
},
{
on: function () {
Resource.on.apply(Resource, arguments);
return this;
},
include: function (props) {
_.extend(Resource.prototype, props);
return this;
},
extend: function (props) {
_.extend(Resource, props);
extend: function (proto, ctor) {
_.extend(Resource.prototype, proto);
_.extend(Resource, ctor);
return this;
}
});
};
function cast (data) {
/* jshint validthis:true */
return !Array.isArray(data) ? new this(data) : data.map(function (result) {
return new this(result);
}, this);
}
function isQueued (err) {
return err.type === 'queued';
}
function isUnknownRecord (err) {
return err.type === 'unknown_record';
}
function createErrors (name) {
return {
NotFoundError: createError(name + 'NotFoundError'),
QueuedError: createError(name + 'QueuedError')
};
}
function extractParams (options) {
var params = _.omit(options || {},
'path', 'method', 'params',
'client', 'api', 'stream'
);
return _.isEmpty(params) ? null : params;
}
......@@ -4,7 +4,8 @@ var resource = require('./resource');
exports.Watchlist = resource.create('Watchlist', {
api: 'watchlist'
}).extend({
})
.extend(null, {
search: function(options) {
return this.post('/search/all', options);
}
......@@ -12,7 +13,8 @@ exports.Watchlist = resource.create('Watchlist', {
exports.WatchlistIndividual = resource.create('WatchlistIndividual', {
api: 'watchlist'
}).extend({
})
.extend(null, {
search: function(options) {
return this.post('/search/individuals', options);
}
......@@ -20,7 +22,8 @@ exports.WatchlistIndividual = resource.create('WatchlistIndividual', {
exports.WatchlistEntity = resource.create('WatchlistEntity', {
api: 'watchlist'
}).extend({
})
.extend(null, {
search: function(options) {
return this.post('/search/entities', options);
}
......
'use strict';
var expect = require('chai').expect;
var nock = require('nock');
var expect = require('chai').expect;
var nock = require('nock');
var Watchlist = require('../')('k').Watchlist;
describe('Watchlist', function () {
......@@ -25,7 +25,6 @@ describe('Watchlist', function () {
.reply(200, watchlist);
return Watchlist.search({name: 'Joe'})
.then(function (watchlist) {
// console.log(watchlist.constructor)
expect(watchlist[0])
.to.be.an.instanceOf(Watchlist)
.and.have.property('id', watchlist.id);
......
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