class RecipeParser

Attributes

directions[RW]
recipe[RW]

Public Class Methods

new(page, keys=nil) click to toggle source
# 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

Public Instance Methods

agent() click to toggle source
# File lib/allrecipes/recipe_parser.rb, line 13
def agent
  Mechanize.new
end
cook_time() click to toggle source
# File lib/allrecipes/recipe_parser.rb, line 81
def cook_time
  time("cook")
end
default_keys() click to toggle source
# 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() click to toggle source
# File lib/allrecipes/recipe_parser.rb, line 85
def directions_class
  ".directions ol li span"
end
directions_list() click to toggle source
# File lib/allrecipes/recipe_parser.rb, line 89
def directions_list
  @page.search(directions_class)
end
get_directions() click to toggle source
# File lib/allrecipes/recipe_parser.rb, line 93
def get_directions
  directions_list.each do |direction|
    @directions << direction.text
  end
end
hours(type) click to toggle source
# File lib/allrecipes/recipe_parser.rb, line 73
def hours(type)
  @page.search("##{type}HoursSpan em")
end
image() click to toggle source
# File lib/allrecipes/recipe_parser.rb, line 33
def image
  @page.search(image_id).first.attributes["src"].value
end
image_id() click to toggle source
# File lib/allrecipes/recipe_parser.rb, line 37
def image_id
  "#imgPhoto"
end
ingredients() click to toggle source
# File lib/allrecipes/recipe_parser.rb, line 99
def ingredients
  @ingredients ||= IngredientsParser.new(@page).ingredients
end
minutes(type) click to toggle source
# File lib/allrecipes/recipe_parser.rb, line 69
def minutes(type)
  @page.search("##{type}MinsSpan em")
end
name() click to toggle source
# File lib/allrecipes/recipe_parser.rb, line 25
def name
  @page.search(name_id).inner_text
end
name_id() click to toggle source
# File lib/allrecipes/recipe_parser.rb, line 29
def name_id
  "#itemTitle"
end
populate_recipe() click to toggle source
# 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() click to toggle source
# File lib/allrecipes/recipe_parser.rb, line 77
def prep_time
  time("prep")
end
rating() click to toggle source
# 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_class() click to toggle source
# File lib/allrecipes/recipe_parser.rb, line 57
def rating_class
  ".detail-right meta[itemprop='ratingValue']"
end
request_page(page) click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# File lib/allrecipes/recipe_parser.rb, line 41
def servings
  @page.search(servings_id).inner_text.gsub(" servings", "").to_i
end
servings_id() click to toggle source
# File lib/allrecipes/recipe_parser.rb, line 45
def servings_id
  "#lblYield"
end
time(type) click to toggle source
# 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