-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
163 lines (128 loc) · 4.74 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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
'use strict';
const eachAsync = require('async-each-map');
module.exports.conf = {
API_KEY: process.env.NTB_API_KEY,
API_ENV: process.env.NTB_API_ENV || 'api',
USER_AGENT: process.env.NTB_USER_AGENT || require('./package.json').version,
};
module.exports._requestDefaults = function reqestDefaults() {
return require('request').defaults({
followAllRedirects: true,
baseUrl: `https://${module.exports.conf.API_ENV}.nasjonalturbase.no/`,
headers: {
'user-agent': module.exports.conf.USER_AGENT,
},
json: true,
qs: {
api_key: module.exports.conf.API_KEY,
},
});
};
let request = module.exports._requestDefaults();
[
'aktiviteter',
'bilder',
'grupper',
'områder',
'steder',
'turer',
].forEach((type) => {
module.exports[type] = function typeAll(params, callback) {
if (!callback) { return request.get({ url: encodeURIComponent(type), qs: params }); }
return request.get({ url: encodeURIComponent(type), qs: params }, callback);
};
module.exports[type].each = function typeEach(params, callback, done) {
const query = params;
query.skip = query.skip || 0;
query.limit = query.limit || 50;
query.sort = query.sort || '_id';
const noSkip = !!query.__no_skip__;
delete query.__no_skip__;
module.exports[type](query, (typeErr, res, body) => {
if (typeErr) { return done(typeErr); }
return eachAsync(body.documents, callback, (eachErr) => {
if (eachErr) { return done(eachErr); }
if (body.documents < query.limit) {
return done(null);
}
if (!noSkip) {
query.skip += query.limit;
}
return module.exports[type].each(query, callback, done);
});
});
};
module.exports[type].post = function typePost(data, callback) {
if (!callback) {
return request.post({ url: encodeURIComponent(type), body: data });
}
return request.post({ url: encodeURIComponent(type), body: data }, callback);
};
module.exports[type].get = function typeGet(id, callback) {
if (!callback) {
return request.get({ url: `${encodeURIComponent(type)}/${id}` });
}
return request.get({ url: `${encodeURIComponent(type)}/${id}` }, callback);
};
module.exports[type].delete = function typeDelete(id, callback) {
if (!callback) {
return request.del({ url: `${encodeURIComponent(type)}/${id}` });
}
return request.del({ url: `${encodeURIComponent(type)}/${id}` }, callback);
};
module.exports[type].put = function typePut(id, data, callback) {
if (!callback) {
return request.put({ url: `${encodeURIComponent(type)}/${id}`, body: data });
}
return request.put({ url: `${encodeURIComponent(type)}/${id}`, body: data }, callback);
};
module.exports[type].patch = function typePatch(id, data, callback) {
if (!callback) {
return request.patch({ url: `${encodeURIComponent(type)}/${id}`, body: data });
}
return request.patch({ url: `${encodeURIComponent(type)}/${id}`, body: data }, callback);
};
});
module.exports.configure = function configure(obj) {
Object.keys(obj).forEach((key) => {
module.exports.conf[key] = obj[key];
});
// Generate a new request object
request = module.exports._requestDefaults();
};
module.exports.util = {};
module.exports.util.attribution = function navngiving(type, doc, authors, license) {
const licenses = new Map([
['CC BY 4.0', 'http://creativecommons.org/licenses/by/4.0/deed.no'],
['CC BY-SA 4.0', 'http://creativecommons.org/licenses/by-sa/4.0/deed.no'],
['CC BY-ND 4.0', 'http://creativecommons.org/licenses/by-nd/4.0/deed.no'],
['CC BY-NC 4.0', 'http://creativecommons.org/licenses/by-nc/4.0/deed.no'],
['CC BY-NC-SA 4.0', 'http://creativecommons.org/licenses/by-nc-sa/4.0/deed.no'],
['CC BY-NC-ND 4.0', 'http://creativecommons.org/licenses/by-nc-nd/4.0/deed.no'],
]);
const docName = doc.navn ? doc.navn : 'Uten Navn';
const docUrl = `http://www.ut.no/${type}/${doc._id}/`;
let attribution = `"<a href="${docUrl}">${docName}</a>" av `;
if (authors && authors instanceof Array) {
attribution = attribution + authors.reduce((prev, author, i, a) => {
let delim;
if (prev) {
delim = i < a.length - 1 ? ', ' : ' og ';
} else {
delim = '';
}
if (author.url) {
return `${prev}${delim}<a href="${author.url}">${author.navn}</a>`;
}
return prev + delim + author.navn;
}, '');
} else if (typeof authors === 'string') {
attribution = attribution + authors;
} else {
attribution = `${attribution}Ukjent`;
}
if (licenses.has(license)) {
return `${attribution} er lisensiert under <a href="${licenses.get(license)}">${license}</a>.`;
}
return `${attribution} er lisensiert under ${license}.`;
};