Commit ca90443d by Ben Drucker

Provide a client#request method

parent c0d2af94
...@@ -36,6 +36,7 @@ ...@@ -36,6 +36,7 @@
"gulp-mocha": "1", "gulp-mocha": "1",
"jshint-stylish": "1", "jshint-stylish": "1",
"mocha": "1", "mocha": "1",
"sinon": "1" "sinon": "1",
"nock": "0.42"
} }
} }
'use strict'; 'use strict';
var assert = require('assert'); var assert = require('assert');
var util = require('util'); var util = require('util');
var _ = require('lodash'); var _ = require('lodash');
var Promise = require('bluebird');
var needle = Promise.promisifyAll(require('needle'));
var pkg = require('../package.json');
function ClearbitClient (config) { function ClearbitClient (config) {
config = config || {}; config = config || {};
...@@ -13,7 +16,7 @@ function ClearbitClient (config) { ...@@ -13,7 +16,7 @@ function ClearbitClient (config) {
var base = 'https://%s%s.clearbit.co/v%s'; var base = 'https://%s%s.clearbit.co/v%s';
ClearbitClient.prototype.base = function (options) { ClearbitClient.prototype.base = function (options) {
options = _.defaults(options || {}, { options = _.defaults(options, {
version: '1', version: '1',
stream: false stream: false
}); });
...@@ -26,4 +29,30 @@ ClearbitClient.prototype.base = function (options) { ...@@ -26,4 +29,30 @@ ClearbitClient.prototype.base = function (options) {
]); ]);
}; };
ClearbitClient.prototype.url = function (options) {
_.defaults(options, {
path: ''
});
return this.base(options) + options.path;
}
ClearbitClient.prototype.request = function (options) {
options = _.defaults(options || {}, {
method: 'get',
data: null
});
return needle.requestAsync(
options.method,
this.url(options),
options.data,
{
username: this.key,
user_agent: 'ClearbitNode/v' + pkg.version
}
)
.spread(function (response) {
return response;
});
};
module.exports = ClearbitClient; module.exports = ClearbitClient;
\ No newline at end of file
'use strict'; 'use strict';
var expect = require('chai').expect; var expect = require('chai').expect;
var nock = require('nock');
var ClearbitClient = require('../src/client'); var ClearbitClient = require('../src/client');
var pkg = require('../package.json');
describe('ClearbitClient', function () { describe('ClearbitClient', function () {
...@@ -34,7 +36,8 @@ describe('ClearbitClient', function () { ...@@ -34,7 +36,8 @@ describe('ClearbitClient', function () {
describe('#base', function () { describe('#base', function () {
it('requires an API', function () { it('requires an API', function () {
expect(client.base).to.throw(/API must be specified/); expect(client.base.bind(client, {}))
.to.throw(/API must be specified/);
}); });
it('can generate the default base', function () { it('can generate the default base', function () {
...@@ -62,4 +65,49 @@ describe('ClearbitClient', function () { ...@@ -62,4 +65,49 @@ describe('ClearbitClient', function () {
}); });
describe('#request', function () {
var mock;
before(function () {
mock = nock('https://person.clearbit.co');
});
after(nock.restore);
afterEach(function () {
mock.done();
});
it('sends a get request to the specified endpoint', function () {
mock
.get('/v1/people/email/bvdrucker@gmail.com')
.reply(202);
return client.request({
api: 'person',
path: '/people/email/bvdrucker@gmail.com'
});
});
it('sends a basic auth header', function () {
mock
.get('/v1/people/email/bvdrucker@gmail.com')
.matchHeader('Authorization', 'Basic aw==')
.reply(202);
return client.request({
api: 'person',
path: '/people/email/bvdrucker@gmail.com'
});
});
it('sends a user agent', function () {
mock
.get('/v1/people/email/bvdrucker@gmail.com')
.matchHeader('User-Agent', 'ClearbitNode/v' + pkg.version)
.reply(202);
return client.request({
api: 'person',
path: '/people/email/bvdrucker@gmail.com'
});
});
});
}); });
\ 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