forked from jcblw/geode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeode.js
144 lines (125 loc) · 3.26 KB
/
geode.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
/* Geode
* ==========================
* Wrapper for http://www.geonames.org/ api in NodeJS Module form
*/
var request = require('request');
/* @Constructor
* @Param username :String - username to geonames.org
* @Param local :Object - local information (OPTIONAL)
* @local countryCode :String - eg. "US", "CA"
* @local language :String - eg. "en", "sp"
*/
var Geode = function(username, local) {
var that = this;
that.username = (username) ? username : null;
that.endpoint = 'http://api.geonames.org/';
/* only attempt to set countryCode and language
if local object passed */
if(local) {
that.countryCode = (local.countryCode ? local.countryCode : 'US');
that.language = (local.language ? local.language : 'en');
}
if(that.username) {
that.ready = true;
}
else {
throw new Error('username is required');
}
that.localize = {
username : that.username,
country : that.countryCode,
language : that.language
};
/* @Method error :Function - handle errors
* @Param err :Object - Error object returned from request
* @Param callback :Function - Function to pass error data back to
*/
that.error = function(err, callback){
if(process.env.NODE_ENV !== 'production')
console.log(err);
callback(err, {});
};
/* @Method merge :Function - *utility* merging objects
* @Params * :Objects - passed in via arguments array, objects to merge
*/
that.merge = function(){
if(typeof arguments[0] === 'object' && !arguments[0].length){
var base = arguments[0];
for(var i = 1; i < arguments.length; i += 1){
for(var key in arguments[i]){
base[key] = arguments[i][key];
}
}
}
return base;
};
/* @Method request :Function - sends out request to geonames server
* @Param collection :String - corresponds to url endpoints in api
* @Param data :Object - Payload to send in query string
* @Param callback :Function - Function to pass error data back to
*/
that.request = function(collection, data, callback){
var url = that.endpoint + collection + 'JSON';
var payload = that.merge({},that.localize,data);
request.get({
url : url,
qs : payload
}, function(err, res, body){
if(err) that.error(err, callback);
else{
callback(null, JSON.parse(body));
}
});
};
/* All method requirement can be found here
* http://www.geonames.org/export/web-services.html
*/
that.methods = [
'search',
'get',
'postalCode',
'postalCodeLookup',
'findNearbyPostalCodes',
'postalCodeCountryInfo',
'findNearbyPlaceName',
'findNearby',
'extendedFindNearby',
'children',
'hierarchy',
'neighbours',
'siblings',
'findNearbyWikipedia',
'wikipediaSearch',
'wikipediaBoundingBox',
'cities',
'earthquakes',
'weather',
'weatherIcaoJSON',
'findNearByWeather',
'countryInfo',
'countryCode',
'countrySubdivision',
'ocean',
'neighbourhood',
'srtm3',
'astergdem',
'gtopo30',
'timezone'
];
// Compile methods
for(var i = 0; i < that.methods.length; i += 1){
if(!that[that.methods[i]]){
(function(n){
that[that.methods[n]] = function(data, callback){
that.request(that.methods[n], data, callback);
};
}(i));
}
}
/* Eg.
* that.search = function(data, callback){
* that.request('search', data, callback);
* };
*/
};
module.exports = Geode;