-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
executable file
·74 lines (63 loc) · 2.19 KB
/
index.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
var Fixture = require('./lib/Fixture'),
FixtureLoader = require('./lib/FixtureLoader'),
ElementPool = require('./lib/ElementPool'),
Fetcher = require('./lib/Fetcher'),
q = require('q'),
fs = require('fs'),
feedsPath = '../iblfetcher/feeds/';
q.longStackSupport = true;
function FixtureCreator(config) {
that = this;
that.config = config;
that.feeds = {};
that.elementPool = new ElementPool();
that.fetcher = new Fetcher({
apiKey: config.apiKey,
iblUrl: config.iblUrl,
proxy: config.proxy,
cacheDir: config.cacheDir,
cacheExpireTime: config.cacheExpireTime || (new Date().getTime() - (60 * 60 * 1000))
});
that.prefetch = that.fetcher.prefetch().then(function (feeds) {
that.feeds = feeds;
for (var name in feeds) {
var feed = feeds[name];
that.elementPool.processFeed(feed);
}
if (config.debug) {
console.log('Processed feeds, found the following elements for use');
console.log('Episodes:', that.elementPool.pools.Episode.length);
console.log('Group:',that.elementPool.pools.Group.length);
console.log('Programmes:',that.elementPool.pools.Programme.length);
}
}).fail(function (err) {
console.log('Error prefetching feeds', err);
throw err;
});
}
FixtureCreator.prototype.createFixture = function(feedName, params) {
var defer = q.defer(),
newFeedDefer = q.defer(),
that = this,
path = feedName,
cloneFeed;
feed = that.feeds[feedName];
if (feed === undefined) {
that.fetcher.fetch(feedName, params).done(function (json) {
that.feeds[feedName] = json;
newFeedDefer.resolve(json);
});
} else {
cloneFeed = JSON.parse(JSON.stringify(feed));
newFeedDefer.resolve(cloneFeed);
}
newFeedDefer.promise.then(function (feed) {
fixture = new Fixture(feedName, feed, that.elementPool, that.config);
defer.resolve(fixture);
}).fail(function (e) {
defer.reject(e);
}).done();
return defer.promise;
};
module.exports = FixtureCreator;
module.exports.Loader = FixtureLoader;