more tests, added contributing.md and license.md
This commit is contained in:
@ -31,6 +31,13 @@ var ca = require('./client_action');
|
||||
var Transport = require('./transport');
|
||||
|
||||
function Client(config) {
|
||||
config = config || {};
|
||||
|
||||
// our client will log minimally by default
|
||||
if (!config.hasOwnProperty('log')) {
|
||||
config.log = 'warning';
|
||||
}
|
||||
|
||||
this.transport = new Transport(config);
|
||||
|
||||
// instantiate the api's namespaces
|
||||
|
||||
@ -45,7 +45,11 @@ var castType = {
|
||||
return param.options[i];
|
||||
}
|
||||
}
|
||||
throw new TypeError('Invalid ' + name + ': expected one of ' + param.options.join(','));
|
||||
throw new TypeError('Invalid ' + name + ': expected ' + (
|
||||
param.options.length > 1
|
||||
? 'one of ' + param.options.join(',')
|
||||
: param.options[0]
|
||||
));
|
||||
},
|
||||
duration: function (param, val, name) {
|
||||
if (_.isNumeric(val) || _.isInterval(val)) {
|
||||
|
||||
@ -7,25 +7,12 @@ module.exports = Transport;
|
||||
var _ = require('./utils');
|
||||
var errors = require('./errors');
|
||||
var Host = require('./host');
|
||||
var Log = require('./log');
|
||||
var when = require('when');
|
||||
|
||||
function Transport(config) {
|
||||
config = config || {};
|
||||
|
||||
var LogClass;
|
||||
// setup the log
|
||||
switch (typeof config.log) {
|
||||
case 'function':
|
||||
LogClass = config.log;
|
||||
break;
|
||||
case 'undefined':
|
||||
config.log = 'warning';
|
||||
/* fall through */
|
||||
default:
|
||||
LogClass = Log;
|
||||
}
|
||||
|
||||
var LogClass = _.funcEnum(config, 'logClass', Transport.logs, 'main');
|
||||
config.log = this.log = new LogClass(config);
|
||||
|
||||
// overwrite the createDefer method if a new implementation is provided
|
||||
@ -37,25 +24,31 @@ function Transport(config) {
|
||||
var ConnectionPool = _.funcEnum(config, 'connectionPool', Transport.connectionPools, 'main');
|
||||
this.connectionPool = new ConnectionPool(config);
|
||||
|
||||
// setup the serializer
|
||||
var Serializer = _.funcEnum(config, 'serializer', Transport.serializers, 'json');
|
||||
this.serializer = new Serializer(config);
|
||||
|
||||
if (config.hosts) {
|
||||
var hosts = _.createArray(config.hosts, function (val) {
|
||||
var hostsConfig = _.createArray(config.hosts, function (val) {
|
||||
if (_.isPlainObject(val) || _.isString(val)) {
|
||||
return val;
|
||||
}
|
||||
});
|
||||
if (!hosts) {
|
||||
if (!hostsConfig) {
|
||||
throw new Error('Invalid hosts config. Expected a URL, an array of urls, a host config object, or an array of ' +
|
||||
'host config objects.');
|
||||
}
|
||||
|
||||
this.connectionPool.setHosts(_.map(hosts, function (conf) {
|
||||
var hosts = _.map(hostsConfig, function (conf) {
|
||||
return new Host(conf);
|
||||
}));
|
||||
}
|
||||
});
|
||||
|
||||
// setup the serializer
|
||||
var Serializer = _.funcEnum(config, 'serializer', Transport.serializers, 'json');
|
||||
this.serializer = new Serializer(config);
|
||||
if (config.randomizeHosts) {
|
||||
hosts = _.shuffle(hosts);
|
||||
}
|
||||
|
||||
this.connectionPool.setHosts(hosts);
|
||||
}
|
||||
}
|
||||
|
||||
Transport.connectionPools = {
|
||||
@ -66,6 +59,10 @@ Transport.serializers = {
|
||||
json: require('./serializers/json')
|
||||
};
|
||||
|
||||
Transport.logs = {
|
||||
main: require('./log')
|
||||
};
|
||||
|
||||
/**
|
||||
* Perform a request with the client's transport
|
||||
*
|
||||
|
||||
@ -399,13 +399,23 @@ _.funcEnum = function (config, name, opts, def) {
|
||||
case 'function':
|
||||
return val;
|
||||
case 'string':
|
||||
if (opts[val]) {
|
||||
if (opts.hasOwnProperty(val)) {
|
||||
return opts[val];
|
||||
}
|
||||
/* falls through */
|
||||
default:
|
||||
throw new TypeError('Invalid ' + name + ' "' + val + '", expected a function or one of ' +
|
||||
_.keys(opts).join(', '));
|
||||
var err = 'Invalid ' + name + ' "' + val + '", expected a function';
|
||||
switch (_.size(opts)) {
|
||||
case 0:
|
||||
break;
|
||||
case 1:
|
||||
err += 'or ' + _.keys(opts)[0];
|
||||
break;
|
||||
default:
|
||||
err += 'or one of ' + _.keys(opts).join(', ');
|
||||
break;
|
||||
}
|
||||
throw new TypeError(err);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user