Commit c0d2af94 by Ben Drucker

Generate the base URL for request using client#base

parent 16127fba
...@@ -24,7 +24,8 @@ ...@@ -24,7 +24,8 @@
"dependencies": { "dependencies": {
"bluebird": "2", "bluebird": "2",
"create-error": "0.3", "create-error": "0.3",
"needle": "0.7" "needle": "0.7",
"lodash": "2"
}, },
"devDependencies": { "devDependencies": {
"chai": "1", "chai": "1",
......
'use strict'; 'use strict';
var assert = require('assert'); var assert = require('assert');
var util = require('util');
var _ = require('lodash');
function ClearbitClient (config) { function ClearbitClient (config) {
config = config || {}; config = config || {};
...@@ -9,4 +11,19 @@ function ClearbitClient (config) { ...@@ -9,4 +11,19 @@ function ClearbitClient (config) {
this.key = config.key; this.key = config.key;
}; };
var base = 'https://%s%s.clearbit.co/v%s';
ClearbitClient.prototype.base = function (options) {
options = _.defaults(options || {}, {
version: '1',
stream: false
});
assert(options.api, 'An API must be specified');
return util.format.apply(util, [
base,
options.api,
options.stream ? '-stream' : '',
options.version
]);
};
module.exports = ClearbitClient; module.exports = ClearbitClient;
\ No newline at end of file
...@@ -5,19 +5,61 @@ var ClearbitClient = require('../src/client'); ...@@ -5,19 +5,61 @@ var ClearbitClient = require('../src/client');
describe('ClearbitClient', function () { describe('ClearbitClient', function () {
it('must be called with new', function () { var client;
expect(ClearbitClient).to.throw(/called with new/); beforeEach(function () {
client = new ClearbitClient({
key: 'k'
});
}); });
it('must provide an API key', function () { describe('Constructor', function () {
expect(function () {
return new ClearbitClient(); it('must be called with new', function () {
}) expect(ClearbitClient).to.throw(/called with new/);
.to.throw(/API key/); });
it('must provide an API key', function () {
expect(function () {
return new ClearbitClient();
})
.to.throw(/API key/);
});
it('configures the API key', function () {
expect(new ClearbitClient({key: 'k'})).to.have.property('key', 'k');
});
}); });
it('configures the API key', function () { describe('#base', function () {
expect(new ClearbitClient({key: 'k'})).to.have.property('key', 'k');
it('requires an API', function () {
expect(client.base).to.throw(/API must be specified/);
});
it('can generate the default base', function () {
expect(client.base({
api: 'person'
}))
.to.equal('https://person.clearbit.co/v1');
});
it('can generate a streaming base', function () {
expect(client.base({
api: 'person',
stream: true
}))
.to.equal('https://person-stream.clearbit.co/v1');
});
it('can set a custom version', function () {
expect(client.base({
api: 'person',
version: '2'
}))
.to.equal('https://person.clearbit.co/v2');
});
}); });
}); });
\ No newline at end of file
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