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

Commit b8b72ac

Browse files
committed
return results and print them in a nice way
1 parent 94bfc69 commit b8b72ac

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

shell.js

+24-6
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,26 @@ function WebShell(stream) {
223223
return headers;
224224
}
225225

226-
doHttpReq = function(verb, urlStr, result) {
227-
result = result || {};
226+
function ResultHolder(verb, url) {
227+
this.verb = verb;
228+
this.url = url;
229+
}
230+
var oldToString = ResultHolder.prototype.toString;
231+
ResultHolder.prototype = {
232+
toString: function() {
233+
return "[Pending]";
234+
},
235+
inspect: function() {
236+
return this.verb + " " + this.url;
237+
}
238+
};
239+
_.define(ResultHolder.prototype, 'finalize', function() {
240+
_.define(this, 'toString', oldToString);
241+
_.define(this, 'inspect', null);
242+
});
243+
244+
doHttpReq = function(verb, urlStr) {
245+
result = new ResultHolder(verb, urlStr);
228246
var u = parseURL(urlStr);
229247
var client = http.createClient(u.port, u.hostname, u.protocol === 'https:');
230248
var jsonHeaders = ['application/json', 'text/x-json'];
@@ -290,15 +308,15 @@ function WebShell(stream) {
290308
}
291309

292310
_.extend(result, {raw: ctx.$_.raw, headers: ctx.$_.headers, statusCode: ctx.$_.status, json: ctx.$_.json});
311+
result.finalize();
293312
});
294313
});
314+
return result;
295315
};
296316

297317
_.each(verbs, function (v) {
298-
$_[v.toLowerCase()] = function(url, result) {
299-
var out = result || {};
300-
doHttpReq(v, url, out);
301-
return out;
318+
$_[v.toLowerCase()] = function(url) {
319+
return doHttpReq(v, url);
302320
};
303321
});
304322

underscore.js

+6
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,12 @@
568568
return typeof obj == 'undefined';
569569
};
570570

571+
_.define = function(prototype, property, value) {
572+
if (!prototype.hasOwnProperty(property)) {
573+
Object.defineProperty(prototype, property, { value:value, enumerable:false });
574+
}
575+
};
576+
571577
// -------------------------- Utility Functions: ----------------------------
572578

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

0 commit comments

Comments
 (0)