Skip to content

Instantly share code, notes, and snippets.

@alexanderankin
Last active March 9, 2016 17:20
Show Gist options
  • Save alexanderankin/0038d558fe6487a56ebb to your computer and use it in GitHub Desktop.
Save alexanderankin/0038d558fe6487a56ebb to your computer and use it in GitHub Desktop.
golf
var async = require('async');
var _ = require('underscore');
var phone = require('phone');
var request = require('request');
var ph = function (s) {
s += "";
return phone(s)[0];
}
var username = "+12623144291";
var password = "pass";
function provideUsers(callback, paths) {
var paths = paths || ["/admins", "/applicants", "/counselors"];
var obj = {};
async.each(paths, function (path) {
console.log(path)
request('http://ankin.info'
+ path + '.json', function(error, response, body) {
// callback(JSON.parse(response.body));
obj = _.extend(obj, JSON.parse(response.body));
// console.log("updating object",
// (JSON.stringify(obj).substring(0, 100)))
callback();
});
}, function (err) {
console.log("calling callback")
callback(obj);
});
}
/**
* can you have in the token roles instead of role.
*
* finds all users with phone number, accumulates
* password array.
*/
function gtoken(uname, pword, cb) {
console.log("gtoken calling")
return (function (uname, pword, cb) {
console.log("running anon")
provideUsers(function(allusers) {
console.log("all users is " + allusers)
uname = ph(uname);
var roles = [];
var passwords = [];
for (role in allusers) {
// console.log(role);
// if (role !== "admins") continue;
var rolegroup = allusers[role];
for (userid in rolegroup) {
var user = (ph(userid) ||
ph(rolegroup[userid]["phone"]));
var pass = rolegroup[userid]["password"];
if (user === uname) {
roles.push(role);
if (pass) passwords.push(pass);
}
// console.log({ u: user, p: pass });
// allusers.push({ u: user, p: pass });
}
}
console.log("calling callback")
cb({
u: uname,
p: passwords,
r: roles
});
});
})(uname, pword, cb);
}
gtoken(username, password, function (a) {
console.log("gtoken returned");
console.log(a);
});
// var a = {
// "key": "value"
// }
// for (key in a) {
// console.log(key);
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment