Class: RecipeParser
- Inherits:
-
Object
- Object
- RecipeParser
- Defined in:
- lib/allrecipes/recipe_parser.rb
Instance Method Summary collapse
- #add_ingredient_to_list(amount, ingredient_name) ⇒ Object
- #agent ⇒ Object
- #amount(ingredient) ⇒ Object
- #cooking_time ⇒ Object
- #directions_class ⇒ Object
- #directions_list ⇒ Object
- #get_directions ⇒ Object
- #get_ingredients ⇒ Object
- #hours(type) ⇒ Object
- #image ⇒ Object
- #image_id ⇒ Object
- #ingredient_amount_class ⇒ Object
- #ingredient_name(ingredient) ⇒ Object
- #ingredient_name_class ⇒ Object
- #ingredients_class ⇒ Object
- #ingredients_list ⇒ Object
-
#initialize(page) ⇒ RecipeParser
constructor
A new instance of RecipeParser.
- #minutes(type) ⇒ Object
- #name ⇒ Object
- #name_id ⇒ Object
- #preparation_time ⇒ Object
- #rating ⇒ Object
- #rating_class ⇒ Object
- #recipe ⇒ Object
- #request_page(page) ⇒ Object
- #servings ⇒ Object
- #servings_id ⇒ Object
- #time(type) ⇒ Object
Constructor Details
#initialize(page) ⇒ RecipeParser
Returns a new instance of RecipeParser.
3 4 5 6 7 8 9 |
# File 'lib/allrecipes/recipe_parser.rb', line 3 def initialize(page) @page = request_page(page) @directions = [] @ingredients = [] get_ingredients get_directions end |
Instance Method Details
#add_ingredient_to_list(amount, ingredient_name) ⇒ Object
113 114 115 116 117 118 |
# File 'lib/allrecipes/recipe_parser.rb', line 113 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 |
#agent ⇒ Object
11 12 13 |
# File 'lib/allrecipes/recipe_parser.rb', line 11 def agent Mechanize.new end |
#amount(ingredient) ⇒ Object
120 121 122 |
# File 'lib/allrecipes/recipe_parser.rb', line 120 def amount(ingredient) ingredient.search(ingredient_amount_class).children[0] end |
#cooking_time ⇒ Object
79 80 81 |
# File 'lib/allrecipes/recipe_parser.rb', line 79 def cooking_time time("cook") end |
#directions_class ⇒ Object
83 84 85 |
# File 'lib/allrecipes/recipe_parser.rb', line 83 def directions_class ".directions ol li span" end |
#directions_list ⇒ Object
87 88 89 |
# File 'lib/allrecipes/recipe_parser.rb', line 87 def directions_list @page.search(directions_class) end |
#get_directions ⇒ Object
91 92 93 94 95 |
# File 'lib/allrecipes/recipe_parser.rb', line 91 def get_directions directions_list.each do |direction| @directions << direction.text end end |
#get_ingredients ⇒ Object
105 106 107 108 109 110 111 |
# File 'lib/allrecipes/recipe_parser.rb', line 105 def get_ingredients ingredients_list.each do |ingredient| amount = amount(ingredient) ingredient_name= ingredient_name(ingredient) add_ingredient_to_list(amount, ingredient_name) end end |
#hours(type) ⇒ Object
71 72 73 |
# File 'lib/allrecipes/recipe_parser.rb', line 71 def hours(type) @page.search("##{type}HoursSpan em") end |
#image ⇒ Object
31 32 33 |
# File 'lib/allrecipes/recipe_parser.rb', line 31 def image @page.search(image_id).first.attributes["src"].value end |
#image_id ⇒ Object
35 36 37 |
# File 'lib/allrecipes/recipe_parser.rb', line 35 def image_id "#imgPhoto" end |
#ingredient_amount_class ⇒ Object
124 125 126 |
# File 'lib/allrecipes/recipe_parser.rb', line 124 def ingredient_amount_class ".ingredient-amount" end |
#ingredient_name(ingredient) ⇒ Object
128 129 130 |
# File 'lib/allrecipes/recipe_parser.rb', line 128 def ingredient_name(ingredient) ingredient.search(ingredient_name_class).children[0] end |
#ingredient_name_class ⇒ Object
132 133 134 |
# File 'lib/allrecipes/recipe_parser.rb', line 132 def ingredient_name_class ".ingredient-name" end |
#ingredients_class ⇒ Object
101 102 103 |
# File 'lib/allrecipes/recipe_parser.rb', line 101 def ingredients_class ".fl-ing" end |
#ingredients_list ⇒ Object
97 98 99 |
# File 'lib/allrecipes/recipe_parser.rb', line 97 def ingredients_list @page.search(ingredients_class) end |
#minutes(type) ⇒ Object
67 68 69 |
# File 'lib/allrecipes/recipe_parser.rb', line 67 def minutes(type) @page.search("##{type}MinsSpan em") end |
#name ⇒ Object
23 24 25 |
# File 'lib/allrecipes/recipe_parser.rb', line 23 def name @page.search(name_id).inner_text end |
#name_id ⇒ Object
27 28 29 |
# File 'lib/allrecipes/recipe_parser.rb', line 27 def name_id "#itemTitle" end |
#preparation_time ⇒ Object
75 76 77 |
# File 'lib/allrecipes/recipe_parser.rb', line 75 def preparation_time time("prep") end |
#rating ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/allrecipes/recipe_parser.rb', line 47 def = @page.search() if .length > 0 float_value = .attr("content").inner_text.to_f (float_value * 2).round / 2.0 #to convert to nearest 0.5 end end |
#rating_class ⇒ Object
55 56 57 |
# File 'lib/allrecipes/recipe_parser.rb', line 55 def ".detail-right meta[itemprop='ratingValue']" end |
#recipe ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/allrecipes/recipe_parser.rb', line 136 def recipe begin { name: name, image: image, servings: servings, ingredients: @ingredients, directions: @directions, rating: , prep_time: preparation_time, cook_time: cooking_time } rescue raise "Error getting recipe" end end |
#request_page(page) ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/allrecipes/recipe_parser.rb', line 15 def request_page(page) if page.match(/allrecipes\.com/) agent.get page else raise "Invalid URL" end end |
#servings ⇒ Object
39 40 41 |
# File 'lib/allrecipes/recipe_parser.rb', line 39 def servings @page.search(servings_id).inner_text.gsub(" servings", "").to_i end |
#servings_id ⇒ Object
43 44 45 |
# File 'lib/allrecipes/recipe_parser.rb', line 43 def servings_id "#lblYield" end |
#time(type) ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/allrecipes/recipe_parser.rb', line 59 def time(type) minutes = minutes(type) hours = hours(type) time = 0 time += minutes ? minutes.inner_text.to_i : 0 time += hours ? hours.inner_text.to_i * 60 : 0 #hours to minutes end |