|
|
|
/** |
|
* Module dependencies |
|
*/ |
|
|
|
var agent = require('superagent') |
|
, prompt = require('prompt') |
|
, through = require('through') |
|
, format = require('util').format |
|
, fs = require('fs') |
|
|
|
var ENDPOINT = process.env.PERKA_ENDPOINT; |
|
var RESUME = process.env.RESUME; |
|
|
|
var print = console.log; |
|
var argv = process.argv; |
|
var exit = process.exit; |
|
var fread = fs.readFileSync; |
|
var stdin = process.stdin; |
|
var stdout = process.stdout; |
|
var stderr = process.stderr; |
|
var payload = null; |
|
var resume = []; |
|
|
|
void function main () { |
|
var stream = null; |
|
|
|
if (null == ENDPOINT) { |
|
e("`PERKA_ENDPOINT' not defined"); |
|
} else { |
|
info("PERKA_ENDPOINT=%s", ENDPOINT); |
|
} |
|
|
|
print(); |
|
info("... Reading resume from '%s'", RESUME); |
|
|
|
(stream = fs.createReadStream(RESUME, {encoding: 'base64'})) |
|
.on('error', function (err) { |
|
e(err.message); |
|
exit(1); |
|
}) |
|
.on('readable', function () { |
|
var chunk = null; |
|
while (null != (chunk = stream.read())) { |
|
resume.push(String(chunk)); |
|
} |
|
}) |
|
.on('end', function () { |
|
resume = resume.join(''); |
|
info("... Resume buffered !"); |
|
print(); |
|
init(); |
|
}); |
|
|
|
}(); |
|
|
|
/** |
|
* Begins questionnaire |
|
* prompts |
|
*/ |
|
|
|
function init () { |
|
var schema = { properties: {} }; |
|
|
|
q('first_name', "What's your first name?", true); |
|
q('last_name', "What's your last name?", true); |
|
q('email', "What's your email?", true); |
|
q('position_id', |
|
|
|
"Id of position you are applying for "+ |
|
"(Don't want to be placed in a bucket? Use GENERALIST)?", |
|
true); |
|
|
|
q('explanation', "How did you make the API request?", false); |
|
|
|
q('projects', |
|
"Links to your GitHub profile, personal projects, "+ |
|
"or other awesome things that you've done?", |
|
false, |
|
function (v) { |
|
return v.split(','); |
|
}); |
|
|
|
q('source', |
|
"How did you find Perka? (avid user, talent scout, "+ |
|
"friend, Hacker School…)?", |
|
false); |
|
|
|
prompt.start({message: " ", delimiter: "> "}); |
|
prompt.get(schema, done); |
|
|
|
function q (f, d, r, b) { |
|
return schema.properties[f] = { |
|
name: f, description: d.cyan, type: 'string', |
|
require: (null == r ? true : r), |
|
before: b || function (v) { return v; } |
|
}; |
|
} |
|
|
|
function done (err, results) { |
|
if (null != err) { |
|
e(err.message); |
|
exit(1); |
|
} |
|
|
|
payload = results; |
|
// for preview |
|
payload.resume = "<Resume "+ resume.length +">"; |
|
|
|
print(); |
|
prompt.get([ |
|
'\n'+ JSON.stringify(payload, null, ' ').cyan + |
|
' Is this okay ? (yes) \n\n'.green |
|
], function (err, res) { |
|
var anser = null; |
|
|
|
if (null != err) { |
|
e(err.message); |
|
exit(1); |
|
} |
|
|
|
answer = res[Object.keys(res)[0]]; |
|
|
|
if ('n' != answer.toLowerCase()) { |
|
payload.resume = resume; |
|
request(payload); |
|
} else { |
|
exit(0); |
|
} |
|
}); |
|
|
|
} |
|
} |
|
|
|
/** |
|
* Makes a POST request of a |
|
* payload to the `PERKA_ENDPOINT' |
|
*/ |
|
|
|
function request (data) { |
|
agent |
|
.post(ENDPOINT) |
|
.send(data) |
|
.end(function (err, res) { |
|
if (null != err) { |
|
e(err.message); |
|
exit(1); |
|
} |
|
|
|
info("... Resume sent ! '%s' => '%s'", RESUME, ENDPOINT); |
|
}); |
|
} |
|
|
|
/** |
|
* Prints a message prefixed |
|
* with 'error: ' to `stderr' |
|
*/ |
|
|
|
function e () { |
|
stderr.write("error: "); |
|
stderr.write(format.apply(null, arguments)); |
|
stderr.write('\n'); |
|
} |
|
|
|
/** |
|
* Prints info to stdout |
|
*/ |
|
|
|
function info () { |
|
stdout.write(" "); |
|
stdout.write(format.apply(null, arguments)); |
|
stdout.write('\n'); |
|
} |