using AS1024.GeoFeed.Models; using System.Text; namespace AS1024.GeoFeed.Core.Tools { public static class GeoFeedTools { /// /// Returns a CSV string for a given GeoFeed retrieved from various sources. /// /// GeoFeed returned from the source of truth. /// If a timestamp should be appended at the header. /// If the result is cached. /// CSV formatted string of GeoFeed data. public static string ToGeoFeedCsv(this List 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.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"); // Iterate over each GeoFeed entry to append its details to the CSV content foreach (IPGeoFeed feed in geoFeeds) { // 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(); } } }