-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from Bandwidth/DX-1359/geocode
Dx 1359/geocode
- Loading branch information
Showing
5 changed files
with
154 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters