Class: Allrecipes

Inherits:
Object
  • Object
show all
Includes:
URLHelper
Defined in:
lib/allrecipes/main.rb,
lib/allrecipes/version.rb

Constant Summary collapse

VERSION =
"1.2.0"

Constants included from URLHelper

URLHelper::COURSES, URLHelper::REGIONS

Instance Method Summary collapse

Methods included from URLHelper

#course_base_url, #ingredient_sort_parameter, #recipe_filters, #recipes_url, #region_base_url, #search_sort_parameter

Constructor Details

#initializeAllrecipes

Returns a new instance of Allrecipes.



5
6
7
# File 'lib/allrecipes/main.rb', line 5

def initialize
  @agent = Mechanize.new
end

Instance Method Details

#all(options = {}) ⇒ Object



9
10
11
12
# File 'lib/allrecipes/main.rb', line 9

def all(options={})
  page = @agent.get $URL + "/recipes" + recipe_filters(options)
  PageParser.new(page, options).recipes
end

#course(course_type, options = {}) ⇒ Object



14
15
16
# File 'lib/allrecipes/main.rb', line 14

def course(course_type, options={})
  recipe_search(course_type, options.merge({ url_type: "course" }))
end

#ingredient(name, options = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/allrecipes/main.rb', line 32

def ingredient(name, options={})
  begin
    url = $URL + "/search/?wt=#{name}&page=#{options[:page]}&sb=#{ingredient_sort_parameter(options[:sort_by])}"
    page = @agent.get(url)
    options = options.merge({type: "ingredient"})
    PageParser.new(page, options).recipes
  rescue Exception
    raise "Could not find recipes that include this ingredient"
  end
end

#page_url(url, keys = nil) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/allrecipes/main.rb', line 51

def page_url(url, keys=nil)
  begin
    page = @agent.get(url)
    PageParser.new(page, { keys: keys }).recipes
  rescue Exception
    raise "This page does not contain recipes"
  end
end

#recipe_search(type, options) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/allrecipes/main.rb', line 22

def recipe_search(type, options)
  begin
    url = recipes_url(type, options)
    page = @agent.get(url)
    PageParser.new(page, options).recipes
  rescue Exception
    raise "#{options[:url_type].capitalize} doesn't exist"
  end
end

#recipe_url(url, keys = nil) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/allrecipes/main.rb', line 43

def recipe_url(url, keys=nil)
  begin
    RecipeParser.new(url, keys).recipe
  rescue Exception
    raise "This page does not contain a recipe"
  end
end

#region(region_type, options = {}) ⇒ Object



18
19
20
# File 'lib/allrecipes/main.rb', line 18

def region(region_type, options={})
  recipe_search(region_type, options.merge({ url_type: "region" }))
end