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
5fed700a
Commit
5fed700a
authored
Oct 16, 2014
by
Ben Drucker
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Create resource factory for instantiating resources with minimal code duplication
parent
5ac7b823
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
60 additions
and
0 deletions
+60
-0
resource.js
src/resource.js
+60
-0
No files found.
src/resource.js
0 → 100644
View file @
5fed700a
'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
)
{
return
this
.
client
.
request
(
_
.
extend
({
api
:
this
.
_options
.
api
,
path
:
this
.
_options
.
template
(
options
),
query
:
_
.
pick
(
options
,
this
.
_options
.
queryKeys
)
},
options
))
.
bind
(
this
)
.
then
(
function
(
data
)
{
return
new
this
(
data
);
})
.
catch
(
isQueued
,
function
()
{
throw
new
this
.
QueuedError
(
this
.
_name
+
' lookup queued'
);
})
.
catch
(
isUnknownRecord
,
function
()
{
throw
new
this
.
NotFoundError
(
this
.
_name
+
' not found'
);
});
});
function
createErrors
(
name
)
{
return
{
NotFoundError
:
createError
(
name
+
'NotFoundError'
),
QueuedError
:
createError
(
name
+
'QueuedError'
)
};
}
exports
.
create
=
function
(
name
,
options
)
{
return
function
(
client
)
{
var
Resource
=
function
()
{
ClearbitResource
.
apply
(
this
,
arguments
);
};
_
.
extend
(
Resource
,
new
EventEmitter
(),
ClearbitResource
,
createErrors
(
name
),
{
_name
:
name
,
_options
:
_
.
extend
(
options
,
{
template
:
_
.
template
(
options
.
path
)
}),
client
:
client
});
};
};
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