Class: TodaysTopDesserts::Scraper

Inherits:
Object
  • Object
show all
Defined in:
lib/todays_top_desserts/scraper.rb

Class Method Summary collapse

Class Method Details

.scrape_dessertsObject



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/todays_top_desserts/scraper.rb', line 3

def self.scrape_desserts
  #returns an array of recipes with a name and url.
  page = Nokogiri::HTML(open("https://www.allrecipes.com/recipes/79/desserts/"))

  page.css("article.list-recipes")[0].css("li")[0..9].collect do |dessert|
      {:name => dessert.css("img").attr("title").text,
        :url => dessert.css("a").attr("href").value
      }
  end

end

.scrape_recipe(recipe_url) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/todays_top_desserts/scraper.rb', line 15

def self.scrape_recipe(recipe_url)
  #returns a hash of recipe attributes
  page = Nokogiri::HTML(open(recipe_url))

  recipe = {}

  recipe[:author] = page.css(".submitter__name").text.strip
  recipe[:description] = page.css(".submitter__description").text.strip
  recipe[:serving_size] = page.css("#metaRecipeServings").attr("content").value
  recipe[:prep_time] = page.css("time[itemprop='prepTime']").text.strip
  recipe[:cook_time] = page.css("time[itemprop='cookTime']").text.strip
  recipe[:ready_time] = page.css("time[itemprop='totalTime']").text.strip
  recipe[:calorie_count] = page.css(".calorie-count").text.strip

  recipe[:ingredients] = page.css("span[itemprop='ingredients']").collect {|ingredient| ingredient.text.strip}

  recipe[:instructions] = page.css("ol[itemprop='recipeInstructions'] span.recipe-directions__list--item").collect {|instruction| instruction.text.strip}

  recipe

end