Class: RecipeParser
- Inherits:
-
Object
- Object
- RecipeParser
- Defined in:
- lib/allrecipes/recipe_parser.rb
Instance Attribute Summary collapse
-
#directions ⇒ Object
Returns the value of attribute directions.
-
#recipe ⇒ Object
Returns the value of attribute recipe.
Instance Method Summary collapse
- #agent ⇒ Object
- #cook_time ⇒ Object
- #default_keys ⇒ Object
- #directions_class ⇒ Object
- #directions_list ⇒ Object
- #get_directions ⇒ Object
- #hours(type) ⇒ Object
- #image ⇒ Object
- #image_id ⇒ Object
- #ingredients ⇒ Object
-
#initialize(page, keys = nil) ⇒ RecipeParser
constructor
A new instance of RecipeParser.
- #minutes(type) ⇒ Object
- #name ⇒ Object
- #name_id ⇒ Object
- #populate_recipe ⇒ Object
- #prep_time ⇒ Object
- #rating ⇒ Object
- #rating_class ⇒ Object
- #request_page(page) ⇒ Object
- #sanitized_keys ⇒ Object
- #servings ⇒ Object
- #servings_id ⇒ Object
- #time(type) ⇒ Object
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
#directions ⇒ Object
Returns the value of attribute directions.
2 3 4 |
# File 'lib/allrecipes/recipe_parser.rb', line 2 def directions @directions end |
#recipe ⇒ Object
Returns the value of attribute recipe.
2 3 4 |
# File 'lib/allrecipes/recipe_parser.rb', line 2 def recipe @recipe end |
Instance Method Details
#agent ⇒ Object
13 14 15 |
# File 'lib/allrecipes/recipe_parser.rb', line 13 def agent Mechanize.new end |
#cook_time ⇒ Object
81 82 83 |
# File 'lib/allrecipes/recipe_parser.rb', line 81 def cook_time time("cook") end |
#default_keys ⇒ Object
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_class ⇒ Object
85 86 87 |
# File 'lib/allrecipes/recipe_parser.rb', line 85 def directions_class ".directions ol li span" end |
#directions_list ⇒ Object
89 90 91 |
# File 'lib/allrecipes/recipe_parser.rb', line 89 def directions_list @page.search(directions_class) end |
#get_directions ⇒ Object
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 |
#image ⇒ Object
33 34 35 |
# File 'lib/allrecipes/recipe_parser.rb', line 33 def image @page.search(image_id).first.attributes["src"].value end |
#image_id ⇒ Object
37 38 39 |
# File 'lib/allrecipes/recipe_parser.rb', line 37 def image_id "#imgPhoto" end |
#ingredients ⇒ Object
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 |
#name ⇒ Object
25 26 27 |
# File 'lib/allrecipes/recipe_parser.rb', line 25 def name @page.search(name_id).inner_text end |
#name_id ⇒ Object
29 30 31 |
# File 'lib/allrecipes/recipe_parser.rb', line 29 def name_id "#itemTitle" end |
#populate_recipe ⇒ Object
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_time ⇒ Object
77 78 79 |
# File 'lib/allrecipes/recipe_parser.rb', line 77 def prep_time time("prep") end |
#rating ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/allrecipes/recipe_parser.rb', line 49 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
57 58 59 |
# File 'lib/allrecipes/recipe_parser.rb', line 57 def ".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_keys ⇒ Object
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 |
#servings ⇒ Object
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_id ⇒ Object
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 |