forked from frictionlessdata/frictionlessdata.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.js
53 lines (47 loc) · 1.05 KB
/
model.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
var request = require('request');
var marked = require('marked');
var Catalog = function() {
this._cache = {};
};
Catalog.prototype.load = function(datapackages) {
var that = this;
for (id in datapackages) {
var dp = datapackages[id];
if (!dp.id) {
dp.id = dp.name || 'no-id';
}
if (dp.resources && dp.resources.length > 0) {
dp.download_url = dp.resources[0].url;
} else {
dp.download_url = '';
}
if (dp.readme) {
dp.readme_html = marked(dp.readme);
} else {
dp.readme_html = '';
}
that._cache[id] = dp;
}
}
Catalog.prototype.loadURL = function(url, cb) {
var that = this;
request(url, function(err, res, body) {
if (err) {
cb(err);
} else {
that.load(JSON.parse(body));
cb();
}
});
}
Catalog.prototype.get = function(id) {
return this._cache[id];
}
Catalog.prototype.query = function(q) {
var that = this;
// TODO: actual search
return Object.keys(this._cache).map(function(key) {
return that._cache[key];
});
}
exports.Catalog = Catalog;