Ejemplo n.º 1
0
        public void it_should_return_food()
        {
            var scrapper = new XPathScrapper();
            var scrap = new Scrap
            {
                CaloriesXPath =  @"//*[@id=""prod-detail""]/section/section[2]/table/tbody/tr[2]/td[3]",
                ProteinXPath = @"//*[@id=""prod-detail""]/section/section[2]/table/tbody/tr[3]/td[3]",
                CarbohydrateXPath = @"//*[@id=""prod-detail""]/section/section[2]/table/tbody/tr[12]/td[3]",
                FatXPath = @"//*[@id=""prod-detail""]/section/section[2]/table/tbody/tr[4]/td[3]",
                ProductXPath = @"//*[@id=""subPageTitle""]"
            };

            var results = scrapper.Scrap(scrap, File.ReadAllText("Data/john-west.html"));
            var expected = new Food
            {
                Brand = "John West",
                Product = "Pole and Line Skipjack Tuna in Springwater 185g",
                Calories = 115,
                Protein = 26.4M,
                Fat = 0.9M,
                Carbohydrate = 0
            };

            Assert.Equal(expected.Calories, results.Food.Calories);
            Assert.Equal(expected.Protein, results.Food.Protein);
            Assert.Equal(expected.Fat, results.Food.Fat);
            Assert.Equal(expected.Carbohydrate, results.Food.Carbohydrate);
            Assert.Equal(expected.Product, results.Food.Product);
        }
Ejemplo n.º 2
0
 public ActionResult Edit(Food request)
 {
     db.Update(request);
     return View(request);
 }
Ejemplo n.º 3
0
 public ActionResult Delete(Food request)
 {
     db.Delete<Food>(f => f.Id == request.Id);
     return RedirectToAction("Index");
 }
Ejemplo n.º 4
0
 public ActionResult Add(Food request)
 {
     db.Insert(request);
     return RedirectToAction("Index");
 }
Ejemplo n.º 5
0
        public ScrappedContent Scrap(Scrap scrap, string content)
        {
            var doc = new HtmlDocument();
            doc.LoadHtml(content);

            var food = new Food();

            if (!String.IsNullOrEmpty(scrap.BrandXPath))
            {
                Log("Brand");
                food.Brand = GetString(doc, scrap.BrandXPath);
            }

            if (!String.IsNullOrEmpty(scrap.ProductXPath))
            {
                Log("Product");
                food.Product = GetString(doc, scrap.ProductXPath);
            }

            if (!String.IsNullOrEmpty(scrap.CaloriesXPath))
            {
                Log("Calories");
                food.Calories = GetDecimal(doc, scrap.CaloriesXPath);
            }

            if (!String.IsNullOrEmpty(scrap.CarbohydrateXPath))
            {
                Log("Carbohydrate");
                food.Carbohydrate = GetDecimal(doc, scrap.CarbohydrateXPath);
            }

            if (!String.IsNullOrEmpty(scrap.ProteinXPath))
            {
                Log("Protein");
                food.Protein = GetDecimal(doc, scrap.ProteinXPath);
            }

            if (!String.IsNullOrEmpty(scrap.FatXPath))
            {
                Log("Fat");
                food.Fat = GetDecimal(doc, scrap.FatXPath);
            }

            return new ScrappedContent
            {
                Food = food,
                Messages = _log.ToString()
            };
        }