Class: RecipeParser

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(page, keys = nil) ⇒ RecipeParser

Returns a new instance of RecipeParser.



4
5
6
7
8
9
10
11
# File 'lib/allrecipes/recipe_parser.rb', line 4

def initialize(page, keys=nil)
  @page = request_page(page)
  @keys = keys
  @directions = []
  @recipe = {}
  get_directions
  populate_recipe
end

Instance Attribute Details

#directionsObject

Returns the value of attribute directions.



2
3
4
# File 'lib/allrecipes/recipe_parser.rb', line 2

def directions
  @directions
end

#recipeObject

Returns the value of attribute recipe.



2
3
4
# File 'lib/allrecipes/recipe_parser.rb', line 2

def recipe
  @recipe
end

Instance Method Details

#agentObject



13
14
15
# File 'lib/allrecipes/recipe_parser.rb', line 13

def agent
  Mechanize.new
end

#cook_timeObject



81
82
83
# File 'lib/allrecipes/recipe_parser.rb', line 81

def cook_time
  time("cook")
end

#default_keysObject



103
104
105
# File 'lib/allrecipes/recipe_parser.rb', line 103

def default_keys
  %w{name image servings ingredients directions rating prep_time cook_time}
end

#directions_classObject



85
86
87
# File 'lib/allrecipes/recipe_parser.rb', line 85

def directions_class
  ".directions ol li span"
end

#directions_listObject



89
90
91
# File 'lib/allrecipes/recipe_parser.rb', line 89

def directions_list
  @page.search(directions_class)
end

#get_directionsObject



93
94
95
96
97
# File 'lib/allrecipes/recipe_parser.rb', line 93

def get_directions
  directions_list.each do |direction|
    @directions << direction.text
  end
end

#hours(type) ⇒ Object



73
74
75
# File 'lib/allrecipes/recipe_parser.rb', line 73

def hours(type)
  @page.search("##{type}HoursSpan em")
end

#imageObject



33
34
35
# File 'lib/allrecipes/recipe_parser.rb', line 33

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

#image_idObject



37
38
39
# File 'lib/allrecipes/recipe_parser.rb', line 37

def image_id
  "#imgPhoto"
end

#ingredientsObject



99
100
101
# File 'lib/allrecipes/recipe_parser.rb', line 99

def ingredients
  @ingredients ||= IngredientsParser.new(@page).ingredients
end

#minutes(type) ⇒ Object



69
70
71
# File 'lib/allrecipes/recipe_parser.rb', line 69

def minutes(type)
  @page.search("##{type}MinsSpan em")
end

#nameObject



25
26
27
# File 'lib/allrecipes/recipe_parser.rb', line 25

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

#name_idObject



29
30
31
# File 'lib/allrecipes/recipe_parser.rb', line 29

def name_id
  "#itemTitle"
end

#populate_recipeObject



115
116
117
118
119
120
121
# File 'lib/allrecipes/recipe_parser.rb', line 115

def populate_recipe
  begin
    sanitized_keys.each{ |key| @recipe[key.to_sym] = send(key) }
  rescue
    raise "Error getting recipe"
  end
end

#prep_timeObject



77
78
79
# File 'lib/allrecipes/recipe_parser.rb', line 77

def prep_time
  time("prep")
end

#ratingObject



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

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



57
58
59
# File 'lib/allrecipes/recipe_parser.rb', line 57

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

#request_page(page) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/allrecipes/recipe_parser.rb', line 17

def request_page(page)
  if page.match(/allrecipes\.com/)
    agent.get page
  else
    raise "Invalid URL"
  end
end

#sanitized_keysObject



107
108
109
110
111
112
113
# File 'lib/allrecipes/recipe_parser.rb', line 107

def sanitized_keys
  if @keys && @keys.count > 0
    @keys.select{ |key| default_keys.include?(key) }
  else
    default_keys
  end
end

#servingsObject



41
42
43
# File 'lib/allrecipes/recipe_parser.rb', line 41

def servings
  @page.search(servings_id).inner_text.gsub(" servings", "").to_i
end

#servings_idObject



45
46
47
# File 'lib/allrecipes/recipe_parser.rb', line 45

def servings_id
  "#lblYield"
end

#time(type) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/allrecipes/recipe_parser.rb', line 61

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