forked from owenobyrne/node-postcodeanywhere
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
107 lines (99 loc) · 2.63 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
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
var requestify = require('requestify');
var baseURL = "http://services.postcodeanywhere.co.uk";
var KEY = "";
exports.initialize = function(key) {
KEY = key;
};
exports.CapturePlusInteractiveFind = function(data, next) {
requestify.request(baseURL + "/CapturePlus/Interactive/Find/v2.10/json3.ws", {
method: "POST",
params: {
Key: KEY,
SearchTerm: data.searchTerm,
LastId : data.lastId,
SearchFor : data.searchFor,
Country : data.country,
LanguagePreference : data.languagePreference,
MaxSuggestions : data.maxSuggestions,
MaxResults : data.maxResults
},
dataType: "json"
}
)
.then(function(response) {
var body = JSON.parse(response.body);
// Test for an error
if (body.Items.length == 1 && typeof(body.Items[0].Error) != "undefined") {
// Show the error message
next( {
description: body.Items[0].Description
}, null);
}
else {
// Check if there were any items found
if (body.Items.length == 0)
next(null, []);
else {
next(null, body.Items);
}
}
});
};
exports.CapturePlusInteractiveRetrieve = function(data, next) {
requestify.request(baseURL + "/CapturePlus/Interactive/Retrieve/v2.10/json3.ws", {
method: "POST",
params: {
Key: KEY,
Id: data.id
},
dataType: "json"
}
)
.then(function(response) {
var body = JSON.parse(response.body);
// Test for an error
if (body.Items.length == 1 && typeof(body.Items[0].Error) != "undefined") {
// Show the error message
next( {
description: body.Items[0].Description
}, null);
}
else {
// Check if there were any items found
if (body.Items.length == 0)
next(null, []);
else {
next(null, body.Items);
}
}
});
};
exports.RetrieveByAddress= function(data, next) {
requestify.request(baseURL + "/PostcodeAnywhere/Interactive/RetrieveByAddress/v1.20/json3.ws", {
method: "POST",
params: {
Key: KEY,
Address: data.address,
Company: data.company
},
dataType: "json"
})
.then(function(response) {
var body = JSON.parse(response.body);
// Test for an error
if (body.Items.length == 1 && typeof(body.Items[0].Error) != "undefined") {
// Show the error message
next( {
description: body.Items[0].Description
}, null);
}
else {
// Check if there were any items found
if (body.Items.length == 0)
next(null, []);
else {
next(null, body.Items);
}
}
});
};