forked from Arty-ux/uk-postcodes-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
47 lines (38 loc) · 1.26 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
var path = require("path"),
fs = require("fs"),
utils = require("./lib/utils"),
package = JSON.parse(fs.readFileSync(path.join(__dirname, "package.json")));
var DEFAULTS = {};
DEFAULTS.host = "uk-postcodes.com";
DEFAULTS.port = 80;
DEFAULTS.version = package.version;
var resources = {
Postcode: require(path.join(__dirname, "lib/postcode")),
Geolocation: require(path.join(__dirname, "lib/geolocation")),
Vicinity: require(path.join(__dirname, "lib/vicinity"))
}
function UKPostcodes() {
this._api = {
host: DEFAULTS.host,
port: DEFAULTS.port,
platform: process.platform,
version: DEFAULTS.version
}
for (res in resources) {
this[res] = new resources[res](this);
}
}
// Helper methods to get data quicker without managing REST resources
UKPostcodes.prototype.getPostcode = function (postcode, callback) {
this.Postcode.get(postcode, callback);
}
UKPostcodes.prototype.nearestPostcode = function (geolocation, callback) {
if (!utils.validGeolocation(geolocation)) {
return callback(new Error("Invalid location specified"), null);
}
this.Geolocation.get(geolocation, callback);
}
UKPostcodes.prototype.nearestPostcodes = function (location, radius, callback) {
this.Vicinity.get(location, radius, callback);
}
module.exports = new UKPostcodes();