Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
clearbit
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
prospector
clearbit
Commits
752f0548
Commit
752f0548
authored
Jan 20, 2015
by
Ben Drucker
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add watchlist API support
Closes #7
parent
001b6908
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
208 additions
and
111 deletions
+208
-111
.jshintrc
.jshintrc
+0
-1
client.js
src/client.js
+19
-19
company.js
src/company.js
+15
-14
person.js
src/person.js
+26
-23
resource.js
src/resource.js
+63
-47
watchlist.js
src/watchlist.js
+30
-0
client.js
test/client.js
+7
-7
watchlist.json
test/fixtures/watchlist.json
+12
-0
watchlist.js
test/watchlist.js
+36
-0
No files found.
.jshintrc
View file @
752f0548
...
...
@@ -6,7 +6,6 @@
"eqeqeq": true,
"immed": true,
"indent": 2,
"latedef": true,
"newcap": true,
"noarg": true,
"quotmark": "single",
...
...
src/client.js
View file @
752f0548
...
...
@@ -12,52 +12,52 @@ var pkg = require('../package.json');
function
ClearbitClient
(
config
)
{
config
=
config
||
{};
assert
(
this
instanceof
ClearbitClient
,
'Client must be called with new'
);
assert
(
!!
config
.
key
,
'An API key must be provided'
);
this
.
key
=
config
.
key
;
this
.
key
=
config
.
key
||
process
.
env
.
CLEARBIT_KEY
;
assert
(
!!
this
.
key
,
'An API key must be provided'
);
this
.
Company
=
require
(
'./company'
).
Company
(
this
);
this
.
Person
=
require
(
'./person'
).
Person
(
this
);
this
.
PersonCompany
=
require
(
'./person'
).
PersonCompany
(
this
);
this
.
Company
=
require
(
'./company'
)(
this
);
this
.
Watchlist
=
require
(
'./watchlist'
).
Watchlist
(
this
);
this
.
WatchlistEntity
=
require
(
'./watchlist'
).
WatchlistEntity
(
this
);
this
.
WatchlistIndividual
=
require
(
'./watchlist'
).
WatchlistIndividual
(
this
);
}
var
base
=
'https://%s%s.clearbit.com/v%s'
;
ClearbitClient
.
prototype
.
base
=
function
(
options
)
{
var
ENDPOINT
=
'https://%s%s.clearbit.com/v%s'
;
ClearbitClient
.
prototype
.
endpoint
=
function
(
options
)
{
options
=
_
.
defaults
(
options
,
{
version
:
'1'
,
stream
:
false
});
assert
(
options
.
api
,
'An API must be specified'
);
return
util
.
format
.
apply
(
util
,
[
base
,
return
util
.
format
(
ENDPOINT
,
options
.
api
,
options
.
stream
?
'-stream'
:
''
,
options
.
version
]
);
);
};
ClearbitClient
.
prototype
.
url
=
function
(
options
)
{
_
.
defaults
(
options
,
{
path
:
''
});
return
this
.
base
(
options
)
+
options
.
path
;
return
this
.
endpoint
(
options
)
+
options
.
path
;
};
function
generateQuery
()
{
var
query
=
_
.
omit
(
_
.
extend
.
apply
(
_
,
[{}].
concat
([].
slice
.
apply
(
arguments
))),
_
.
isUndefined
);
return
_
.
isEmpty
(
query
)
?
undefined
:
query
;
}
ClearbitClient
.
prototype
.
request
=
function
(
options
)
{
options
=
_
.
defaults
(
options
,
{
method
:
'get'
,
query
:
{}
method
:
'get'
});
return
needle
.
requestAsync
(
options
.
method
,
this
.
url
(
options
),
generateQuery
({
webhook_id
:
options
.
webhook_id
},
options
.
query
),
options
.
query
,
{
timeout
:
options
.
stream
?
60000
:
10000
,
username
:
this
.
key
,
...
...
src/company.js
View file @
752f0548
...
...
@@ -4,19 +4,20 @@ var assert = require('assert');
var
resource
=
require
(
'./resource'
);
var
_
=
require
(
'lodash'
);
module
.
exports
=
resource
.
create
(
'Company'
,
{
api
:
'company'
,
path
:
'/companies/domain/<%= domain %>'
})
.
on
(
'preFind'
,
function
(
options
)
{
exports
.
Company
=
resource
.
create
(
'Company'
,
{
api
:
'company'
})
.
extend
({
flag
:
function
(
options
){
return
this
.
constructor
.
post
(
'/companies/'
+
this
.
id
+
'/flag'
,
options
);
}
},
{
find
:
function
(
options
)
{
options
=
options
||
{};
assert
(
options
.
domain
,
'A domain must be provided'
);
}).
include
({
flag
:
function
(
params
,
options
){
return
this
.
client
.
request
(
_
.
extend
({
api
:
this
.
_options
.
api
,
method
:
'post'
,
path
:
_
.
template
(
'/companies/<%= id %>/flag'
,
this
),
query
:
params
||
{}
},
options
));
return
this
.
get
(
'/companies/domain/'
+
options
.
domain
,
_
.
omit
(
options
,
'domain'
)
);
}
});
});
src/person.js
View file @
752f0548
...
...
@@ -4,30 +4,33 @@ var assert = require('assert');
var
resource
=
require
(
'./resource'
);
var
_
=
require
(
'lodash'
);
function
requireEmail
(
options
)
{
exports
.
Person
=
resource
.
create
(
'Person'
,
{
api
:
'person'
})
.
extend
({
flag
:
function
(
options
){
return
this
.
constructor
.
post
(
'/people/'
+
this
.
id
+
'/flag'
,
options
);
}
},
{
find
:
function
(
options
){
options
=
options
||
{};
assert
(
options
.
email
,
'An email must be provided'
);
}
exports
.
Person
=
resource
.
create
(
'Person'
,
{
api
:
'person'
,
path
:
'/people/email/<%= email %>'
,
queryKeys
:
'subscribe'
})
.
on
(
'preFind'
,
requireEmail
)
.
include
({
flag
:
function
(
params
,
options
){
return
this
.
client
.
request
(
_
.
extend
({
api
:
this
.
_options
.
api
,
method
:
'post'
,
path
:
_
.
template
(
'/people/<%= id %>/flag'
,
this
),
query
:
params
||
{}
},
options
));
return
this
.
get
(
'/people/email/'
+
options
.
email
,
_
.
omit
(
options
,
'email'
)
);
}
});
});
exports
.
PersonCompany
=
resource
.
create
(
'PersonCompany'
,
{
api
:
'person'
})
.
extend
(
null
,
{
find
:
function
(
options
){
options
=
options
||
{};
assert
(
options
.
email
,
'An email must be provided'
);
exports
.
PersonCompany
=
resource
.
create
(
'PersonCompany'
,
{
api
:
'person'
,
path
:
'/combined/email/<%= email %>'
,
queryKeys
:
'subscribe'
})
.
on
(
'preFind'
,
requireEmail
);
return
this
.
get
(
'/combined/email/'
+
options
.
email
,
_
.
omit
(
options
,
'email'
)
);
}
}
);
src/resource.js
View file @
752f0548
'use strict'
;
var
createError
=
require
(
'create-error'
);
var
EventEmitter
=
require
(
'events'
).
EventEmitter
;
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
)
{
_
.
extend
(
this
,
data
);
}
ClearbitResource
.
find
=
Promise
.
method
(
function
(
options
)
{
options
=
options
||
/* istanbul ignore next */
{};
this
.
emit
(
'preFind'
,
options
);
return
this
.
client
.
request
(
_
.
extend
({
api
:
this
.
_options
.
api
,
path
:
this
.
_options
.
template
(
options
),
query
:
_
.
pick
(
options
,
this
.
_options
.
queryKeys
)
},
options
)
)
ClearbitResource
.
get
=
function
(
path
,
options
)
{
options
=
_
.
extend
({
path
:
path
,
method
:
'get'
,
query
:
extractParams
(
options
)
},
this
.
options
,
options
);
return
this
.
client
.
request
(
options
)
.
bind
(
this
)
.
then
(
function
(
data
)
{
return
new
this
(
_
.
extend
({},
data
,
{
_options
:
this
.
_options
,
client
:
this
.
client
})
);
})
.
then
(
cast
)
.
catch
(
isQueued
,
function
()
{
throw
new
this
.
QueuedError
(
this
.
_
name
+
' lookup queued'
);
throw
new
this
.
QueuedError
(
this
.
name
+
' lookup queued'
);
})
.
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'
)
};
}
ClearbitResource
.
post
=
function
(
path
,
options
)
{
options
=
_
.
extend
({
path
:
path
,
method
:
'post'
,
query
:
extractParams
(
options
)
},
this
.
options
,
options
);
return
this
.
client
.
request
(
options
)
.
bind
(
this
)
.
then
(
cast
)
.
catch
(
isUnknownRecord
,
function
()
{
throw
new
this
.
NotFoundError
(
this
.
name
+
' not found'
);
});
};
exports
.
create
=
function
(
name
,
options
)
{
var
Resource
=
function
()
{
ClearbitResource
.
apply
(
this
,
arguments
);
};
_
.
extend
(
Resource
,
new
EventEmitter
(),
EventEmitter
.
prototype
,
ClearbitResource
,
createErrors
(
name
),
{
_name
:
name
,
_options
:
_
.
extend
({},
options
,
{
template
:
_
.
template
(
options
.
path
)
})
_
.
extend
(
Resource
,
ClearbitResource
,
createErrors
(
name
),
{
name
:
name
,
options
:
options
});
return
_
.
extend
(
function
(
client
)
{
...
...
@@ -67,14 +56,41 @@ exports.create = function (name, options) {
});
},
{
on
:
function
()
{
Resource
.
on
.
apply
(
Resource
,
arguments
);
return
this
;
},
include
:
function
(
props
)
{
_
.
extend
(
Resource
.
prototype
,
props
);
extend
:
function
(
proto
,
ctor
)
{
_
.
extend
(
Resource
.
prototype
,
proto
);
_
.
extend
(
Resource
,
ctor
);
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
;
}
src/watchlist.js
0 → 100644
View file @
752f0548
'use strict'
;
var
resource
=
require
(
'./resource'
);
exports
.
Watchlist
=
resource
.
create
(
'Watchlist'
,
{
api
:
'watchlist'
})
.
extend
(
null
,
{
search
:
function
(
options
)
{
return
this
.
post
(
'/search/all'
,
options
);
}
});
exports
.
WatchlistIndividual
=
resource
.
create
(
'WatchlistIndividual'
,
{
api
:
'watchlist'
})
.
extend
(
null
,
{
search
:
function
(
options
)
{
return
this
.
post
(
'/search/individuals'
,
options
);
}
});
exports
.
WatchlistEntity
=
resource
.
create
(
'WatchlistEntity'
,
{
api
:
'watchlist'
})
.
extend
(
null
,
{
search
:
function
(
options
)
{
return
this
.
post
(
'/search/entities'
,
options
);
}
});
test/client.js
View file @
752f0548
...
...
@@ -34,22 +34,22 @@ describe('Client', function () {
});
describe
(
'#
base
'
,
function
()
{
describe
(
'#
endpoint
'
,
function
()
{
it
(
'requires an API'
,
function
()
{
expect
(
client
.
base
.
bind
(
client
,
{}))
expect
(
client
.
endpoint
.
bind
(
client
,
{}))
.
to
.
throw
(
/API must be specified/
);
});
it
(
'can generate the default
base
'
,
function
()
{
expect
(
client
.
base
({
it
(
'can generate the default
endpoint
'
,
function
()
{
expect
(
client
.
endpoint
({
api
:
'person'
}))
.
to
.
equal
(
'https://person.clearbit.com/v1'
);
});
it
(
'can generate a streaming
base
'
,
function
()
{
expect
(
client
.
base
({
it
(
'can generate a streaming
endpoint
'
,
function
()
{
expect
(
client
.
endpoint
({
api
:
'person'
,
stream
:
true
}))
...
...
@@ -57,7 +57,7 @@ describe('Client', function () {
});
it
(
'can set a custom version'
,
function
()
{
expect
(
client
.
base
({
expect
(
client
.
endpoint
({
api
:
'person'
,
version
:
'2'
}))
...
...
test/fixtures/watchlist.json
0 → 100644
View file @
752f0548
[
{
"id"
:
"6ffa17f3-2653-485f-9129-73bdaa88087a"
,
"name"
:
{
"fullName"
:
"Joe Schmo"
},
"addresses"
:
[],
"list"
:
"ofac_sdn"
,
"remarks"
:
null
,
"type"
:
"entity"
}
]
test/watchlist.js
0 → 100644
View file @
752f0548
'use strict'
;
var
expect
=
require
(
'chai'
).
expect
;
var
nock
=
require
(
'nock'
);
var
Watchlist
=
require
(
'../'
)(
'k'
).
Watchlist
;
describe
(
'Watchlist'
,
function
()
{
var
mock
;
before
(
function
()
{
mock
=
nock
(
'https://watchlist.clearbit.com'
);
});
after
(
nock
.
cleanAll
);
afterEach
(
function
()
{
mock
.
done
();
});
describe
(
'Watchlist#search'
,
function
()
{
var
watchlist
=
require
(
'./fixtures/watchlist'
);
it
(
'can search a watchlist by name'
,
function
()
{
mock
.
post
(
'/v1/search/all'
)
.
reply
(
200
,
watchlist
);
return
Watchlist
.
search
({
name
:
'Joe'
})
.
then
(
function
(
watchlist
)
{
expect
(
watchlist
[
0
])
.
to
.
be
.
an
.
instanceOf
(
Watchlist
)
.
and
.
have
.
property
(
'id'
,
watchlist
.
id
);
});
});
});
});
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment