Class: RecipeParser

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

Instance Method Summary collapse

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

#agentObject



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_timeObject



79
80
81
# File 'lib/allrecipes/recipe_parser.rb', line 79

def cooking_time
  time("cook")
end

#directions_classObject



83
84
85
# File 'lib/allrecipes/recipe_parser.rb', line 83

def directions_class
  ".directions ol li span"
end

#directions_listObject



87
88
89
# File 'lib/allrecipes/recipe_parser.rb', line 87

def directions_list
  @page.search(directions_class)
end

#get_directionsObject



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_ingredientsObject



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

#imageObject



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

def image
  @page.search(image_id).first.attributes["src"].value
end

#image_idObject



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

def image_id
  "#imgPhoto"
end

#ingredient_amount_classObject



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_classObject



132
133
134
# File 'lib/allrecipes/recipe_parser.rb', line 132

def ingredient_name_class
  ".ingredient-name"
end

#ingredients_classObject



101
102
103
# File 'lib/allrecipes/recipe_parser.rb', line 101

def ingredients_class
  ".fl-ing"
end

#ingredients_listObject



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

#nameObject



23
24
25
# File 'lib/allrecipes/recipe_parser.rb', line 23

def name
  @page.search(name_id).inner_text
end

#name_idObject



27
28
29
# File 'lib/allrecipes/recipe_parser.rb', line 27

def name_id
  "#itemTitle"
end

#preparation_timeObject



75
76
77
# File 'lib/allrecipes/recipe_parser.rb', line 75

def preparation_time
  time("prep")
end

#ratingObject



47
48
49
50
51
52
53
# File 'lib/allrecipes/recipe_parser.rb', line 47

def rating
  rating_html = @page.search(rating_class)
  if rating_html.length > 0
    float_value = rating_html.attr("content").inner_text.to_f
    (float_value * 2).round / 2.0 #to convert to nearest 0.5
  end
end

#rating_classObject



55
56
57
# File 'lib/allrecipes/recipe_parser.rb', line 55

def rating_class
  ".detail-right meta[itemprop='ratingValue']"
end

#recipeObject



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: 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

#servingsObject



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_idObject



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