Esempio n. 1
0
        public void ShouldReturnTrueIfPostIsValid()
        {
            var post = new HackerNewsPost
            {
                Author   = "some one",
                Comments = 1,
                Points   = 1,
                Rank     = 1,
                Title    = "some title",
                Url      = "https://somewhere.com"
            };

            Assert.True(_hackerNewsPostItemValidator.IsValid(post));
        }
Esempio n. 2
0
        public async Task <List <HackerNewsPost> > GetPosts(int numberOfPosts)
        {
            List <HackerNewsPost> result = new List <HackerNewsPost>();
            int currentPage = 1;

            do
            {
                //download page
                var pageData = await _hackerNewsServiceAgent.GetDataFromPage(currentPage ++).ConfigureAwait(false);

                if (_pageParser.IsPageValid(pageData))
                {
                    //parse and get all posts
                    var posts = _pageParser.GetHackerNewsPosts(pageData);
                    posts.ForEach(p =>
                    {
                        //save only those that passes validation
                        if (_hackerNewsPostItemValidator.IsValid(p))
                        {
                            result.Add(p);
                        }
                    });
                }
            } while (result.Count < numberOfPosts);

            //return only desired number of posts
            if (result.Count > numberOfPosts)
            {
                result = result.Take(numberOfPosts).ToList();
            }
            return(result);
        }