Skip to content

Instantly share code, notes, and snippets.

@grgaortiz
Created September 10, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grgaortiz/a68943824d1f29517684 to your computer and use it in GitHub Desktop.
Save grgaortiz/a68943824d1f29517684 to your computer and use it in GitHub Desktop.
AngelList NPM package updates
var config = require('./config'),
request = require('request'),
urlHelper = require('./util/url_helper');
function init(clientID, secret) {
config.clientID = clientID;
config.secret = secret;
}
function setAccessToken(token) {
config.access_token = token;
}
function getAuthUrl() {
return urlHelper.createAuthUrl();
}
function requestAccessToken(code, callback) {
var url = urlHelper.createAccessTokenRequestUrl(code);
createPostRequest(url, '', callback);
}
function getMe(callback) {
// If the access token isn't set, return an error
if(!config.hasOwnProperty('access_token') || config.secret === null || config.secret === '') {
callback({'error':'no access token'}, null);
} else {
var url = urlHelper.createMeUrl();
createRequest(url, callback);
}
}
function search(query, callback) {
var url = urlHelper.createSearchUrl(query);
createRequest(url, callback);
}
function searchBySlug(query, callback) {
var url = urlHelper.createSearchSlugUrl(query);
createRequest(url, callback);
}
function searchByStartupTag(tagID, page, callback) {
var url = urlHelper.createStartupTagUrl(tagID, page);
createRequest(url, callback);
}
function getUserById(angelID, callback) {
var url = urlHelper.createUserUrl(angelID);
createRequest(url, callback);
}
function getUsersById(angelIDs, callback) {
var url = urlHelper.createUsersBatchUrl(angelIDs);
createRequest(url, callback);
}
function createRequest(url, callback) {
request(url, function (error, response, body) {
if (response.statusCode == 200) {
callback(error, JSON.parse(body));
} else {
callback(error, body);
}
});
}
function createPostRequest(url, body, callback) {
request.post({
headers: {'content-type' : 'application/x-www-form-urlencoded'},
url: url,
body: body
}, function(error, response, body){
if (response.statusCode == 200) {
callback(error, JSON.parse(body));
} else {
callback(error, body);
}
});
}
module.exports = {
init: function(clientID, secret) {
return init(clientID, secret);
},
setAccessToken: function(token) {
return setAccessToken(token);
},
getAuthUrl: function() {
return getAuthUrl();
},
requestAccessToken: function(code, callback) {
return requestAccessToken(code, callback);
},
getMe: function(callback) {
return getMe(callback);
},
search: function(query, callback) {
return search(query, callback);
},
searchBySlug: function(query, callback) {
return searchBySlug(query, callback);
},
searchByStartupTag: function(tagID, page, callback) {
return searchByStartupTag(tagID, page, callback);
},
getUserById: function(angelID, callback) {
return getUserById(angelID, callback);
},
getUsersById: function(angelIDs, callback) {
return getUsersById(angelIDs, callback);
}
}
var config = require('../config'),
_ = require('underscore');
function createAuthUrl() {
// URL should look like: https://angel.co/api/oauth/authorize?client_id=CLIENTID&scope=email&response_type=code
return 'https://angel.co/api/oauth/authorize?client_id='
+ config.clientID
+ '&scope=email&response_type=code';
}
function createAccessTokenRequestUrl(code) {
// URL should look like: 'https://angel.co/api/oauth/token?client_id=CLIENTID&client_secret=SECRET&code=CODE&grant_type=authorization_code';
return 'https://angel.co/api/oauth/token?client_id='
+ config.clientID
+ '&client_secret='
+ config.secret + '&code='
+ code
+ '&grant_type=authorization_code';
}
function createMeUrl() {
// URL should look like: 'https://api.angel.co/1/me?access_token=TOKEN;
return 'https://api.angel.co/1/me?access_token='
+ config.secret;
}
function createSearchUrl(query) {
// URL should look like: 'https://api.angel.co/1/search?query=QUERY'
return 'https://api.angel.co/1/search?query='
+ encodeURIComponent(query) +
'&access_token='
+ config.secret;
}
function createSearchSlugUrl(query) {
// URL should look like: 'https://api.angel.co/1/search/slugs?query=QUERY'
return 'https://api.angel.co/1/search/slugs?query='
+ encodeURIComponent(query) +
'&access_token='
+ config.secret;
}
function createStartupTagUrl(tagID, page) {
// URL should look like: 'https://api.angel.co/1/search/slugs?query=QUERY'
return 'https://api.angel.co/1/tags/'
+ tagID +
'/startups?order=popularity&page='
+ page +
'&access_token='
+ config.secret;
}
function createUserUrl(userID) {
// URL should look like: 'https://api.angel.co/1/users/ANGELLISTID';
return 'https://api.angel.co/1/users/'
+ userID;
}
function createUsersBatchUrl(userIDs) {
// Map array of IDs to a comma-separated string
var idList = _.reduce(userIDs, function(initial, userID){ return initial + userID + ','; }, '');
// URL should look like: 'https://api.angel.co/1/users/batch?ids=ID1,ID2';
return 'https://api.angel.co/1/users/batch?ids='
+ idList;
}
module.exports = {
createAuthUrl: function() {
return createAuthUrl();
},
createAccessTokenRequestUrl: function(code) {
return createAccessTokenRequestUrl(code);
},
createMeUrl: function() {
return createMeUrl();
},
createSearchUrl: function(query) {
return createSearchUrl(query);
},
createSearchSlugUrl: function(query) {
return createSearchSlugUrl(query);
},
createStartupTagUrl: function(tagID, page) {
return createStartupTagUrl(tagID, page);
},
createUserUrl: function(userID) {
return createUserUrl(userID);
},
createUsersBatchUrl: function(userIDs) {
return createUsersBatchUrl(userIDs);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment