Skip to content

Commit

Permalink
Merge pull request #18 from Bandwidth/DX-1359/geocode
Browse files Browse the repository at this point in the history
Dx 1359/geocode
  • Loading branch information
BryanZWu authored Jun 24, 2020
2 parents 9053839 + 76350b8 commit 8a765bd
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 1 deletion.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1172,6 +1172,27 @@ catch (e) {
}
```
## Geocoding
### Make a geocode request
```Javascript
var data = data = {
addressLine1: "900 Main Campus Dr",
city: 'raleigh',
stateCode: 'nc',
zip: 27606
}

numbers.Geocode.request(data, function(error, address) {
if (error) {
return callback(error)
}
console.log(address.stateCode, address.houseNumber, address.streetName, address.streetSuffix, address.city)
//NC, 900, Main Campus, Dr, Raleigh
});
```
## Aeuis
### List Aeuis's
Expand Down
26 changes: 26 additions & 0 deletions lib/geocode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
var Client = require("./client");
var GEOCODE_PATH = "geocodeRequest";
module.exports = {
request: function(client, data, callback) {
if(arguments.length === 2){
callback = data;
data = client;
client = new Client();
}
var url = client.concatAccountPath(GEOCODE_PATH);
client.makeRequest("post", url, {requestAddress: data}, function(err,res){
if(err&&err.status !== 409){
return callback(err);
}
if (err) { //409 means they found a geocode.
return client.parseXml(err.response.res.text, function(err, results) {
if (err) {
return callback(err);
}
return callback(null, results.geocodeRequestResponse.geocodedAddress);
})
}
return callback(null, res.geocodedAddress);
})
}
}
3 changes: 2 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ module.exports = {
CsrOrder: require("./csrOrder"),
EmergencyNotification: require("./emergencyNotification"),
Aeuis: require("./aeuis"),
Application: require('./application')
Application: require('./application'),
Geocode: require('./geocode')
};

for (const property in module.exports) {
Expand Down
104 changes: 104 additions & 0 deletions test/geocode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
var lib = require("../");
var helper = require("./helper");
var nock = require("nock");
var Geocode = lib.Geocode;

describe("Geocode", function(){
before(function(){
nock.disableNetConnect();
helper.setupGlobalOptions();
});
after(function(){
nock.cleanAll();
nock.enableNetConnect();
});
describe("#request", function(){
it("should make a geocode request", function(done){
geoData = {
addressLine1: "1 Street Name",
city: "City",
stateCode: "State",
zip: "ZipCode"
}
helper.nock().post("/accounts/FakeAccountId/geocodeRequest", helper.buildXml({requestAddress: geoData})).reply(200, helper.xml.geocode, {"Content-Type": "application/xml"});
Geocode.request(helper.createClient(), geoData, function(err, geocode){
if(err){
return done(err);
}
geocode.houseNumber.should.eql(1);
geocode.streetName.should.eql("Street");
geocode.streetSuffix.should.eql("Name");
geocode.city.should.eql("City");
geocode.stateCode.should.eql("State");
geocode.zip.should.eql("ZipCode");
geocode.plusFour.should.eql(1234);
geocode.country.should.eql("US");
done();
});
});

it("should make a geocode with default client", function(done){
geoData = {
addressLine1: "1 Street Name",
city: "City",
stateCode: "State",
zip: "ZipCode"
}
helper.nock().post("/accounts/FakeAccountId/geocodeRequest", helper.buildXml({requestAddress: geoData})).reply(200, helper.xml.geocode, {"Content-Type": "application/xml"});
Geocode.request(geoData, function(err, geocode){
if(err){
return done(err);
}
geocode.houseNumber.should.eql(1);
geocode.streetName.should.eql("Street");
geocode.streetSuffix.should.eql("Name");
geocode.city.should.eql("City");
geocode.stateCode.should.eql("State");
geocode.zip.should.eql("ZipCode");
geocode.plusFour.should.eql(1234);
geocode.country.should.eql("US");
done();
});
});

it("should handle 409 collision without error", function(done){
geoData = {
addressLine1: "123 Street Name",
city: "City",
stateCode: "State",
zip: "ZipCode"
}
helper.nock().post("/accounts/FakeAccountId/geocodeRequest", helper.buildXml({requestAddress: geoData})).reply(409, helper.xml.geocode, {"Content-Type": "application/xml"});
Geocode.request(helper.createClient(), geoData, function(err, geocode){
if(err){
return done(err);
}
geocode.houseNumber.should.eql(1);
geocode.streetName.should.eql("Street");
geocode.streetSuffix.should.eql("Name");
geocode.city.should.eql("City");
geocode.stateCode.should.eql("State");
geocode.zip.should.eql("ZipCode");
geocode.plusFour.should.eql(1234);
geocode.country.should.eql("US");
done();
});
});

it("should report errors", function(done){
geoData = {
addressLine1: "Bad adrress line 1",
city: "City",
stateCode: "State",
zip: "ZipCode"
}
helper.nock().post("/accounts/FakeAccountId/geocodeRequest", helper.buildXml({requestAddress: geoData})).reply(400, {"Content-Type": "application/xml"});
Geocode.request(helper.createClient(), geoData, function(err, geocode){
if(err){
return done();
}
done(new Error('An error is estimated'));
});
});
});
});
1 change: 1 addition & 0 deletions test/xml.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"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>",
"peerApplications": "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><ApplicationSettingsResponse><ApplicationSettings><HttpMessagingV2AppId>100</HttpMessagingV2AppId></ApplicationSettings></ApplicationSettingsResponse>",
"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>",
"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>",
Expand Down

0 comments on commit 8a765bd

Please sign in to comment.