-
Notifications
You must be signed in to change notification settings - Fork 0
/
GeoAPI.js
125 lines (102 loc) · 2.6 KB
/
GeoAPI.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
var q = require('q');
var http = require('http');
var extend = require('util')._extend;
var querystring = require('querystring');
module.exports = function() {
var self = this;
this.conf = {
url: 'apiv1.geoapi.es',
type: 'JSON',
key: '',
sandbox: 0
};
this._data = {
lastQuery: {
url: '',
params: {}
},
lastResult: {
status: 0,
data: {}
}
};
return {
setConfig: function(constant, value){
self.conf[constant] = value;
},
getLastQuery: function(){
return self._data.lastQuery;
},
getLastResult: function(){
return self._data.lastResult;
},
_call: function(accion, params, deferred){
params = extend(extend({}, self.conf), params);
delete params.url;
var req = http.request({
method: 'GET',
hostname: self.conf.url,
path: '/' + accion + '?' + querystring.stringify(params)
}, function(res){
var data = "";
res.setEncoding('utf8');
res.on("data", function(chunk) {
data += chunk;
});
res.on("end", function() {
self._data = {
lastQuery: {
url: self.conf.url + accion,
params: params
},
lastResult: {
status: res.statusCode,
res: JSON.parse(data)
}
};
if(res.statusCode == 200){
deferred.resolve(JSON.parse(data));
}
});
});
req.on("error", function(e) {
deferred.reject(e);
console.log("Error: ", e.message);
});
req.end();
return deferred.promise;
},
calles: function calles(params){ /*CPRO, CMUM, CUN, CPOS*/
var deferred = q.defer();
return this._call('calles', params, deferred);
},
comunidades: function comunidades(params){ /**/
var deferred = q.defer();
return this._call('comunidades', params, deferred);
},
cps: function cps(params){ /*CPRO, CMUM, CUN*/
var deferred = q.defer();
return this._call('cps', params, deferred);
},
municipios: function municipios(params){ /*CPRO*/
var deferred = q.defer();
return this._call('municipios', params, deferred);
},
nucleos: function nucleos(params){ /*CPRO, CMUM, NENTSI50*/
var deferred = q.defer();
return this._call('nucleos', params, deferred);
},
poblaciones: function poblaciones(params){ /*CPRO, CMUM*/
var deferred = q.defer();
return this._call('poblaciones', params, deferred);
},
provincias: function provincias(params){ /*CCOM*/
var deferred = q.defer();
return this._call('provincias', params, deferred);
},
qcalles: function qcalles(params){ /*QUERY*/
var deferred = q.defer();
return this._call('qcalles', params, deferred);
}
}
};