From b8b72ac8a90bec49e6d87815ea6b9d8a5e85d9f1 Mon Sep 17 00:00:00 2001 From: Evan Haas Date: Tue, 10 Aug 2010 16:18:45 -0700 Subject: [PATCH] return results and print them in a nice way --- shell.js | 30 ++++++++++++++++++++++++------ underscore.js | 6 ++++++ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/shell.js b/shell.js index e7fc043..83eefad 100644 --- a/shell.js +++ b/shell.js @@ -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); }; }); diff --git a/underscore.js b/underscore.js index 79a3bd6..b2e4b6f 100644 --- a/underscore.js +++ b/underscore.js @@ -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