Skip to content

Instantly share code, notes, and snippets.

@sineer
Last active August 29, 2015 14:07
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 sineer/d7429677a85db97f5dda to your computer and use it in GitHub Desktop.
Save sineer/d7429677a85db97f5dda to your computer and use it in GitHub Desktop.
// 1st thing I should've done is write down the question
// Q: Do REST call, process response body and print O.K if all params are true else print Error: some_param and exit.
// Response format: multi-lines ascii: "- attr_name_string : bool"
// 2nd, beside choosing just about any other language than node.js because it is very hard to do this in node...
// find a proper example of node.js http.get client code such as:
// http://stackoverflow.com/questions/6968448/where-is-body-in-a-nodejs-http-get-response
// and remember that res.on('data', function(chunk) { ... })
// FYI, Not true what I said about console.dir I mixed up with Python dir() :-/
// node.js HTTP Client/Server API
var http = require("http");
// Call cb with a Status String after doing asynchronous HTTP GET of url
getStatus = function(url, cb) {
var status = "Status O.K." // NOTE: If we receive empty response or no "param" are found we return "STATUS O.K." !
http.get(url, function(res) {
console.log("HTTP response: " + res.statusCode);
res.on('data', function(chunk) {
// This is tricky because http response may call 'data' many times before it call 'end'...
console.log("HTTP DATA chunk: " + chunk);
var processedStatus = _processStatus(chunk);
if( processedStatus ) {
// Non null means we got a new valid Status String
status = processedStatus;
}
});
res.on('end', function() {
// Callback with status string: "Status O.K." or "Status Error: param_name"
cb(url, status);
});
}).on('error', function(e) {
var err = "HTTP error: " + e.message
cb(url, err);
});
}
_processStatus = function(chunk) {
var data = chunk.toString('utf8');
if( data ) {
var lines = data.split('\n');
for(var i=0; i < lines.length; i++) {
if( lines[i] ) {
var rows = lines[i].split(' ');
for( var j=0; j < rows.length; j++) {
if( rows[0] == '-' && rows[2] == ':' ) {
var name = rows[1];
var value = rows[3];
console.log("_processStatus ATTR NAME: " + name + " VALUE: " + value);
// Process ATTR:
if( ! value ) {
return "Status Error: " + name;
} else {
console.log("Status ATTR: " + name + " is O.K.");
return "Status O.K."
}
} else {
// ERR! WRONG FORMAT (return empty string)
return "";
}
}
}
// else ==> skip empty line.
}
}
// else ==> no data, do nothing.
}
// MAIN
var urls = ["http://www.google.com", "http://www.yahoo.com"];
var status = [];
var req_cnt = 0;
var res_cnt = 0;
for(var i=0; i < urls.length; i++) {
req_cnt = req_cnt + 1;
getStatus(urls[i], function(url, res) {
res_cnt = res_cnt + 1;
status[i] = res;
console.log("URL: " + url + " STATUS: " + res);
});
}
var wait_for_all_responses = function(cb) {
if( res_cnt != req_cnt ) {
// Wait a sec...
setTimeout(function() {
wait_for_all_responses(cb);
}, 1000);
} else {
// We're done! CALLBACK
cb();
}
}
var were_done = function() {
console.log("We're done! Here are the " + res_cnt + " URL:STATUS RESPONSE(s):\n");
for(var i=0; i < res_cnt; i++) {
console.log(urls[i] + ":" + status[i]);
}
}
// Wait for all requests to finish:
wait_for_all_responses(were_done);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment