Some refactoring here
This commit is contained in:
parent
e82d198ee7
commit
6544dc5a3d
|
|
@ -6,28 +6,34 @@ namespace AS1024.GeoFeed.Core.Tools
|
|||
public static class GeoFeedTools
|
||||
{
|
||||
/// <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>
|
||||
/// <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="isCached">If the result is cached</param>
|
||||
/// <returns></returns>
|
||||
/// <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="isCached">If the result is cached.</param>
|
||||
/// <returns>CSV formatted string of GeoFeed data.</returns>
|
||||
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();
|
||||
|
||||
// Append timestamp header if required
|
||||
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)
|
||||
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)
|
||||
{
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue