forked from RafaelVidaurre/yakuza
-
Notifications
You must be signed in to change notification settings - Fork 0
/
yakuza-base.js
119 lines (100 loc) · 2.93 KB
/
yakuza-base.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
* @module YakuzaBase
* @author Rafael Vidaurre
* @requires Utils
* @requires Scraper
* @requires Job
*/
'use strict';
var shortId, utils, _, Scraper, Job;
shortId = require('shortid');
utils = require('./utils');
_ = require('lodash');
Scraper = require('./scraper');
Job = require('./job');
/**
* Main singleton class used to define scrapers and their properties
* @class
*/
function YakuzaBase () {
/**
* Set of scraper instances
* @private
*/
this._scrapers = {};
/**
* Set of job instances
* @private
*/
this._jobs = {};
}
/**
* Creates a new scraper instance
* @param {string} scraperId name to be asigned to the created scraper
* @return {Scraper} scraper created
* @private
*/
YakuzaBase.prototype._createScraper = function (scraperId) {
this._scrapers[scraperId] = new Scraper();
return this._scrapers[scraperId];
};
/**
* Returns a scraper instance, if it doesn't exist, it creates it
* @param {string} scraperId name for the new scraper or by which to look for if it exists
* @return {Scraper} scraper instance
*/
YakuzaBase.prototype.scraper = function (scraperId) {
var thisScraper, scraperExists;
scraperExists = utils.hasKey(this._scrapers, scraperId);
thisScraper = scraperExists ? this._scrapers[scraperId] : this._createScraper(scraperId);
return thisScraper;
};
/**
* Returns an agent instance, if it doesn't exists, it creates it
* @param {string} scraperId name of the scraper to which the agent belongs to
* @param {string} agentId name of the new agent or by which to look for if it exists
* @return {Agent} agent instance
*/
YakuzaBase.prototype.agent = function (scraperId, agentId) {
return this.scraper(scraperId).agent(agentId);
};
YakuzaBase.prototype.task = function (scraperId, agentId, taskId) {
return this.agent(scraperId, agentId).task(taskId);
};
/**
* Instances a new job
* @param {string} scraperId name of the scraper that will be used by the Job
* @param {string} agentId name of the agent that will be used by the Job
* @return {Job} Job instance that has been created
*/
YakuzaBase.prototype.job = function (scraperId, agentId, params) {
var newId, scraper, agent, newJob;
if (!scraperId) {
throw new Error('Scraper id must be passed');
}
if (!agentId) {
throw new Error('Agent id must be passed');
}
if (params && !_.isObject(params)) {
throw new Error('Params passed must be an object');
}
scraper = this._scrapers[scraperId];
agent = scraper._agents[agentId];
newId = shortId.generate();
newJob = new Job(newId, scraper, agent, params);
this._jobs[newId] = newJob;
return newJob;
};
/**
* Applies all configurations for all scrapers and agents, Yakuza can eager-load (via this
* method) or lazy-load (by running a job).
*/
YakuzaBase.prototype.ready = function () {
_.each(this._scrapers, function (scraper) {
scraper._applySetup();
_.each(scraper._agents, function (agent) {
agent._applySetup();
});
});
};
module.exports = YakuzaBase;