Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

cleaned up some stuff, adopted promise-object, and added travis

  • Loading branch information...
commit 2c7e2a4addde8b1212f4fc84cb4d56ab8acbe78c 1 parent bd19cc8
Chad Scira authored
Showing with 90 additions and 91 deletions.
  1. +3 −0  .travis.yml
  2. +1 −1  Readme.md
  3. +74 −82 lib/requester.js
  4. +12 −8 package.json
3  .travis.yml
View
@@ -0,0 +1,3 @@
+language: node_js
+node_js:
+ - 0.8
2  Readme.md
View
@@ -1,4 +1,4 @@
-## node-requester
+## node-requester [![Build Status](https://travis-ci.org/icodeforlove/node-requester.png?branch=master)](https://travis-ci.org/icodeforlove/node-requester)
A simple network request helper that is geared towards crawling. (a few keywords GZIP, XML, JSON, PROXIES)
156 lib/requester.js
View
@@ -11,35 +11,32 @@ var http = require('http'),
async = require('async'),
zlib = require('zlib'),
_ = require('underscore'),
- colors = require('colors');
-
-function RequesterHandler ($config) {
- $config = $config || {};
-
- this._debug = $config.debug;
- this._cookies = $config.cookies || {};
- this._headers = $config.headers || {};
- this._timeout = $config.timeout || 30000;
- this._proxies = $config.proxies || [];
- this._retries = $config.retries || 0;
- this._encoding = $config.encoding || 'utf8';
- this._cookiejar = $config.cookiejar ? {} : false;
- this._follow = _.isUndefined($config.follow) ? true : $config.follow;
- this._followMax = _.isUndefined($config.followMax) ? 5 : $config.followMax;
- this._requestID = 0;
- this._didRequestFail = $config.didRequestFail;
- this._signRequest = $config.signRequest;
-
- this._beforeRequest = $config.beforeRequest;
- this._afterResponse = $config.afterResponse;
- this._processResponse = $config.processResponse;
- this._dataType = $config.dataType;
- this._auth = $config.auth;
-
- if (this._cookiejar) this._cookiejar = _.extend(this._cookiejar, this._cookies);
-}
-
-RequesterHandler.prototype = {
+ colors = require('colors'),
+ PromiseObject = require('promise-object'),
+ EventsMixin = require('promise-object/mixins/events');
+
+var RequesterHandler = PromiseObject.create(EventsMixin, {
+ initialize: function ($config) {
+ this._debug = $config.debug;
+ this._cookies = $config.cookies || {};
+ this._headers = $config.headers || {};
+ this._timeout = $config.timeout || 30000;
+ this._proxies = $config.proxies || [];
+ this._retries = $config.retries || 0;
+ this._encoding = $config.encoding || 'utf8';
+ this._cookiejar = $config.cookiejar ? {} : false;
+ this._follow = _.isUndefined($config.follow) ? true : $config.follow;
+ this._followMax = _.isUndefined($config.followMax) ? 5 : $config.followMax;
+ this._requestID = 0;
+ this._didRequestFail = $config.didRequestFail;
+ this._signRequest = $config.signRequest;
+ this._processResponse = $config.processResponse;
+ this._dataType = $config.dataType;
+ this._auth = $config.auth;
+
+ if (this._cookiejar) this._cookiejar = _.extend(this._cookiejar, this._cookies);
+ },
+
post: function ($url, $options, callback) {
if (arguments.length === 2) {
callback = arguments[1];
@@ -47,7 +44,7 @@ RequesterHandler.prototype = {
}
if (!$options.method) $options.method = 'POST';
- this._makeRequest($url, $options, callback);
+ return this._makeRequest($url, $options, callback);
},
put: function ($url, $options, callback) {
@@ -57,7 +54,7 @@ RequesterHandler.prototype = {
}
if (!$options.method) $options.method = 'PUT';
- this._makeRequest($url, $options, callback);
+ return this._makeRequest($url, $options, callback);
},
get: function ($url, $options, callback) {
@@ -67,7 +64,7 @@ RequesterHandler.prototype = {
}
if (!$options.method) $options.method = 'GET';
- this._makeRequest($url, $options, callback);
+ return this._makeRequest($url, $options, callback);
},
del: function ($url, $options, callback) {
@@ -77,7 +74,7 @@ RequesterHandler.prototype = {
}
if (!$options.method) $options.method = 'DELETE';
- this._makeRequest($url, $options, callback);
+ return this._makeRequest($url, $options, callback);
},
addProxies: function () {
@@ -90,7 +87,7 @@ RequesterHandler.prototype = {
multipart: function ($url, $options, callback) {
$options.multipart = true;
- this.post($url, $options, callback);
+ return this.post($url, $options, callback);
},
removeProxies: function () {
@@ -120,8 +117,6 @@ RequesterHandler.prototype = {
$options.encoding = $options.encoding || this._encoding;
$options.didRequestFail = $options.didRequestFail || this._didRequestFail;
$options.signRequest = $options.signRequest || this._signRequest;
- $options.beforeRequest = $options.beforeRequest || this._beforeRequest;
- $options.afterResponse = $options.afterResponse || this._afterResponse;
$options.processResponse = $options.processResponse || this._processResponse;
$options.dataType = $options.dataType || this._dataType;
@@ -134,48 +129,45 @@ RequesterHandler.prototype = {
this._requestID++;
- new Requester(url.parse($url), $options, callback);
+ return new Requester(url.parse($url), $options, callback);
}
-};
-
-function Requester ($url, $options, callback) {
- this._callback = callback;
- this._retry = 0;
- this._followCount = 0;
- this._url = $url;
-
- this._multipart = $options.multipart;
- this._method = $options.method;
- this._cookies = $options.cookies;
- this._headers = $options.headers;
- this._timeout = $options.timeout;
- this._retries = $options.retries;
- this._encoding = $options.encoding;
- this._didRequestFail = $options.didRequestFail;
- this._beforeRequest = $options.beforeRequest;
- this._afterResponse = $options.afterResponse;
- this._signRequest = $options.signRequest;
- this._processResponse = $options.processResponse;
- this._dataType = $options.dataType;
-
- this._cookiejar = $options.cookiejar;
- this._follow = $options.follow;
- this._followMax = $options.followMax;
- this._proxy = $options.proxy;
- this._debug = $options.debug;
- this._data = $options.data;
- this._protocol = $url.protocol === 'https:' && !this._proxy ? https : http;
- this._port = $url.protocol === 'https:' && this._proxy ? 443 : 80;
-
- this._auth = this._proxy && this._proxy.auth ? this._proxy.auth : $options.auth;
- if (this._beforeRequest) this._beforeRequest();
- this._queryString = this._signRequest && this._method !== 'GET' ? this._signRequest(this._data) : this._data ? qs.stringify(this._data) : '';
-
- this._attempt = this._attempt.bind(this);
- this._prepareHttpOptions();
-}
-
-Requester.prototype = {
+});
+
+var Requester = PromiseObject.create(EventsMixin, {
+ initialize: function ($url, $options, callback) {
+ this._callback = callback;
+ this._retry = 0;
+ this._followCount = 0;
+ this._url = $url;
+
+ this._multipart = $options.multipart;
+ this._method = $options.method;
+ this._cookies = $options.cookies;
+ this._headers = $options.headers;
+ this._timeout = $options.timeout;
+ this._retries = $options.retries;
+ this._encoding = $options.encoding;
+ this._didRequestFail = $options.didRequestFail;
+ this._signRequest = $options.signRequest;
+ this._processResponse = $options.processResponse;
+ this._dataType = $options.dataType;
+
+ this._cookiejar = $options.cookiejar;
+ this._follow = $options.follow;
+ this._followMax = $options.followMax;
+ this._proxy = $options.proxy;
+ this._debug = $options.debug;
+ this._data = $options.data;
+ this._protocol = $url.protocol === 'https:' && !this._proxy ? https : http;
+ this._port = $url.protocol === 'https:' && this._proxy ? 443 : 80;
+
+ this._auth = this._proxy && this._proxy.auth ? this._proxy.auth : $options.auth;
+ this._queryString = this._signRequest && this._method !== 'GET' ? this._signRequest(this._data) : this._data ? qs.stringify(this._data) : '';
+
+ this._attempt = this._attempt.bind(this);
+ this._prepareHttpOptions();
+ },
+
_prepareHttpOptions: function () {
var self = this;
@@ -354,7 +346,7 @@ Requester.prototype = {
}
if (this._processResponse) response.body = this._processResponse.call(response, response.body);
-
+
if (this._dataType === 'JSON') {
var json;
try {
@@ -363,7 +355,7 @@ Requester.prototype = {
if (json) {
if (this._didRequestFail && !this._didRequestFail(json) || !this._didRequestFail) {
- if (this._afterResponse) this._afterResponse(json);
+ this.dispatchEvent('response-recieved', {data: json});
this._callback.call(response, json);
} else {
this._requestFailed(response);
@@ -377,13 +369,13 @@ Requester.prototype = {
if (error) {
self._requestFailed(response);
} else {
- if (self._afterResponse) self._afterResponse(xml);
+ self.dispatchEvent('response-recieved', {data: xml});
self._callback.call(response, xml);
}
});
} else {
if (this._didRequestFail && !this._didRequestFail(response.body) || !this._didRequestFail) {
- if (self._afterResponse) self._afterResponse(response.body);
+ this.dispatchEvent('response-recieved', {data: response.body});
this._callback.call(response, response.body);
} else {
this._requestFailed(response);
@@ -406,6 +398,6 @@ Requester.prototype = {
return cookies;
}
-};
+});
module.exports = exports = RequesterHandler;
20 package.json
View
@@ -1,6 +1,6 @@
{
"name": "requester",
- "version": "0.1.13",
+ "version": "0.1.14",
"description": "swiss army knife for requests",
"main": "index.js",
"directories": {
@@ -21,14 +21,18 @@
"author": "Chad Scira",
"license": "MIT",
"devDependencies": {
- "async": "0.1.22",
- "vows": "1.2.7"
+ "async": "~0.1.22",
+ "vows": "0.7.0"
},
"dependencies": {
- "async": "0.1.22",
- "form-data": "0.0.6",
- "underscore": "1.4.0",
- "xml2js": "0.2.0",
- "colors": "0.6.0-1"
+ "async": "~0.1.22",
+ "form-data": "~0.0.6",
+ "underscore": "~1.4.0",
+ "xml2js": "~0.2.0",
+ "colors": "~0.6.0-1",
+ "promise-object": "~0.0.17"
+ },
+ "scripts": {
+ "test": "vows --spec"
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.