Skip to content

Commit 3192d42

Browse files
committed
Define all scraper methods in prototype
1 parent b2d2bed commit 3192d42

File tree

2 files changed

+50
-63
lines changed

2 files changed

+50
-63
lines changed

lib/scraper.js

Lines changed: 47 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,55 @@ var loadHtml = require('./file-handlers/html');
1212
var loadCss = require('./file-handlers/css');
1313
var compareUrls = require('compare-urls');
1414

15-
function getLoadedResource (resource) {
15+
function getHandleFunction (resource) {
16+
var type = resource.getType();
17+
switch (type) {
18+
case types.css: return loadCss;
19+
case types.html: return function loadHtmlAndCss (context, po) {
20+
return loadHtml(context, po).then(function (loaded) {
21+
return loadCss(context, loaded);
22+
});
23+
};
24+
default: return _.noop;
25+
}
26+
}
27+
28+
function Scraper (options) {
29+
this.originalResources = [];
30+
this.loadedResources = [];
31+
32+
this.options = _.extend({}, defaults, options);
33+
this.options.directory = path.resolve(process.cwd(), this.options.directory || '');
34+
}
35+
36+
Scraper.prototype.getLoadedResource = function getLoadedResource (resource) {
1637
return _.find(this.loadedResources, function(lr) {
1738
return compareUrls(resource.getUrl(), lr.getUrl());
1839
});
19-
}
40+
};
2041

21-
function addLoadedResource (resource) {
42+
Scraper.prototype.addLoadedResource = function addLoadedResource (resource) {
2243
this.loadedResources.push(resource);
23-
}
44+
};
2445

25-
function getOccupiedFilenames () {
46+
Scraper.prototype.getOccupiedFilenames = function getOccupiedFilenames () {
2647
var subdirectories = _.map(this.options.subdirectories, function (dir) { return dir.directory; });
2748
var loadedFiles = _.map(this.loadedResources, function(r) { return r.getFilename(); });
2849
return subdirectories.concat(loadedFiles);
29-
}
50+
};
3051

31-
function getHtmlSources () {
52+
Scraper.prototype.getHtmlSources = function getHtmlSources () {
3253
return this.options.sources;
33-
}
54+
};
3455

35-
function generateFilename (resource) {
56+
Scraper.prototype.generateFilename = function generateFilename (resource) {
3657
var self = this;
3758

3859
var occupiedFilenames = self.getOccupiedFilenames();
3960

40-
var preferedFilename = resource.getFilename(); // which was set in options
61+
var preferredFilename = resource.getFilename(); // which was set in options
4162
var urlFilename = utils.getFilenameFromUrl(resource.getUrl()); // try to get filename from url
42-
var filename = preferedFilename || urlFilename || self.options.defaultFilename;
63+
var filename = preferredFilename || urlFilename || self.options.defaultFilename;
4364

4465
var ext = path.extname(filename);
4566
var dir = self.getDirectoryByExtension(ext);
@@ -52,30 +73,17 @@ function generateFilename (resource) {
5273
index++;
5374
}
5475
return currentFilename;
55-
}
76+
};
5677

57-
function getDirectoryByExtension (ext) {
78+
Scraper.prototype.getDirectoryByExtension = function getDirectoryByExtension (ext) {
5879
return _.chain(this.options.subdirectories)
5980
.filter(function (dir) { return _.contains(dir.extensions, ext); })
6081
.map(function (dir) { return dir.directory; })
6182
.first()
6283
.value() || '';
63-
}
64-
65-
function getHandleFunction (resource) {
66-
var type = resource.getType();
67-
switch (type) {
68-
case types.css: return loadCss;
69-
case types.html: return function loadHtmlAndCss (context, po) {
70-
return loadHtml(context, po).then(function (loaded) {
71-
return loadCss(context, loaded);
72-
});
73-
};
74-
default: return _.noop;
75-
}
76-
}
84+
};
7785

78-
function loadResource (resource) {
86+
Scraper.prototype.loadResource = function loadResource (resource) {
7987
var self = this;
8088

8189
var loaded = self.getLoadedResource(resource); // try to find already loaded
@@ -104,18 +112,17 @@ function loadResource (resource) {
104112
return Promise.resolve(resource);
105113
});
106114
}
107-
108115
return Promise.resolve(loaded);
109-
}
116+
};
110117

111-
function validate () {
118+
Scraper.prototype.validate = function validate () {
112119
if (fs.existsSync(this.options.directory)) {
113120
return Promise.reject(new Error('Path ' + this.options.directory + ' exists'));
114121
}
115122
return Promise.resolve();
116-
}
123+
};
117124

118-
function prepare () {
125+
Scraper.prototype.prepare = function prepare () {
119126
var self = this;
120127
fs.ensureDirSync(self.options.directory);
121128

@@ -130,9 +137,9 @@ function prepare () {
130137
return new Resource(url, filename);
131138
});
132139
return Promise.resolve();
133-
}
140+
};
134141

135-
function load () {
142+
Scraper.prototype.load = function load () {
136143
var self = this;
137144
return Promise.map(self.originalResources, function loadPage (po) {
138145
return self.loadResource(po).then(function pageLoaded (loaded) {
@@ -142,39 +149,19 @@ function load () {
142149
});
143150
});
144151
});
145-
}
152+
};
146153

147-
function errorCleanup (error) {
154+
Scraper.prototype.errorCleanup = function errorCleanup (error) {
148155
if (!_.isEmpty(this.loadedResources)) {
149156
fs.removeSync(this.options.directory);
150157
}
151158
throw error;
152-
}
153-
154-
function Scraper (options) {
155-
this.originalResources = [];
156-
this.loadedResources = [];
157-
158-
this.options = _.extend({}, defaults, options);
159-
this.options.directory = path.resolve(process.cwd(), this.options.directory || '');
160-
161-
this.validate = validate.bind(this);
162-
this.prepare = prepare.bind(this);
163-
this.load = load.bind(this);
164-
this.errorCleanup = errorCleanup.bind(this);
165-
166-
this.loadResource = loadResource.bind(this);
167-
this.getLoadedResource = getLoadedResource.bind(this);
168-
this.addLoadedResource = addLoadedResource.bind(this);
169-
this.getOccupiedFilenames = getOccupiedFilenames.bind(this);
170-
this.generateFilename = generateFilename.bind(this);
171-
this.getDirectoryByExtension = getDirectoryByExtension.bind(this);
172-
this.getHtmlSources = getHtmlSources.bind(this);
173-
}
159+
};
174160

175161
Scraper.prototype.scrape = function scrape(callback) {
176162
var self = this;
177-
return self.validate()
163+
return Promise.bind(self)
164+
.then(self.validate)
178165
.then(self.prepare)
179166
.then(self.load)
180167
.catch(self.errorCleanup)

test/unit/scraper-test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ describe('Scraper', function () {
157157
});
158158

159159
var loadResourceSpy = sinon.spy(s, 'loadResource');
160-
s.prepare().then(s.load).then(function() {
160+
s.prepare().bind(s).then(s.load).then(function() {
161161
loadResourceSpy.calledTwice.should.be.eql(true);
162162
done();
163163
}).catch(done);
@@ -175,7 +175,7 @@ describe('Scraper', function () {
175175
directory: testDirname
176176
});
177177

178-
s.prepare().then(s.load).then(function(res) {
178+
s.prepare().bind(s).then(s.load).then(function(res) {
179179
res.should.be.instanceOf(Array);
180180
res.should.have.length(2);
181181
res[0].should.have.properties(['url', 'filename']);
@@ -487,7 +487,7 @@ describe('Scraper', function () {
487487
});
488488
});
489489

490-
describe('#load', function() {
490+
describe('#loadResource', function() {
491491
it('should load resource', function(done) {
492492
nock('http://example.com').get('/a.png').reply(200, 'OK');
493493

0 commit comments

Comments
 (0)