Skip to content

Commit 8a765bd

Browse files
authored
Merge pull request #18 from Bandwidth/DX-1359/geocode
Dx 1359/geocode
2 parents 9053839 + 76350b8 commit 8a765bd

File tree

5 files changed

+154
-1
lines changed

5 files changed

+154
-1
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,6 +1172,27 @@ catch (e) {
11721172
}
11731173
```
11741174
1175+
1176+
## Geocoding
1177+
### Make a geocode request
1178+
1179+
```Javascript
1180+
var data = data = {
1181+
addressLine1: "900 Main Campus Dr",
1182+
city: 'raleigh',
1183+
stateCode: 'nc',
1184+
zip: 27606
1185+
}
1186+
1187+
numbers.Geocode.request(data, function(error, address) {
1188+
if (error) {
1189+
return callback(error)
1190+
}
1191+
console.log(address.stateCode, address.houseNumber, address.streetName, address.streetSuffix, address.city)
1192+
//NC, 900, Main Campus, Dr, Raleigh
1193+
});
1194+
```
1195+
11751196
## Aeuis
11761197
11771198
### List Aeuis's

lib/geocode.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
var Client = require("./client");
2+
var GEOCODE_PATH = "geocodeRequest";
3+
module.exports = {
4+
request: function(client, data, callback) {
5+
if(arguments.length === 2){
6+
callback = data;
7+
data = client;
8+
client = new Client();
9+
}
10+
var url = client.concatAccountPath(GEOCODE_PATH);
11+
client.makeRequest("post", url, {requestAddress: data}, function(err,res){
12+
if(err&&err.status !== 409){
13+
return callback(err);
14+
}
15+
if (err) { //409 means they found a geocode.
16+
return client.parseXml(err.response.res.text, function(err, results) {
17+
if (err) {
18+
return callback(err);
19+
}
20+
return callback(null, results.geocodeRequestResponse.geocodedAddress);
21+
})
22+
}
23+
return callback(null, res.geocodedAddress);
24+
})
25+
}
26+
}

lib/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ module.exports = {
3131
CsrOrder: require("./csrOrder"),
3232
EmergencyNotification: require("./emergencyNotification"),
3333
Aeuis: require("./aeuis"),
34-
Application: require('./application')
34+
Application: require('./application'),
35+
Geocode: require('./geocode')
3536
};
3637

3738
for (const property in module.exports) {

test/geocode.js

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
var lib = require("../");
2+
var helper = require("./helper");
3+
var nock = require("nock");
4+
var Geocode = lib.Geocode;
5+
6+
describe("Geocode", function(){
7+
before(function(){
8+
nock.disableNetConnect();
9+
helper.setupGlobalOptions();
10+
});
11+
after(function(){
12+
nock.cleanAll();
13+
nock.enableNetConnect();
14+
});
15+
describe("#request", function(){
16+
it("should make a geocode request", function(done){
17+
geoData = {
18+
addressLine1: "1 Street Name",
19+
city: "City",
20+
stateCode: "State",
21+
zip: "ZipCode"
22+
}
23+
helper.nock().post("/accounts/FakeAccountId/geocodeRequest", helper.buildXml({requestAddress: geoData})).reply(200, helper.xml.geocode, {"Content-Type": "application/xml"});
24+
Geocode.request(helper.createClient(), geoData, function(err, geocode){
25+
if(err){
26+
return done(err);
27+
}
28+
geocode.houseNumber.should.eql(1);
29+
geocode.streetName.should.eql("Street");
30+
geocode.streetSuffix.should.eql("Name");
31+
geocode.city.should.eql("City");
32+
geocode.stateCode.should.eql("State");
33+
geocode.zip.should.eql("ZipCode");
34+
geocode.plusFour.should.eql(1234);
35+
geocode.country.should.eql("US");
36+
done();
37+
});
38+
});
39+
40+
it("should make a geocode with default client", function(done){
41+
geoData = {
42+
addressLine1: "1 Street Name",
43+
city: "City",
44+
stateCode: "State",
45+
zip: "ZipCode"
46+
}
47+
helper.nock().post("/accounts/FakeAccountId/geocodeRequest", helper.buildXml({requestAddress: geoData})).reply(200, helper.xml.geocode, {"Content-Type": "application/xml"});
48+
Geocode.request(geoData, function(err, geocode){
49+
if(err){
50+
return done(err);
51+
}
52+
geocode.houseNumber.should.eql(1);
53+
geocode.streetName.should.eql("Street");
54+
geocode.streetSuffix.should.eql("Name");
55+
geocode.city.should.eql("City");
56+
geocode.stateCode.should.eql("State");
57+
geocode.zip.should.eql("ZipCode");
58+
geocode.plusFour.should.eql(1234);
59+
geocode.country.should.eql("US");
60+
done();
61+
});
62+
});
63+
64+
it("should handle 409 collision without error", function(done){
65+
geoData = {
66+
addressLine1: "123 Street Name",
67+
city: "City",
68+
stateCode: "State",
69+
zip: "ZipCode"
70+
}
71+
helper.nock().post("/accounts/FakeAccountId/geocodeRequest", helper.buildXml({requestAddress: geoData})).reply(409, helper.xml.geocode, {"Content-Type": "application/xml"});
72+
Geocode.request(helper.createClient(), geoData, function(err, geocode){
73+
if(err){
74+
return done(err);
75+
}
76+
geocode.houseNumber.should.eql(1);
77+
geocode.streetName.should.eql("Street");
78+
geocode.streetSuffix.should.eql("Name");
79+
geocode.city.should.eql("City");
80+
geocode.stateCode.should.eql("State");
81+
geocode.zip.should.eql("ZipCode");
82+
geocode.plusFour.should.eql(1234);
83+
geocode.country.should.eql("US");
84+
done();
85+
});
86+
});
87+
88+
it("should report errors", function(done){
89+
geoData = {
90+
addressLine1: "Bad adrress line 1",
91+
city: "City",
92+
stateCode: "State",
93+
zip: "ZipCode"
94+
}
95+
helper.nock().post("/accounts/FakeAccountId/geocodeRequest", helper.buildXml({requestAddress: geoData})).reply(400, {"Content-Type": "application/xml"});
96+
Geocode.request(helper.createClient(), geoData, function(err, geocode){
97+
if(err){
98+
return done();
99+
}
100+
done(new Error('An error is estimated'));
101+
});
102+
});
103+
});
104+
});

test/xml.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"geocode": "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><GeocodeRequestResponse><GeocodedAddress><AddressLine1>1 Street Name</AddressLine1><HouseNumber>1</HouseNumber><StreetName>Street</StreetName><StreetSuffix>Name</StreetSuffix><City>City</City><StateCode>State</StateCode><Zip>ZipCode</Zip><PlusFour>1234</PlusFour><Country>US</Country></GeocodedAddress></GeocodeRequestResponse>",
23
"peerApplications": "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><ApplicationSettingsResponse><ApplicationSettings><HttpMessagingV2AppId>100</HttpMessagingV2AppId></ApplicationSettings></ApplicationSettingsResponse>",
34
"application": "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><ApplicationProvisioningResponse><Application><ApplicationId>1</ApplicationId><ServiceType>Messaging-V2</ServiceType><AppName>Test Application</AppName><MsgCallbackUrl>http://a.com</MsgCallbackUrl></Application></ApplicationProvisioningResponse>",
45
"voiceApplication": "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><ApplicationProvisioningResponse><Application><ApplicationId>2</ApplicationId><AppName>Test Application 2</AppName><ServiceType>Voice-V2</ServiceType><CallInitiatedCallbackUrl>http://b.com</CallInitiatedCallbackUrl></Application></ApplicationProvisioningResponse>",

0 commit comments

Comments
 (0)