-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.js
112 lines (99 loc) · 3.07 KB
/
api.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
// Generated by CoffeeScript 1.10.0
(function() {
var API, Promise, get_data, get_meta, get_topic_tree, read_odata;
Promise = require("promise");
read_odata = require("./utils").read_odata;
API = "https://opendata.cbs.nl/ODataApi/odata";
get_meta = function(table, callback) {
var keys, store, url;
store = {};
url = API + "/" + table;
keys = [];
return read_odata(url).then(function(metadata) {
var j, len, md, parts;
parts = [];
for (j = 0, len = metadata.length; j < len; j++) {
md = metadata[j];
if (md.name === "UntypedDataSet" || md.name === "TypedDataSet") {
continue;
}
keys.push(md.name);
parts.push(read_odata(md.url));
}
return Promise.all(parts);
}).then(function(parts) {
var i, j, metadata, ref;
metadata = {};
for (i = j = 0, ref = parts.length; 0 <= ref ? j < ref : j > ref; i = 0 <= ref ? ++j : --j) {
metadata[keys[i]] = parts[i];
}
return metadata;
}).nodeify(callback);
};
/* Get data via API, which is restricted to 10 000 rows */
get_data = function(table, filter, select, callback) {
var url;
url = API + "/" + table + "/TypedDataSet";
return read_odata(url, filter, select).nodeify(callback);
};
get_topic_tree = function(meta) {
var col, dataprops, idx, j, k, l, len, len1, len2, parent, roots, set_title;
dataprops = meta.DataProperties;
idx = {};
for (j = 0, len = dataprops.length; j < len; j++) {
col = dataprops[j];
idx[col.ID] = col;
}
roots = [];
for (k = 0, len1 = dataprops.length; k < len1; k++) {
col = dataprops[k];
if (col.ParentID && (parent = idx[col.ParentID])) {
if (parent.children == null) {
parent.children = [];
}
parent.children.push(col);
} else {
roots.push(col);
}
}
set_title = function(mcol, prefix) {
var child, l, len2, ref, results;
if (prefix == null) {
prefix = "";
}
mcol.title = "" + prefix + mcol.Title;
if (mcol.children) {
ref = mcol.children;
results = [];
for (l = 0, len2 = ref.length; l < len2; l++) {
child = ref[l];
results.push(set_title(child, mcol.title + "|"));
}
return results;
}
};
for (l = 0, len2 = roots.length; l < len2; l++) {
col = roots[l];
set_title(col);
}
return roots;
};
module.exports = {
get_meta: get_meta,
get_data: get_data
};
/* Testing
get_meta("81251ned", console.log)
#get_meta("70636eng").then(console.log).catch(console.log)
get_data("81251ned",
{Perioden: ['2010MM12','2011MM12'], WoonregioS:['NL10 ']}).then((results) -> console.log results[0], results.length)
get_meta("81251ned", console.log)
get_meta("81251ned").then console.log
get_data("81251ned",
{Perioden: ['2010MM12','2011MM12'], WoonregioS:['NL10 ']})
.then((results) -> console.log results[0], results.length)
get_meta("81037ned")
.then(get_topic_tree)
.then(console.log)
*/
}).call(this);