Skip to content
This repository has been archived by the owner on Feb 8, 2018. It is now read-only.

Commit

Permalink
return results and print them in a nice way
Browse files Browse the repository at this point in the history
  • Loading branch information
Evan Haas committed Aug 10, 2010
1 parent 94bfc69 commit b8b72ac
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
30 changes: 24 additions & 6 deletions shell.js
Expand Up @@ -223,8 +223,26 @@ function WebShell(stream) {
return headers;
}

doHttpReq = function(verb, urlStr, result) {
result = result || {};
function ResultHolder(verb, url) {
this.verb = verb;
this.url = url;
}
var oldToString = ResultHolder.prototype.toString;
ResultHolder.prototype = {
toString: function() {
return "[Pending]";
},
inspect: function() {
return this.verb + " " + this.url;
}
};
_.define(ResultHolder.prototype, 'finalize', function() {
_.define(this, 'toString', oldToString);
_.define(this, 'inspect', null);
});

doHttpReq = function(verb, urlStr) {
result = new ResultHolder(verb, urlStr);
var u = parseURL(urlStr);
var client = http.createClient(u.port, u.hostname, u.protocol === 'https:');
var jsonHeaders = ['application/json', 'text/x-json'];
Expand Down Expand Up @@ -290,15 +308,15 @@ function WebShell(stream) {
}

_.extend(result, {raw: ctx.$_.raw, headers: ctx.$_.headers, statusCode: ctx.$_.status, json: ctx.$_.json});
result.finalize();
});
});
return result;
};

_.each(verbs, function (v) {
$_[v.toLowerCase()] = function(url, result) {
var out = result || {};
doHttpReq(v, url, out);
return out;
$_[v.toLowerCase()] = function(url) {
return doHttpReq(v, url);
};
});

Expand Down
6 changes: 6 additions & 0 deletions underscore.js
Expand Up @@ -568,6 +568,12 @@
return typeof obj == 'undefined';
};

_.define = function(prototype, property, value) {
if (!prototype.hasOwnProperty(property)) {
Object.defineProperty(prototype, property, { value:value, enumerable:false });
}
};

// -------------------------- Utility Functions: ----------------------------

// Run Underscore.js in noConflict mode, returning the '_' variable to its
Expand Down

0 comments on commit b8b72ac

Please sign in to comment.