Class: PageParser

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

Instance Method Summary collapse

Constructor Details

#initialize(page, options = {}) ⇒ PageParser

Returns a new instance of PageParser.



3
4
5
6
7
8
9
# File 'lib/allrecipes/page_parser.rb', line 3

def initialize(page, options={})
  @page = page
  @recipes = []
  @options = options
  @limit = options[:limit]
  get_recipes
end

Instance Method Details

#filtered_recipesObject



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

def filtered_recipes
 @limit ? recipe_info.take(@limit) : recipe_info
end

#get_recipesObject



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

def get_recipes
  filtered_recipes.each do |info|
    recipe_link = recipe_link(info)
    @recipes <<  RecipeParser.new(recipe_link, @options[:keys]).recipe
  end
end

#recipe_infoObject



19
20
21
22
23
24
25
# File 'lib/allrecipes/page_parser.rb', line 19

def recipe_info
  if @options[:type] == "ingredient"
    @page.search(recipe_info_class)
  else
    recipes_grid.search(recipe_info_class)
  end
end

#recipe_info_classObject



11
12
13
# File 'lib/allrecipes/page_parser.rb', line 11

def recipe_info_class
   ".recipe-info"
end


39
40
41
# File 'lib/allrecipes/page_parser.rb', line 39

def recipe_link(info)
  "http://allrecipes.com" + info.search(title_class)[0].attributes["href"].value
end

#recipesObject



50
51
52
# File 'lib/allrecipes/page_parser.rb', line 50

def recipes
  @recipes
end

#recipes_gridObject



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

def recipes_grid
  @page.search(recipes_grid_class)[-1]
end

#recipes_grid_classObject



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

def recipes_grid_class
  ".grid-view"
end

#title_classObject



15
16
17
# File 'lib/allrecipes/page_parser.rb', line 15

def title_class
  ".title"
end