Class: SeriousEats::Scraper

Inherits:
Object
  • Object
show all
Defined in:
lib/serious-eats/scraper.rb

Constant Summary collapse

INDEX_URL =
"https://www.seriouseats.com/the-food-lab/recipes"

Instance Method Summary collapse

Instance Method Details

#fetch_recipe_data(recipe) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/serious-eats/scraper.rb', line 27

def fetch_recipe_data(recipe)
  block = get_recipe_data(recipe)

  recipe.description = block.css(".recipe-introduction-body p:not(.caption)").map do |description|
    description.text.strip
  end.select do |description|
    # some recipes have some empty <p> tags that need to be filtered out
    description.length > 0
  end

  recipe.portion = block.css("span.info.yield").text.strip
  recipe.active_time = block.css("ul.recipe-about li:nth-child(2) span.info").text.strip
  recipe.total_time = block.css("ul.recipe-about li:nth-child(3) span.info").text.strip
  recipe.rating = block.css("span.info.rating-value").text.strip.to_f.round(1)

  recipe.ingredients = block.css("li.ingredient").map do |ingredient|
    text = ingredient.text.strip

    if ingredient.css("strong").any?
      text = "\n#{text.underline}"
    end

    text
  end

  recipe.directions = block.css(".recipe-procedure-text").map do |direction|
    direction.text.strip
  end
end

#fetch_recipesObject



17
18
19
20
21
22
23
24
25
# File 'lib/serious-eats/scraper.rb', line 17

def fetch_recipes
  get_recipes.map do |block|
    recipe = Recipe.new
    recipe.name = block.css("h4.title").text.strip
    recipe.category = block.css("a.category-link").text.strip
    recipe.url = block.css("a.module__link").attribute("href").value
    recipe
  end
end

#get_recipe_data(recipe) ⇒ Object



13
14
15
# File 'lib/serious-eats/scraper.rb', line 13

def get_recipe_data(recipe)
  parse_url(recipe.url).css("div.recipe-body")
end

#get_recipesObject



9
10
11
# File 'lib/serious-eats/scraper.rb', line 9

def get_recipes
  parse_url(INDEX_URL).css("#recipes .module")
end

#parse_url(url) ⇒ Object



5
6
7
# File 'lib/serious-eats/scraper.rb', line 5

def parse_url(url)
  Nokogiri::HTML(open(url))
end