-
Notifications
You must be signed in to change notification settings - Fork 0
/
overpass.js
46 lines (37 loc) · 1.1 KB
/
overpass.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
var VERSION = '0.1.0';
var _ = require('underscore');
var request = require('request').defaults({jar: true});
var query = _.template("[out:json][timeout:<%=timeout%>];<%=query%>out body;")
function Overpass(options) {
if (!(this instanceof Overpass)) return new Overpass(options);
var self = this;
var defaults = {
endpoint: "http://overpass-api.de/api/interpreter",
timeout: 30 * 1000, // milisecs
responseformat: "json"
};
self.options = _.extend(defaults, options);
}
Overpass.VERSION = VERSION;
Overpass.prototype.get = function(userquery, done){
var self = this;
request({
method: "GET",
uri: self.options.endpoint,
timeout: self.options.timeout,
qs: {
data: query({
timeout: self.options.timeout / 1000,
query: userquery
})
}
}, function(error, response, body) {
if (error) return done(error);
else if (response.statusCode != 200) {
done({message: 'Response status code ' + response.statusCode }, JSON.parse(body) || {});
} else {
done(null, JSON.parse(body) || {});
}
});
}
module.exports = Overpass;