Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adding response accumulator.

Plan for
 - JSON response parsing
 - XML response parsing
 - YAML response parsing

* Updated test demo
  • Loading branch information...
commit f661845ef88ea2bb39b922db795388980afb86dd 1 parent c94558b
@maxpert authored
Showing with 49 additions and 3 deletions.
  1. +12 −1 lib/Core.js
  2. +26 −0 lib/Response.js
  3. +5 −0 lib/Reston.js
  4. +6 −2 test.js
View
13 lib/Core.js
@@ -1,3 +1,4 @@
+//Says for itself
var mixin = function(obj, target){
target = target || {};
for(var p in target){
@@ -6,11 +7,20 @@ var mixin = function(obj, target){
return obj;
};
-
+//Convert arguments to array
var arrize = function(ary, from){
return Array.prototype.slice.call(ary, from || 0);
};
+//Bind proxy objects to function
+function glue(f, obj){
+ var oargs = arrize(arguments, 2);
+ return function(){
+ var iargs = arrize(arguments);
+ return f.apply && f.apply(obj, oargs.concat(iargs));
+ }
+}
+
var node_ver = null;
(function(){
if( node_ver ) return node_ver;
@@ -29,6 +39,7 @@ var node_ver = null;
var core = {
'mixin': mixin,
'arrize': arrize,
+ 'glue': glue,
'node_ver': node_ver
};
View
26 lib/Response.js
@@ -0,0 +1,26 @@
+var core = require('./Core'),
+ evEmitter = require('events').EventEmitter;
+
+var ResponseAccumulator = function (reston, encoding){
+ var me=this;
+
+ me.data = '';
+ me.headers = {};
+
+ encoding = encoding || 'utf8';
+
+ reston.on('start', function(resp, req){
+ core.mixin(me.headers, resp.headers);
+ });
+ reston.on('data', function(chunk){
+ me.data = me.data + chunk.toString(encoding);
+ });
+};
+
+var proto = ResponseAccumulator.prototype;
+
+if(module){
+ module.exports = ResponseAccumulator;
+}else{
+ exports = ResponseAccumulator;
+}
View
5 lib/Reston.js
@@ -1,6 +1,7 @@
var FileStream = require('./FileStream'),
core = require('./Core'),
MultipartWriter = require('./MultipartWriter'),
+ Accum = require('./Response'),
evEmitter = require('events').EventEmitter;
http = require('http'),
Url = require('url'),
@@ -183,6 +184,10 @@ core.mixin(Reston, {
return {'Authorization': 'Basic '+(new Buffer(user+":"+pass)).toString("base64") };
}
},
+ accumulate: function(reston, encoding){
+ return new Accum(reston, encoding);
+ },
+ Accumulator: Accum,
File: FileStream,
//Warning: will be depriciated to File soon
View
8 test.js
@@ -23,7 +23,11 @@ var request_object = function(method, address, headers){
var data = '';
var req = Reston.get('http://search.twitter.com/search.json');
-req.on('data', function(c){
- console.log(c.toString());
+var rep_ac = new Reston.Accumulator(req, 'utf8');
+req.on(Reston.Events.Success, function(c){
+ console.log(rep_ac.data);
+});
+req.on(Reston.Events.Error, function(){
+ console.log("Something went wrong");
});
req.send({q: 'nodejs'});
Please sign in to comment.
Something went wrong with that request. Please try again.