Class: IngredientsParser

Inherits:
Object
  • Object
show all
Defined in:
lib/allrecipes/ingredients_parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(page) ⇒ IngredientsParser

Returns a new instance of IngredientsParser.



2
3
4
5
6
# File 'lib/allrecipes/ingredients_parser.rb', line 2

def initialize(page)
  @page = page
  @ingredients = []
  parse_ingredients
end

Instance Method Details

#add_ingredient_to_list(amount, ingredient_name) ⇒ Object



24
25
26
27
28
29
# 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) ⇒ Object



31
32
33
# File 'lib/allrecipes/ingredients_parser.rb', line 31

def amount(ingredient)
  ingredient.search(ingredient_amount_class).children[0]
end

#ingredient_amount_classObject



35
36
37
# File 'lib/allrecipes/ingredients_parser.rb', line 35

def ingredient_amount_class
  ".ingredient-amount"
end

#ingredient_name(ingredient) ⇒ Object



39
40
41
# File 'lib/allrecipes/ingredients_parser.rb', line 39

def ingredient_name(ingredient)
  ingredient.search(ingredient_name_class).children[0]
end

#ingredient_name_classObject



43
44
45
# File 'lib/allrecipes/ingredients_parser.rb', line 43

def ingredient_name_class
  ".ingredient-name"
end

#ingredientsObject



47
48
49
# File 'lib/allrecipes/ingredients_parser.rb', line 47

def ingredients
  @ingredients
end

#ingredients_classObject



20
21
22
# File 'lib/allrecipes/ingredients_parser.rb', line 20

def ingredients_class
  ".fl-ing"
end

#ingredients_listObject



16
17
18
# File 'lib/allrecipes/ingredients_parser.rb', line 16

def ingredients_list
  @page.search(ingredients_class)
end

#parse_ingredientsObject



8
9
10
11
12
13
14
# 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