-
Notifications
You must be signed in to change notification settings - Fork 29
/
yakuza-base.js
134 lines (108 loc) · 3.34 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/**
* @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 = {};
}
/**
* 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;
if (!scraperId || !_.isString(scraperId)) {
throw new Error('Scraper id must be passed');
}
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) {
if (!agentId || !_.isString(agentId)) {
throw new Error('Agent id must be passed');
}
return this.scraper(scraperId).agent(agentId);
};
YakuzaBase.prototype.task = function (scraperId, agentId, taskId) {
if (!taskId || !_.isString(taskId)) {
throw new Error('Task id must be passed');
}
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 || !_.isString(scraperId)) {
throw new Error('Scraper id must be passed');
}
if (!agentId || !_.isString(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];
if (!scraper) {
throw new Error('Scraper ' + scraperId + ' doesn\'t exist');
}
agent = scraper._agents[agentId];
if (!agent) {
throw new Error('Agent ' + agentId + ' doesn\'t exist in scraper ' + scraperId);
}
newId = shortId.generate();
newJob = new Job(newId, scraper, agent, params);
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) {
_.each(scraper._agents, function (agent) {
agent._applySetup();
});
});
};
module.exports = YakuzaBase;