-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
103 lines (94 loc) · 3.51 KB
/
Copy pathapp.js
File metadata and controls
103 lines (94 loc) · 3.51 KB
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
var fs = require('fs'),
path = require('path'),
moment = require('moment'),
_ = require('underscore');
// This uses sync-request rather than the standar request module
var SyncRequest = require('sync-request');
// Sample sync request to Postcode.io
// console.log(JSON.parse(SyncRequest('GET', 'https://api.postcodes.io/postcodes/LS14 5QL').body).result);
var postcodeFilePath = path.join(path.dirname(fs.realpathSync(__filename)),'data', 'postcodes.json');
var PostcodeDB = {
initialized: false,
existingData: null,
init: function() {
console.log(postcodeFilePath);
if (fs.existsSync(postcodeFilePath)) {
this.existingData = require('./data/postcodes.json');
} else {
console.log('postcode file does not exist')
}
this.initialized = true;
},
requestPostcodes: function(postcodes) {
if (postcodes.length == 1) {
return JSON.parse(SyncRequest('GET', 'https://api.postcodes.io/postcodes/' + postcodes[0]).body).result;
} else {
return SyncRequest('POST', 'https://api.postcodes.io/postcodes',
{ data: { postcodes: postcodes } });
}
},
update: function(postcodes, callback) {
console.log('Postcode Update ');
var pc_buffer = [];
var cache_updated = false
var _this = this;
var foundPostcodes = [];
for (var i = 0; i < postcodes.length; ++i) {
if (!(postcodes[i] in _this.existingData)) {
var pc = postcodes[i];
if (!_.isEmpty(pc)) {
cache_updated = true
if (pc_buffer.length == 100) {
var newPostcodes = _this.requestPostcodes(pc_buffer);
if (_.isEmpty(newPostcodes)) {
foundPostcodes.push(newPostcodes)
}
pc_buffer = []
} else {
pc_buffer.push(pc)
}
}
}
}
if (pc_buffer.length > 0) {
foundPostcodes.push(_this.requestPostcodes(pc_buffer))
}
console.log('Postcode Update - Found Postcodes = ' + foundPostcodes.length);
for (var i = 0; i < foundPostcodes.length; ++i) {
var pc = foundPostcodes[i];
if (!_.isEmpty(pc)) {
_this.existingData[pc.postcode] = foundPostcodes[i];
}
}
if (cache_updated) {
// this.saveCachedItems();
// runs too often with lots of individual searches
}
return foundPostcodes[0];
},
saveCachedItems: function() {
console.log('SAVING CACHED EXISTING POSTCODE DATA');
fs.writeFile(postcodeFilePath, JSON.stringify(this.existingData),function(err){
if(err) {
console.log(err);
}
})
},
get: function(postcode) {
if (!this.initialized) {
this.init();
}
if (!(postcode in this.existingData)) {
console.log('Postcode does not exist: ' + postcode);
return this.update([postcode]);
} else {
return this.existingData[postcode];
}
}
}
// Sample usage to find postcode
// var postcode = PostcodeDB.get('LS14 2HB');
// console.log('postcode = ' + JSON.stringify(postcode));
// After running queries use PostcodeDB.saveCachedItems()
// to save any newly found postcodes to the json file
module.exports = PostcodeDB;