Skip to content

Commit 64d98e8

Browse files
committed
Implement maxDepth option
1 parent abc4a27 commit 64d98e8

File tree

2 files changed

+27
-16
lines changed

2 files changed

+27
-16
lines changed

lib/scraper.js

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,18 @@ var _ = require('underscore');
1010

1111
var defaults = require('./config/defaults');
1212
var types = require('./config/resource-types');
13-
var resursiveSources = require('./config/recursive-sources');
13+
var recursiveSources = require('./config/recursive-sources');
1414
var utils = require('./utils.js');
1515
var request = require('./request');
1616
var Resource = require('./resource');
17-
var loadHtml = require('./file-handlers/html');
18-
var loadCss = require('./file-handlers/css');
1917
var compareUrls = require('compare-urls');
2018

21-
function getHandleFunction (resource) {
22-
var type = resource.getType();
23-
switch (type) {
24-
case types.css: return loadCss;
25-
case types.html: return function loadHtmlAndCss (context, po) {
26-
return loadHtml(context, po).then(function (loaded) {
27-
return loadCss(context, loaded);
28-
});
29-
};
30-
default: return _.noop;
31-
}
19+
var loadHtml = require('./file-handlers/html');
20+
var loadCss = require('./file-handlers/css');
21+
function loadHtmlAndCss (context, po) {
22+
return loadHtml(context, po).then(function (loaded) {
23+
return loadCss(context, loaded);
24+
});
3225
}
3326

3427
function Scraper (options) {
@@ -89,6 +82,20 @@ Scraper.prototype.getDirectoryByExtension = function getDirectoryByExtension (ex
8982
.value() || '';
9083
};
9184

85+
Scraper.prototype.getResourceHandler = function getHandler (resource) {
86+
var self = this;
87+
var type = resource.getType();
88+
var depth = resource.getDepth();
89+
var depthGreaterThanMax = self.options.maxDepth && depth > self.options.maxDepth;
90+
91+
switch (true) {
92+
case depthGreaterThanMax: return _.noop;
93+
case type == types.css: return loadCss;
94+
case type == types.html: return loadHtmlAndCss;
95+
default: return _.noop;
96+
}
97+
};
98+
9299
Scraper.prototype.loadResource = function loadResource (resource) {
93100
var self = this;
94101

@@ -108,7 +115,7 @@ Scraper.prototype.loadResource = function loadResource (resource) {
108115
return self.makeRequest(url).then(function requestCompleted(data) {
109116
resource.setUrl(data.url); // Url may be changed in redirects
110117
resource.setText(data.body);
111-
handleFile = getHandleFunction(resource);
118+
handleFile = self.getResourceHandler(resource);
112119
return handleFile(self, resource);
113120
}).then(function fileHandled() {
114121
var filename = path.join(self.options.directory, resource.getFilename());
@@ -143,7 +150,7 @@ Scraper.prototype.prepare = function prepare () {
143150
});
144151

145152
if (self.options.recursive) {
146-
self.options.sources = _.union(self.options.sources, resursiveSources);
153+
self.options.sources = _.union(self.options.sources, recursiveSources);
147154
}
148155

149156
return ensureDirAsync(self.options.directory);

test/unit/scraper-test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,10 @@ describe('Scraper', function () {
551551
});
552552
});
553553

554+
describe('#getResourceHandler', function() {
555+
556+
});
557+
554558
describe('#scrape', function() {
555559
it('should call methods in sequence', function(done) {
556560
nock('http://example.com').get('/').reply(200, 'OK');

0 commit comments

Comments
 (0)