class IngredientsParser

Public Class Methods

new(page) click to toggle source
# File lib/allrecipes/ingredients_parser.rb, line 2
def initialize(page)
  @page = page
  @ingredients = []
  parse_ingredients
end

Public Instance Methods

add_ingredient_to_list(amount, ingredient_name) click to toggle source
# File lib/allrecipes/ingredients_parser.rb, line 24
def add_ingredient_to_list(amount, ingredient_name)
  if amount && ingredient_name #some recipes have empty ingredients
    quantity,unit = amount.text.split(" ")
    @ingredients << { quantity: quantity.to_f, unit: unit, name: ingredient_name.text }
  end
end
amount(ingredient) click to toggle source
# File lib/allrecipes/ingredients_parser.rb, line 31
def amount(ingredient)
  ingredient.search(ingredient_amount_class).children[0]
end
ingredient_amount_class() click to toggle source
# File lib/allrecipes/ingredients_parser.rb, line 35
def ingredient_amount_class
  ".ingredient-amount"
end
ingredient_name(ingredient) click to toggle source
# File lib/allrecipes/ingredients_parser.rb, line 39
def ingredient_name(ingredient)
  ingredient.search(ingredient_name_class).children[0]
end
ingredient_name_class() click to toggle source
# File lib/allrecipes/ingredients_parser.rb, line 43
def ingredient_name_class
  ".ingredient-name"
end
ingredients() click to toggle source
# File lib/allrecipes/ingredients_parser.rb, line 47
def ingredients
  @ingredients
end
ingredients_class() click to toggle source
# File lib/allrecipes/ingredients_parser.rb, line 20
def ingredients_class
  ".fl-ing"
end
ingredients_list() click to toggle source
# File lib/allrecipes/ingredients_parser.rb, line 16
def ingredients_list
  @page.search(ingredients_class)
end
parse_ingredients() click to toggle source
# File lib/allrecipes/ingredients_parser.rb, line 8
def parse_ingredients
  ingredients_list.each do |ingredient|
    amount = amount(ingredient)
    ingredient_name= ingredient_name(ingredient)
    add_ingredient_to_list(amount, ingredient_name)
  end
end