first commit
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
using System.Xml.Linq;
|
||||
using Landmark.OLA.Models.Addresses;
|
||||
using Landmark.OLA.Models.Addresses.Enums;
|
||||
|
||||
namespace USPS.Parsers
|
||||
{
|
||||
public class AddressValidationResponseParser : IResponseParser<Address>
|
||||
{
|
||||
|
||||
public bool CheckForError(XElement xdoc)
|
||||
{
|
||||
var elementAddress = xdoc.Element("Address");
|
||||
if (elementAddress == null)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
var error = elementAddress.Element("Error");
|
||||
return (error != null);
|
||||
}
|
||||
|
||||
public Address Parse(XElement xdoc)
|
||||
{
|
||||
if( xdoc == null )
|
||||
{
|
||||
throw new ArgumentNullException("Address xml is null");
|
||||
}
|
||||
|
||||
var elementAddress = xdoc.Element("Address");
|
||||
if (elementAddress == null || elementAddress.Element("Error") != null)
|
||||
{
|
||||
throw new FormatException("Attempt to parse address that is in error.");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var compareAddress = new Address
|
||||
{
|
||||
// USPS considers unit numbers (ex: APT 204) to be Address1. Our app's models would consider these to be Address2. DO NOT CHANGE
|
||||
Address2 = (string)elementAddress.Element("Address1"),
|
||||
Address1 = (string)elementAddress.Element("Address2"),
|
||||
City = (string)elementAddress.Element("City"),
|
||||
State = MakeStateOptionEnum((string)elementAddress.Element("State")),
|
||||
PostalCode = (string)elementAddress.Element("Zip5"),
|
||||
PostalCodeExt = (string)elementAddress.Element("Zip4"),
|
||||
Country = CountryOption.UnitedStates
|
||||
};
|
||||
|
||||
return compareAddress;
|
||||
}
|
||||
catch
|
||||
{
|
||||
throw new FormatException("USPS XML is in an unexpected layout.");
|
||||
}
|
||||
}
|
||||
|
||||
public static StateOption MakeStateOptionEnum(string state)
|
||||
{
|
||||
if (Enum.TryParse(state, out StateOption parsedState))
|
||||
{
|
||||
return parsedState;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new FormatException("USPS XML contains an unexpected 'State'");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user