Some refactoring here

This commit is contained in:
Jeff Leung 2024-02-27 20:53:18 -08:00
parent e82d198ee7
commit 6544dc5a3d
1 changed files with 15 additions and 9 deletions

View File

@ -6,28 +6,34 @@ namespace AS1024.GeoFeed.Core.Tools
public static class GeoFeedTools public static class GeoFeedTools
{ {
/// <summary> /// <summary>
/// Returns a CSV string for a given GeoFeed retreived from various sources /// Returns a CSV string for a given GeoFeed retrieved from various sources.
/// </summary> /// </summary>
/// <param name="geoFeeds">GeoFeed returned from the source of truth</param> /// <param name="geoFeeds">GeoFeed returned from the source of truth.</param>
/// <param name="timeStamp">If a timestamp should be appended at the header</param> /// <param name="timeStamp">If a timestamp should be appended at the header.</param>
/// <param name="isCached">If the result is cached</param> /// <param name="isCached">If the result is cached.</param>
/// <returns></returns> /// <returns>CSV formatted string of GeoFeed data.</returns>
public static string ToGeoFeedCsv(this List<IPGeoFeed> geoFeeds, bool timeStamp = false, bool isCached = false) public static string ToGeoFeedCsv(this List<IPGeoFeed> geoFeeds, bool timeStamp = false, bool isCached = false)
{ {
if (geoFeeds == null) throw new ArgumentNullException(nameof(geoFeeds));
StringBuilder csvContent = new(); StringBuilder csvContent = new();
// Append timestamp header if required
if (timeStamp) if (timeStamp)
csvContent.AppendLine($"# GeoFeed generated on {DateTime.UtcNow:R}"); csvContent.AppendFormat("# GeoFeed generated on {0:R}\n", DateTime.UtcNow);
// Append cache status if required
if (isCached) if (isCached)
csvContent.AppendLine($"# Geofeed data is returned from local in memory cache"); csvContent.AppendLine("# Geofeed data is returned from local in memory cache");
// Iterate over each GeoFeed entry to append its details to the CSV content
foreach (IPGeoFeed feed in geoFeeds) foreach (IPGeoFeed feed in geoFeeds)
{ {
csvContent.AppendLine($"{feed.Prefix},{feed.GeolocCountry},{feed.GeolocRegion},{feed.GeolocCity},{feed.GeolocPostalCode}"); // Using AppendFormat for a cleaner and more readable approach to constructing CSV lines
csvContent.AppendFormat("{0},{1},{2},{3},{4}\n", feed.Prefix, feed.GeolocCountry, feed.GeolocRegion, feed.GeolocCity, feed.GeolocPostalCode);
} }
return csvContent.ToString(); return csvContent.ToString();
} }
} }
} }