Permalink
Browse files

return results and print them in a nice way

  • Loading branch information...
1 parent 94bfc69 commit b8b72ac8a90bec49e6d87815ea6b9d8a5e85d9f1 Evan Haas committed Aug 10, 2010
Showing with 30 additions and 6 deletions.
  1. +24 −6 shell.js
  2. +6 −0 underscore.js
View
@@ -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'];
@@ -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);
};
});
View
@@ -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

0 comments on commit b8b72ac

Please sign in to comment.