From 58e5a0136d66ab8ddb180cff81907ed6d5f5f1fd Mon Sep 17 00:00:00 2001 From: Rob Kennedy Date: Tue, 4 May 2021 22:40:35 -0400 Subject: [PATCH] Added additional ToAddress extension method for string claim values. --- src/Extensions/ContactExtensions.cs | 45 +++++++++++++++++++---------- src/Talegen.Common.Models.csproj | 4 +-- 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/src/Extensions/ContactExtensions.cs b/src/Extensions/ContactExtensions.cs index 45ffdc8..c53a38a 100644 --- a/src/Extensions/ContactExtensions.cs +++ b/src/Extensions/ContactExtensions.cs @@ -36,7 +36,7 @@ public static class ContactExtensions /// This extension method converts a JWT address claim to a model object. /// /// The claim containing the address information. - /// + /// Returns a new Address model containing found claim address details. /// Exception is thrown if is null. public static Address ToAddress(this Claim claim) { @@ -50,23 +50,36 @@ public static Address ToAddress(this Claim claim) // is a JWT address claim if (claim.Type == JwtAddressClaimType && !string.IsNullOrWhiteSpace(claim.Value)) { - JwtAddressModel jwtModel = JsonConvert.DeserializeObject(claim.Value); + result = claim.Value.ToAddress(); + } - if (jwtModel != null) - { - string[] streetLines = jwtModel.StreetAddress.Split('\n'); + return result; + } - result = new Address - { - Street1 = streetLines != null ? streetLines[0].Replace("\r", string.Empty).Replace("\n", string.Empty) : string.Empty, - Street2 = streetLines != null && streetLines.Length > 1 ? streetLines[1].Replace("\r", string.Empty).Replace("\n", string.Empty) : string.Empty, - City = jwtModel.Locality, - Country = jwtModel.Country, - PostalCode = jwtModel.PostalCode, - RegionState = jwtModel.Region, - Formatted = jwtModel.Formatted - }; - } + /// + /// This extension method converts a JWT address claim to a model object. + /// + /// The claim value containing the address information. + /// Returns a new Address model containing found claim address details. + public static Address ToAddress(this string claimValue) + { + Address result = null; + JwtAddressModel jwtModel = JsonConvert.DeserializeObject(claimValue); + + if (jwtModel != null) + { + string[] streetLines = jwtModel.StreetAddress.Split('\n'); + + result = new Address + { + Street1 = streetLines != null ? streetLines[0].Replace("\r", string.Empty).Replace("\n", string.Empty) : string.Empty, + Street2 = streetLines != null && streetLines.Length > 1 ? streetLines[1].Replace("\r", string.Empty).Replace("\n", string.Empty) : string.Empty, + City = jwtModel.Locality, + Country = jwtModel.Country, + PostalCode = jwtModel.PostalCode, + RegionState = jwtModel.Region, + Formatted = jwtModel.Formatted + }; } return result; diff --git a/src/Talegen.Common.Models.csproj b/src/Talegen.Common.Models.csproj index 7f78ecb..6d217e3 100644 --- a/src/Talegen.Common.Models.csproj +++ b/src/Talegen.Common.Models.csproj @@ -13,10 +13,10 @@ https://github.com/Talegen/Talegen.Common.Models git Models - Added additional modelling and extensions for address models. + Added additional ToAddress extension method for string claim values. Assets\logo.ico false - 1.0.3.0 + 1.0.4.0 en