Commit 84168845 by Arnout Kazemier

Merge pull request #11 from observing/company

Company API
parents d0c7ae29 aac1161b
{
"rules": {
"sort-vars": 0,
"no-new": 0,
"indent": [
2,
2
],
"quotes": [
2,
"single"
],
"semi": [
2,
"always"
],
"space-before-function-paren": [
2,
{
"anonymous": "always",
"named": "never"
}
],
"func-style": [
2,
"declaration"
]
},
"env": {
"node": true,
"mocha": true
}
}
\ No newline at end of file
language: node_js
node_js:
- "0.10"
- "0.11"
- "0.12"
- iojs
script:
- "npm run test-travis"
......@@ -119,17 +119,18 @@ fullcontact.location.enrich('denver', function (err, data) {
### Person
The `Person` endpoint is confidently namespaced as a `.person` property. Each
person API has an optional `queue` argument which you can use to indicate that
The `Person` endpoint is confidently namespaced as a `.person` property.
Each person API has an optional `queue` argument which you can use to indicate that
this request will should be pre-processed by FullContact and that you want to
fetch the details later. According to the API it should to receive the value `1`
as queue.
The following methods are available on this API:
#### person.email(address, [queue], fn);
#### person.email(address, [queue], [webhookUrl], [webhookId], fn);
Retrieves contact information by e-mail address.
Supports the use of webhooks by providing an url and id.
```js
fullcontact.person.email('foo@bar.com', function (err, data) {
......@@ -137,6 +138,12 @@ fullcontact.person.email('foo@bar.com', function (err, data) {
});
```
```js
fullcontact.person.email('foo@bar.com', null, 'https://mycallbackurl.com', 'webhooktracker', function (err, data) {
..
});
```
#### person.md5(address, [queue], fn);
Retrieves contact information by e-mail address but transforms the email to an
......
'use strict';
/**
* Access the Company API.
*
* @constructor
* @param {FullContact} api Reference to the FullContact wrapping instance.
* @api public
*/
function Company(api) {
this.api = api;
this.endpoint = 'https://api.fullcontact.com/' + api.version + '/company/lookup.json';
this.send = api.process.bind(api, this);
}
/**
* Retrieve company information by domain
*
* ```js
* fullcontact.company.domain('apple.com', [webhookUrl], [webhookId], fn);
* ```
*
* @returns {Company}
* @api public
*/
Company.prototype.domain = function domain() {
var args = this.api.args(arguments, 'webhookUrl', 'webhookId');
this.send({ domain: args.value }, args);
return this;
};
module.exports = Company;
......@@ -10,7 +10,7 @@
function Email(api) {
this.api = api;
this.endpoint = 'https://api.fullcontact.com/'+ api.version +'/email/';
this.endpoint = 'https://api.fullcontact.com/' + api.version + '/email/';
this.send = api.process.bind(api, this);
}
......@@ -30,7 +30,7 @@ Email.prototype.disposable = function disposable() {
//
// Add a custom endpoint.
//
args.endpoint = this.endpoint +'disposable.json';
args.endpoint = this.endpoint + 'disposable.json';
this.send({ email: args.value }, args);
return this;
......
......@@ -10,7 +10,7 @@
function Location(api) {
this.api = api;
this.endpoint = 'https://api.fullcontact.com/'+ api.version +'/address/';
this.endpoint = 'https://api.fullcontact.com/' + api.version + '/address/';
this.send = api.process.bind(api, this);
}
......@@ -30,7 +30,7 @@ Location.prototype.normalize = function normalize() {
//
// Add a custom endpoint.
//
args.endpoint = this.endpoint +'locationNormalizer.json';
args.endpoint = this.endpoint + 'locationNormalizer.json';
this.send({ place: args.value }, args);
return this;
......@@ -52,7 +52,7 @@ Location.prototype.enrich = function enrichment() {
//
// Add a custom endpoint.
//
args.endpoint = this.endpoint +'locationEnrichment.json';
args.endpoint = this.endpoint + 'locationEnrichment.json';
this.send({ place: args.value }, args);
return this;
......
......@@ -10,7 +10,7 @@
function Name(api) {
this.api = api;
this.endpoint = 'https://api.fullcontact.com/'+ api.version +'/name/';
this.endpoint = 'https://api.fullcontact.com/' + api.version + '/name/';
this.send = api.process.bind(api, this);
}
......@@ -30,7 +30,7 @@ Name.prototype.normalize = function normalize() {
//
// Add a custom endpoint.
//
args.endpoint = this.endpoint +'normalizer.json';
args.endpoint = this.endpoint + 'normalizer.json';
this.send({ q: args.value }, args);
return this;
......@@ -53,7 +53,7 @@ Name.prototype.deducer = function deducer() {
//
// Add a custom endpoint.
//
args.endpoint = this.endpoint +'deducer.json';
args.endpoint = this.endpoint + 'deducer.json';
this.send(args.value, args);
return this;
......@@ -75,7 +75,7 @@ Name.prototype.similarity = function similarity() {
//
// Add a custom endpoint.
//
args.endpoint = this.endpoint +'similarity.json';
args.endpoint = this.endpoint + 'similarity.json';
this.send({ q1: args.value, q2: args.q2 }, args);
return this;
......@@ -100,7 +100,7 @@ Name.prototype.stats = function stats() {
//
// Add a custom endpoint.
//
args.endpoint = this.endpoint +'stats.json';
args.endpoint = this.endpoint + 'stats.json';
this.send(args.value, args);
return this;
......@@ -122,7 +122,7 @@ Name.prototype.parser = function parser() {
//
// Add a custom endpoint.
//
args.endpoint = this.endpoint +'parser.json';
args.endpoint = this.endpoint + 'parser.json';
this.send({ q: args.value }, args);
return this;
......
......@@ -10,7 +10,7 @@
function Person(api) {
this.api = api;
this.endpoint = 'https://api.fullcontact.com/'+ api.version +'/person.json';
this.endpoint = 'https://api.fullcontact.com/' + api.version + '/person.json';
this.send = api.process.bind(api, this);
}
......@@ -18,7 +18,7 @@ function Person(api) {
* Retrieve contact information by e-mail.
*
* ```js
* fullcontact.person.email('opensource@observe.it', [queue], fn);
* fullcontact.person.email('opensource@observe.it', [queue], [webhookUrl], [webhookId], fn);
* ```
*
* @returns {Person}
......
'use strict';
var request = require('request')
, qs = require('qs');
, qs = require('qs');
var slice = Array.prototype.slice;
......@@ -13,7 +13,9 @@ var slice = Array.prototype.slice;
* @api public
*/
function FullContact(api) {
if (!(this instanceof FullContact)) return new FullContact(api);
if (!(this instanceof FullContact)) {
return new FullContact(api);
}
this.key = api; // API key
this.version = 'v2'; // API version
......@@ -25,7 +27,9 @@ function FullContact(api) {
this.queueing = false; // Should we be queueing requests
this.requests = []; // Stores all queued commands
if (!this.key) throw new Error('Missing API key');
if (!this.key) {
throw new Error('Missing API key');
}
}
/**
......@@ -42,23 +46,37 @@ FullContact.prototype.process = function req(api, query, args) {
//
// Add some addition properties
//
if (args.queue) query.queue = args.queue;
if (args.casing) query.casing = args.casing;
if (args.population) query.includeZeroPopulation = !!args.population;
if (args.webhookUrl) query.webhookUrl = args.webhookUrl;
if (args.webhookId) query.webhookId = args.webhookId;
if (args.queue) {
query.queue = args.queue;
}
if (args.casing) {
query.casing = args.casing;
}
if (args.population) {
query.includeZeroPopulation = !!args.population;
}
if (args.webhookUrl) {
query.webhookUrl = args.webhookUrl;
}
if (args.webhookId) {
query.webhookId = args.webhookId;
}
//
// The packet that is send to the server or queued when we are in queuing
// mode.
//
var packet = {
method: 'GET',
uri: args.endpoint || api.endpoint,
qs: query
method: 'GET',
uri: args.endpoint || api.endpoint,
qs: query
};
if (this.queueing) return this.queue(packet, args);
if (this.queueing) {
return this.queue(packet, args);
}
return this.request(packet, args);
};
......@@ -71,10 +89,12 @@ FullContact.prototype.process = function req(api, query, args) {
*/
FullContact.prototype.request = function req(packet, args) {
var fn = args.fn
, self = this;
, self = this;
request(packet, function requested(err, res, body) {
if (err) return fn(err);
if (err) {
return fn(err);
}
self.ratereset = +res.headers['x-rate-limit-reset'] || self.ratereset;
self.ratelimit = +res.headers['x-rate-limit-limit'] || self.ratelimit;
......@@ -83,10 +103,10 @@ FullContact.prototype.request = function req(packet, args) {
//
// Parse response to JSON.
//
if ('string' === typeof body) {
if (typeof body === 'string') {
try { body = JSON.parse(body); }
catch (e) {
return fn(new Error('Failed to parse API response ('+ e.message +')'));
return fn(new Error('Failed to parse API response (' + e.message + ')'));
}
}
......@@ -133,7 +153,7 @@ FullContact.prototype.multi = function multi() {
*/
FullContact.prototype.queue = function queue(packet, args) {
this.requests.push({
url: packet.uri +'?'+ qs.stringify(packet.qs),
url: packet.uri + '?' + qs.stringify(packet.qs),
fn: args.fn
});
......@@ -159,7 +179,9 @@ FullContact.prototype.exec = function exec(fn) {
*/
function bailout(err) {
requests.forEach(function cb(data) {
if (data.fn) data.fn(err);
if (data.fn) {
data.fn(err);
}
});
fn(err);
......@@ -172,7 +194,7 @@ FullContact.prototype.exec = function exec(fn) {
request({
method: 'POST',
uri: 'https://api.fullcontact.com/'+ this.version +'/batch.json',
uri: 'https://api.fullcontact.com/' + this.version + '/batch.json',
qs: { apiKey: this.key },
json: {
requests: requests.map(function urlsonly(data) {
......@@ -180,7 +202,9 @@ FullContact.prototype.exec = function exec(fn) {
})
}
}, function requested(err, res, body) {
if (err) return bailout(err);
if (err) {
return bailout(err);
}
fn(err, body.responses);
});
......@@ -243,9 +267,10 @@ FullContact.createClient = function createClient(api) {
// Expose the endpoints.
//
FullContact.Location = require('./endpoints/location');
FullContact.Person = require('./endpoints/person');
FullContact.Email = require('./endpoints/email');
FullContact.Name = require('./endpoints/name');
FullContact.Person = require('./endpoints/person');
FullContact.Email = require('./endpoints/email');
FullContact.Name = require('./endpoints/name');
FullContact.Company = require('./endpoints/company');
//
// Lazy load the various of endpoints so they only get initialized when we
......@@ -267,6 +292,10 @@ FullContact.define(FullContact.prototype, 'name', function define() {
return new FullContact.Name(this);
});
FullContact.define(FullContact.prototype, 'company', function define() {
return new FullContact.Company(this);
});
//
// Expose the FullContact API.
//
......
......@@ -15,12 +15,12 @@
"author": "Arnout Kazemier <opensource@observe.it>",
"license": "MIT",
"dependencies": {
"request": "~2.25.0",
"qs": "~2.3.3"
"request": "2.x.x",
"qs": "2.x.x"
},
"devDependencies": {
"mocha": "2.1.x",
"chai": "1.10.x",
"pre-commit": "0.0.x"
"mocha": "2.x.x",
"chai": "2.x.x",
"pre-commit": "1.x.x"
}
}
}
\ No newline at end of file
describe('FullContact.Company', function () {
'use strict';
var FullContact = require('../');
var chai = require('chai');
chai.config.includeStack = true;
//
// The API key we use for testing.
//
var key = process.env.API_KEY;
if (!key) {
throw new Error('Please provide your API using the API_KEY env variable.');
}
//
// Some of the requests take a really long time, so set a really long timeout
//
this.timeout(20000);
//
// Pre-create an API instance
//
var api = new FullContact(key);
describe('#domain', function () {
it('retrieves data by domain', function (done) {
api.company.domain('apple.com', done);
});
it('provides the proper casing');
});
describe('#domain with webhook url/id', function () {
it('retrieves data by e-mail and sets up a webhook with the right url and id', function (done) {
api.company.domain('apple.com', 'http://requestb.in/1bxgb751', 'webhookTest', done);
});
it('provides the proper casing');
});
});
describe('FullContact.Email', function () {
'use strict';
var FullContact = require('../')
, chai = require('chai')
, expect = chai.expect;
var FullContact = require('../');
var chai = require('chai');
chai.Assertion.includeStack = true;
chai.config.includeStack = true;
//
// The API key we use for testing.
//
var key = process.env.API_KEY;
if (!key) throw new Error('Please provide your API using the API_KEY env variable.');
if (!key) {
throw new Error('Please provide your API using the API_KEY env variable.');
}
//
// Some of the requests take a really long time, so set a really long timeout
......
describe('FullContact', function () {
'use strict';
var FullContact = require('../')
, chai = require('chai')
, expect = chai.expect;
var FullContact = require('../');
var chai = require('chai');
var expect = chai.expect;
chai.Assertion.includeStack = true;
chai.config.includeStack = true;
//
// The API key we use for testing.
//
var key = process.env.API_KEY;
if (!key) throw new Error('Please provide your API using the API_KEY env variable.');
if (!key) {
throw new Error('Please provide your API using the API_KEY env variable.');
}
//
// Some of the requests take a really long time, so set a really long timeout
......@@ -55,7 +57,7 @@ describe('FullContact', function () {
});
it('errors when an invalid API key is given', function (done) {
var client = new FullContact(key +'adfasfdsfadsfas');
var client = new FullContact(key + 'adfasfdsfadsfas');
client.person.email('arnout@observe.it', function (err) {
expect(err).to.be.instanceOf(Error);
......@@ -71,8 +73,10 @@ describe('FullContact', function () {
expect(api[prop]).to.equal(0);
});
api.person.email('arnout@observe.it', function email(err, data) {
if (err) return done(err);
api.person.email('arnout@observe.it', function email(err) {
if (err) {
return done(err);
}
['remaining', 'ratelimit', 'ratereset'].forEach(function (prop) {
expect(api[prop]).to.not.equal(0);
......@@ -87,7 +91,9 @@ describe('FullContact', function () {
var remaining = api.remaining;
api.person.email('arnout@observe.it', function email(err) {
if (err) return done(err);
if (err) {
return done(err);
}
//
// The value should be same as before or below
......
describe('FullContact.Location', function () {
'use strict';
var FullContact = require('../')
, chai = require('chai')
, expect = chai.expect;
var FullContact = require('../');
var chai = require('chai');
chai.Assertion.includeStack = true;
chai.config.includeStack = true;
//
// The API key we use for testing.
//
var key = process.env.API_KEY;
if (!key) throw new Error('Please provide your API using the API_KEY env variable.');
if (!key) {
throw new Error('Please provide your API using the API_KEY env variable.');
}
//
// Some of the requests take a really long time, so set a really long timeout
......
describe('FullContact.Name', function () {
'use strict';
var FullContact = require('../')
, chai = require('chai')
, expect = chai.expect;
var FullContact = require('../');
var chai = require('chai');
chai.Assertion.includeStack = true;
chai.config.includeStack = true;
//
// The API key we use for testing.
//
var key = process.env.API_KEY;
if (!key) throw new Error('Please provide your API using the API_KEY env variable.');
if (!key) {
throw new Error('Please provide your API using the API_KEY env variable.');
}
//
// Some of the requests take a really long time, so set a really long timeout
......
describe('FullContact.Person', function () {
'use strict';
var FullContact = require('../')
, chai = require('chai')
, expect = chai.expect;
var FullContact = require('../');
var chai = require('chai');
chai.Assertion.includeStack = true;
chai.config.includeStack = true;
//
// The API key we use for testing.
//
var key = process.env.API_KEY;
if (!key) throw new Error('Please provide your API using the API_KEY env variable.');
if (!key) {
throw new Error('Please provide your API using the API_KEY env variable.');
}
//
// Some of the requests take a really long time, so set a really long timeout
......@@ -41,9 +42,9 @@ describe('FullContact.Person', function () {
describe('#md5', function () {
var md5 = require('crypto').createHash('md5')
.update('arnout@observe.it')
.digest('hex')
.toString();
.update('arnout@observe.it')
.digest('hex')
.toString();
it('retrieves data by md5 e-mail', function (done) {
api.person.md5(md5, done);
......@@ -68,7 +69,7 @@ describe('FullContact.Person', function () {
it('provides the proper casing');
});
describe('#facebookId', function () {
describe('#facebookId', function () {
it('retrieves data by facebook id', function (done) {
api.person.facebookId('1844599060', done);
});
......
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