diff --git a/README.md b/README.md index 57e67ee..15c2662 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/geocode.js b/lib/geocode.js new file mode 100644 index 0000000..a2953ff --- /dev/null +++ b/lib/geocode.js @@ -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); + }) + } +} diff --git a/lib/index.js b/lib/index.js index 71981ea..cef2af7 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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) { diff --git a/test/geocode.js b/test/geocode.js new file mode 100644 index 0000000..ab67796 --- /dev/null +++ b/test/geocode.js @@ -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')); + }); + }); + }); +}); diff --git a/test/xml.json b/test/xml.json index 8599251..26acc92 100644 --- a/test/xml.json +++ b/test/xml.json @@ -1,4 +1,5 @@ { + "geocode": "1 Street Name1StreetNameCityStateZipCode1234US", "peerApplications": "100", "application": "1Messaging-V2Test Applicationhttp://a.com", "voiceApplication": "2Test Application 2Voice-V2http://b.com",