//Main method. It would be very useful to modify this class to accept the link and the collection
        //itself, so we can leave this method without hardcoded elements, making it re-usable.

        //I think the best way to accomplish this is to modify this file adding a new class parameter
        //for example: string url (the url for the json) and then, on the place we call this specific parser,
        //pass the url and the collection to parse.

        //I could extend over this possible implementation during the interview
        protected override IEnumerable <ICard> PopulateCards(IHearthstoneCardParser parser)
        {
            using (var reader = OpenTextReader("~/App_Data/cards.json"))
            {
                string[] collections = new string[] { "Basic", "Classic", "Curse of Naxxramas", "Goblins vs Gnomes" };
                return(ParseCardsFromAllCollections(reader, collections));
            }
        }
예제 #2
0
        public WhenParsingHearthstoneCard()
        {
            _cardStream = GetCardsDataAsStream();
            using (var reader = new StreamReader(_cardStream))
            {
                var data     = reader.ReadToEnd();
                var jsonData = JObject.Parse(data);
                _cardData = jsonData.ToString();
            }

            _parser = new HearthstoneCardParser();
        }
		protected override void Context()
		{
			_cardStream = GetCardsDataAsStream();
			using (var reader = new StreamReader(_cardStream))
			{
				var data = reader.ReadToEnd();
				var jsonData = JObject.Parse(data);
				_cardData = jsonData.ToString();
			}

			_parser = new HearthstoneCardParser();
		}
        protected override void Context()
        {
            _cardStream = GetCardsDataAsStream();
            using (var reader = new StreamReader(_cardStream))
            {
                var data     = reader.ReadToEnd();
                var jsonData = JArray.Parse(data);
                _cardsData = jsonData.ToString();
            }

            _parser = new HearthstoneCardParser();
        }
 protected override IEnumerable <ICard> PopulateCards(IHearthstoneCardParser parser)
 {
     using (var reader = File.OpenText(Path.Combine(_hostingEnvironment.ContentRootPath, "App_Data/cards.json")))
     {
         var    cardSets = JObject.Parse(reader.ReadToEnd());
         JToken cards;
         if (cardSets.TryGetValue("Basic", out cards) && cards.Type == JTokenType.Array)
         {
             foreach (var card in cards)
             {
                 var parsedCard = parser.Parse(card.ToString());
                 if (parsedCard != null)
                 {
                     yield return(parsedCard);
                 }
             }
         }
         if (cardSets.TryGetValue("Classic", out cards) && cards.Type == JTokenType.Array)
         {
             foreach (var card in cards)
             {
                 var parsedCard = parser.Parse(card.ToString());
                 if (parsedCard != null)
                 {
                     yield return(parsedCard);
                 }
             }
         }
         if (cardSets.TryGetValue("Curse of Naxxramas", out cards) && cards.Type == JTokenType.Array)
         {
             foreach (var card in cards)
             {
                 var parsedCard = parser.Parse(card.ToString());
                 if (parsedCard != null)
                 {
                     yield return(parsedCard);
                 }
             }
         }
         if (cardSets.TryGetValue("Goblins vs Gnomes", out cards) && cards.Type == JTokenType.Array)
         {
             foreach (var card in cards)
             {
                 var parsedCard = parser.Parse(card.ToString());
                 if (parsedCard != null)
                 {
                     yield return(parsedCard);
                 }
             }
         }
     }
 }
		protected override IEnumerable<ICard> PopulateCards(IHearthstoneCardParser parser)
		{
            List<ICard> cardsToReturn = new List<ICard>();

			using (var reader = File.OpenText(HttpContext.Current.Server.MapPath("~/App_Data/cards.json")))
			{
				var cardSets = JObject.Parse(reader.ReadToEnd());
                cardsToReturn.AddRange(GetCardsForCardType(parser, cardSets, "Basic"));
                cardsToReturn.AddRange(GetCardsForCardType(parser, cardSets, "Classic"));
                cardsToReturn.AddRange(GetCardsForCardType(parser, cardSets, "Curse of Naxxramas"));
                cardsToReturn.AddRange(GetCardsForCardType(parser, cardSets, "Goblins vs Gnomes"));
			}

            return cardsToReturn;
		}
        protected override IEnumerable <ICard> PopulateCards(IHearthstoneCardParser parser)
        {
            // My preferred way to do this would be a DTO class but found that quite difficult
            // as what we need to query ends up as property names, alternatively we could also use a JsonConvert.DeserialiseObject with a customer converter but I couldn't get that to work either.

            JToken       cardSetsAsJson;
            List <ICard> cards = new List <ICard>();

            using (var reader = File.OpenText(HttpContext.Current.Server.MapPath("~/App_Data/cards.json")))
                cardSetsAsJson = JToken.Parse(reader.ReadToEnd());

            foreach (var cardSet in new[] { cardSetsAsJson["Basic"], cardSetsAsJson["Curse of Naxxramas"], cardSetsAsJson["Classic"], cardSetsAsJson["Goblins vs Gnomes"] })
            {
                cards.AddRange(parser.ParseArray(cardSet.ToString()));
            }

            return(cards);
        }
        private IEnumerable<ICard> GetCardsForCardType(IHearthstoneCardParser parser, JObject cardSets, string type)
        {
            List<ICard> parsedCards = new List<ICard>();
            JToken cards;

            if (cardSets.TryGetValue(type, out cards) && cards.Type == JTokenType.Array)
            {
                foreach (var card in cards)
                {
                    var parsedCard = parser.Parse(card.ToString());
                    if (parsedCard != null)
                    {
                        parsedCards.Add(parsedCard);
                    }
                }
            }

            return parsedCards;
        }
        protected override IEnumerable <ICard> PopulateCards(IHearthstoneCardParser parser)
        {
            using (var reader = File.OpenText(HttpContext.Current.Server.MapPath("~/App_Data/cards.json")))
            {
                JObject cardSets = JObject.Parse(reader.ReadToEnd());
                //JToken cards;
                List <string> acceptedCards = new List <string>()
                {
                    "Basic", "Classic", "Curse of Naxxramas", "Goblins vs Gnomes"
                };
                foreach (KeyValuePair <string, JToken> cardList in cardSets)
                {
                    string cardsType = cardList.Key;
                    if (acceptedCards.Contains(cardsType))
                    {
                        foreach (var card in cardList.Value)
                        {
                            var parsedCard = parser.Parse(card.ToString());
                            if (parsedCard != null)
                            {
                                if (string.IsNullOrEmpty(parsedCard.PlayerClass))
                                {
                                    parsedCard.PlayerClass = "Neutral";
                                }
                                yield return(parsedCard);
                            }
                        }
                    }
                }

                /*if (cardSets.TryGetValue("Basic", out cards) && cards.Type == JTokenType.Array)
                 * {
                 *  foreach (var card in cards)
                 *  {
                 *      var parsedCard = parser.Parse(card.ToString());
                 *      if (parsedCard != null)
                 *      {
                 *          yield return parsedCard;
                 *      }
                 *  }
                 * }
                 * if (cardSets.TryGetValue("Classic", out cards) && cards.Type == JTokenType.Array)
                 * {
                 *  foreach (var card in cards)
                 *  {
                 *      var parsedCard = parser.Parse(card.ToString());
                 *      if (parsedCard != null)
                 *      {
                 *          yield return parsedCard;
                 *      }
                 *  }
                 * }
                 * if (cardSets.TryGetValue("Curse of Naxxramas", out cards) && cards.Type == JTokenType.Array)
                 * {
                 *  foreach (var card in cards)
                 *  {
                 *      var parsedCard = parser.Parse(card.ToString());
                 *      if (parsedCard != null)
                 *      {
                 *          yield return parsedCard;
                 *      }
                 *  }
                 * }
                 * if (cardSets.TryGetValue("Goblins vs Gnomes", out cards) && cards.Type == JTokenType.Array)
                 * {
                 *  foreach (var card in cards)
                 *  {
                 *      var parsedCard = parser.Parse(card.ToString());
                 *      if (parsedCard != null)
                 *      {
                 *          yield return parsedCard;
                 *      }
                 *  }
                 * }*/
            }
        }
 public LocalJsonFeedHearthstoneCardCacheFactory(IHearthstoneCardParser parser) : base(parser)
 {
 }
 public LocalJsonFeedHearthstoneCardCacheFactory(IHearthstoneCardParser parser, IHostingEnvironment hostingEnvironment) : base(parser)
 {
     _hostingEnvironment = hostingEnvironment;
 }
		protected abstract IEnumerable<ICard> PopulateCards(IHearthstoneCardParser parser);
		protected HearthstoneCardCacheFactory(IHearthstoneCardParser parser)
		{
			_parser = parser;
		}
예제 #14
0
 protected abstract IEnumerable <ICard> PopulateCards(IHearthstoneCardParser parser);
예제 #15
0
 protected HearthstoneCardCacheFactory(IHearthstoneCardParser parser)
 {
     _parser = parser;
 }
		public LocalJsonFeedHearthstoneCardCacheFactory(IHearthstoneCardParser parser) : base(parser)
		{
		}