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