Skip to content

Instantly share code, notes, and snippets.

@jwerle
Last active January 2, 2016 04:09
Show Gist options
  • Save jwerle/8248522 to your computer and use it in GitHub Desktop.
Save jwerle/8248522 to your computer and use it in GitHub Desktop.
Node.js Application to apply for a job a perka (getperka.com)
node_modules/
resume.pdf
.DS_STORE
/**
* 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');
}
RM ?= rm
NPM ?= npm
NODE ?= node
NODE_MODULES ?= node_modules/
export RESUME ?= resume.pdf
export PERKA_ENDPOINT ?= https://getperka.com/api/2/apply
#export PERKA_ENDPOINT ?= localhost:5000
perka: clean build
build:
$(NPM) i
clean:
$(RM) -rf $(NODE_MODULES)
run:
@$(NODE) index.js
{
"name": "perka-application",
"version": "0.0.1",
"description": "Apply for a job at Perka via Node.js",
"main": "index.js",
"scripts": {
"test": "make test"
},
"keywords": [
"perka",
"job",
"application"
],
"author": "Joseph Werle",
"license": "MIT",
"dependencies": {
"superagent": "~0.15.7",
"prompt": "~0.2.12",
"through": "~2.3.4"
}
}

perka-application

download

$ git clone https://gist.github.com/8248522.git perka-application

build

$ cd perka-application/
$ make build

running

$ make run

license

MIT

/**
* Module dependencies
*/
var http = require('http')
, through = require('through')
, fs = require('fs')
var e = console.error.bind(null, "error: ");
var o = console.log.bind(null, " .. ");
var p = console.log;
var stdout = process.stdout;
var exit = process.exit;
var PORT = process.env.PORT;
var FILE = process.env.FILE;
if (null == FILE) {
throw "error: Missing `FILE'"
}
var server = http.createServer(function (req, res) {
var payload = [];
var body = null;
req
.on('readable', function () {
var chunk = null;
var buf = [];
while (null != (chunk = req.read())) {
buf.push(String(chunk));
}
payload = payload.concat(buf);
})
.on('error', function (err) {
e(err.message)
res.end();
exit(1);
})
.on('end', function () {
var stream = null;
var resume = null;
res.end();
try {
body = JSON.parse(payload.join(''));
} catch (err) {
e(err.message);
exit(1);
}
resume = body.resume;
delete body.resume;
(stream = fs.createWriteStream(FILE, { flags: 'w+' }))
.on('error', function (err) {
e(err.message);
exit(1);
})
.on('finish', function () {
o("write: "+ FILE);
exit(0);
});
o();
p(body);
o();
stream.write(resume, 'base64');
stream.end();
});
}).listen(PORT);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment