Skip to content
This repository has been archived by the owner on Dec 31, 2020. It is now read-only.

Commit

Permalink
Small refactor of requests
Browse files Browse the repository at this point in the history
  • Loading branch information
alexnault committed May 24, 2015
1 parent f718dd0 commit 264dd4d
Showing 1 changed file with 30 additions and 30 deletions.
60 changes: 30 additions & 30 deletions lib/projection.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,11 @@ Projection.prototype.findTheaters = function(near, options, callback) {
return;
}

request({uri: url, method: 'GET', encoding: null}, function (err, res, body) {
err = errorCheck(err, res);
if (err) {
callback(err);
return;
}

var encoding = charset(res.headers, body);
var $ = cheerio.load(iconv.decode(new Buffer(body), encoding));
requestGET(url, function(err, $) {
if (err) {
callback(err);
return;
}

// List of theaters returned
var theaters = [];
Expand Down Expand Up @@ -81,7 +77,7 @@ Projection.prototype.findTheaters = function(near, options, callback) {

self._cache.set(url, theaters);
callback(null, theaters);
})
});

};

Expand All @@ -98,15 +94,11 @@ Projection.prototype.findMovie = function(near, movie, options, callback) {
return;
}

request({uri: url, method: 'GET', encoding: null}, function (err, res, body) {
err = errorCheck(err, res);
if (err) {
callback(err);
return;
}

var encoding = charset(res.headers, body);
var $ = cheerio.load(iconv.decode(new Buffer(body), encoding));
requestGET(url, function(err, $) {
if (err) {
callback(err);
return;
}

var m = $('.movie');

Expand Down Expand Up @@ -158,6 +150,25 @@ Projection.prototype.findMovie = function(near, movie, options, callback) {

};

// Private
function requestGET(url, callback) {
request({uri: url, method: 'GET', encoding: null}, function (err, res, body) {
if (err) {
callback(err);
return;
}
if (res.statusCode !== 200) {
callback(res.statusCode);
returnl
}

var encoding = charset(res.headers, body);
var $ = cheerio.load(iconv.decode(new Buffer(body), encoding));

callback(null, $);
})
};

// Private
function toQS(options, near, movie){
var args = _.pick(options, 'hl', 'date');
Expand All @@ -166,17 +177,6 @@ function toQS(options, near, movie){
return '?' + qs.stringify(args);
};

// Private
function errorCheck(error, response){
if (error) {
return error;
}
if (response.statusCode !== 200) {
return response.statusCode;
}
return null;
};

// Private
function formatInfos(infos) {
var cursor = 0,
Expand Down

0 comments on commit 264dd4d

Please sign in to comment.