Permalink
Browse files

linted, fixed global leak and decoupled http handlers

  • Loading branch information...
1 parent 776b24c commit eb4a0ac1c21cddc077fed18d043fb9f440b4207a @impronunciable committed Apr 17, 2013
Showing with 64 additions and 42 deletions.
  1. +64 −42 lib/tuiter.js
View
@@ -22,6 +22,7 @@ require('superagent-oauth')(request);
function Tuiter(oauth_params){
debug('Creating Tuiter object');
this.OAuth(oauth_params);
+ this.sleep_duration = 0;
debug('Tuiter object created');
return this;
}
@@ -76,8 +77,6 @@ Object.keys(endpoints).forEach(function(key){
*/
var APIRequest = function(endpoint, params, callback) {
- var self = this;
- // supply url vars
var url = endpoint.resource
, url_vars = endpoint.resource.match(/\/:\w+/) || [];
@@ -86,56 +85,80 @@ var APIRequest = function(endpoint, params, callback) {
});
var req = request(endpoint.method, url)
- .sign(self.oa, params.access_token_key || self.access_token_key, params.access_token_secret || self.access_token_secret)
+ .sign(this.oa, params.access_token_key
+ || this.access_token_key,
+ params.access_token_secret
+ || this.access_token_secret)
.query(params)
.set('Accept', '*/*')
.set('User-Agent', 'Tuiter for node.js')
.set('Accept-Encoding', 'gzip, deflate');
- req.req.path = req.req.path.replace(/!/g, '%21').replace(/'/g, '%27')
- .replace(/\(/g, '%28').replace(/\)/g, '%29').replace(/\*/g, '%2A');
+ // handle special characters
+ req.req.path = req.req.path
+ .replace(/!/g, '%21')
+ .replace(/'/g, '%27').replace(/\(/g, '%28')
+ .replace(/\)/g, '%29').replace(/\*/g, '%2A');
if(endpoint.streaming) {
- req.parse(function(){});
- var stream = new NJStream('\r');
- stream.req = req;
- stream.parent = self;
- var args = stream.args = arguments;
- stream.emitAPIEvents();
- stream.autoReconnect();
-
- req.pipe(stream);
- callback(stream);
-
- stream.on('end', function(){
- if(stream.res) stream.res.removeAllListeners('end');
- stream.end();
- req = stream = null;
- });
+ this.handleStream(req, endpoint, params, callback);
+ } else {
+ this.handleSimpleRequest(req, endpoint, params, callback);
+ }
+};
- stream.on('reconnect', function(new_args){
- args[1] = new_args || args[1];
- if(stream.res) stream.res.removeAllListeners('end');
- APIRequest.apply(self, args);
- req = stream = null;
- });
+/*
+ * Handle simple request
+ */
- } else {
- req.end(function(res){
- debug('data received for %s', endpoint.resource);
- if (res.statusCode == 429) {
- sleep_duration = (res.header['x-rate-limit-reset']*1000 - new Date()) + 7000;
- debug('rate limit reached, sleeping during', Math.ceil(sleep_duration/60000), 'minutes');
- setTimeout(function() {
+Tuiter.prototype.handleSimpleRequest = function(req, endpoint, params, callback) {
+ var self = this;
+ req.end(function(res){
+ debug('data received for %s', endpoint.resource);
+ if (res.statusCode == 429) {
+ self.sleep_duration = (res.header['x-rate-limit-reset']*1000 - new Date()) + 7000;
+ debug('rate limit reached, sleeping during', Math.ceil(sleep_duration/60000),
+ 'minutes');
+ setTimeout(function() {
debug('rate limit reset, resuming extraction...');
APIRequest(endpoint, params, callback);
- }, sleep_duration);
- }
- else {
- callback(res.error, res.body);
- }
- });
- }
+ }, sleep_duration);
+ } else {
+ callback(res.error, res.body);
+ }
+ });
+};
+
+
+/*
+ * Handle data stream
+ */
+
+Tuiter.prototype.handleStream = function(req, endpoint, params, callback) {
+ var self = this;
+ req.parse(function(){});
+ var stream = new NJStream('\r');
+ stream.req = req;
+ stream.parent = this;
+ var args = stream.args = arguments;
+ stream.emitAPIEvents();
+ stream.autoReconnect();
+
+ req.pipe(stream);
+ callback(stream);
+
+ stream.on('end', function(){
+ if(stream.res) stream.res.removeAllListeners('end');
+ stream.end();
+ req = stream = null;
+ });
+
+ stream.on('reconnect', function(new_args){
+ params = new_args || params;
+ if(stream.res) stream.res.removeAllListeners('end');
+ req = stream = null;
+ APIRequest.call(self, endpoint, params, callback);
+ });
};
/*
@@ -256,7 +279,6 @@ NJStream.prototype.emitAPIEvents = function() {
}
});
-
this.on('error', function(){});
this.req.on('response', function(res){ self.res = res; });
};

0 comments on commit eb4a0ac

Please sign in to comment.