using System; using System.Collections; using System.Collections.Generic; using System.Runtime.Serialization; namespace Website { [DataContract] public class FeedItem { public const int MAX_SUMMARY_LENGTH = 250; public const string SUMMARY_FORMAT = "{0}... [more]"; [DataMember] public string Title { get; private set; } [DataMember] public string Link { get; private set; } [DataMember] public string Text { get; private set; } public string Summary { get { if(string.IsNullOrEmpty(Text)) return string.Empty; var text = RemoveHtml(Text); return text.Length < MAX_SUMMARY_LENGTH ? text : string.Format(SUMMARY_FORMAT, text.Substring(0, MAX_SUMMARY_LENGTH), Link); } } [DataMember] public DateTime? PostDate { get; private set; } public FeedItem() { } public FeedItem(string title, string link, string text, string pubDate) : this(title, link, text, (DateTime?)null) { DateTime postDate; DateTime.TryParse(pubDate, out postDate); PostDate = (postDate == default(DateTime)) ? (DateTime?)null : postDate; } public FeedItem(string title, string link, string text, DateTime? postDate) { Title = title; Link = link; Text = text; PostDate = postDate; } public override string ToString() { return (PostDate.HasValue) ? string.Format("{0:D}: {1}", PostDate.Value, Title) : Title; } private static string RemoveHtml(string text) { return System.Text.RegularExpressions.Regex.Replace(text, "<[^>]*>", ""); } } [DataContract] public class FeedItemCollection : IEnumerable { [DataMember] private readonly IEnumerable items; public FeedItemCollection(IEnumerable items) { this.items = items; } public IEnumerator GetEnumerator() { return items.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } } }