Class: IngredientsParser
- Inherits:
-
Object
- Object
- IngredientsParser
- Defined in:
- lib/allrecipes/ingredients_parser.rb
Instance Method Summary collapse
- #add_ingredient_to_list(amount, ingredient_name) ⇒ Object
- #amount(ingredient) ⇒ Object
- #ingredient_amount_class ⇒ Object
- #ingredient_name(ingredient) ⇒ Object
- #ingredient_name_class ⇒ Object
- #ingredients ⇒ Object
- #ingredients_class ⇒ Object
- #ingredients_list ⇒ Object
-
#initialize(page) ⇒ IngredientsParser
constructor
A new instance of IngredientsParser.
- #parse_ingredients ⇒ Object
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_class ⇒ Object
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_class ⇒ Object
43 44 45 |
# File 'lib/allrecipes/ingredients_parser.rb', line 43 def ingredient_name_class ".ingredient-name" end |
#ingredients ⇒ Object
47 48 49 |
# File 'lib/allrecipes/ingredients_parser.rb', line 47 def ingredients @ingredients end |
#ingredients_class ⇒ Object
20 21 22 |
# File 'lib/allrecipes/ingredients_parser.rb', line 20 def ingredients_class ".fl-ing" end |
#ingredients_list ⇒ Object
16 17 18 |
# File 'lib/allrecipes/ingredients_parser.rb', line 16 def ingredients_list @page.search(ingredients_class) end |
#parse_ingredients ⇒ Object
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 |