Пример #1
0
        /// <summary>
        /// Helper method that returns an <see cref="AirQuality"/> instance based on values stored in a dictionary
        /// </summary>
        /// <param name="dict"></param>
        /// <returns></returns>
        public static AirQuality CreateAirQuality(IDictionary <string, string> dict)
        {
            AirQuality quality = null;

            if (!String.IsNullOrWhiteSpace(dict?["Location"]))
            {
                var location = dict["Location"].Split(',');
                quality = new AirQuality
                {
                    Location = location.Length == 2
                        ? ValueTuple.Create(location[0], location[1])
                        : ValueTuple.Create(location[0], ""),
                    LastUpdate         = dict["Last Update"],
                    ParticlePollution  = dict["Particle Pollution"],
                    Ozone              = dict["Ozone"],
                    Agency             = dict["Agency"],
                    LocationIdentifier = int.Parse(dict["LocationIdentifier"])
                };
            }
            else
            {
                quality = new NullAirQuality();
            }
            return(quality);
        }
Пример #2
0
 /// <summary>
 /// Parses an RSS feed and returns a <see cref="AirQuality"/>.
 /// </summary>
 public virtual AirQuality ParseRss(string rawFeed, int locationIdentifier)
 {
     try
     {
         XDocument doc = XDocument.Parse(rawFeed);
         if (doc.Root != null)
         {
             var channel = doc.Root.Descendants().First(i => i.Name.LocalName == "channel");
             // RSS/Channel/item
             var entries = from item in channel.Elements().Where(i => i.Name.LocalName == "item")
                           select new
             {
                 Content = item.Elements().First(i => i.Name.LocalName == "description").Value
             };
             return(AirQuality.CreateAirQuality(GetAirQualityData(entries.FirstOrDefault()?.Content, locationIdentifier, FeedType.Rss)));
         }
     }
     catch
     {}
     return(new NullAirQuality());
 }