Class: Kabutops::Recipe

Inherits:
Object
  • Object
show all
Defined in:
lib/kabutops/recipe.rb

Instance Method Summary collapse

Constructor Details

#initializeRecipe

Returns a new instance of Recipe.



3
4
5
6
# File 'lib/kabutops/recipe.rb', line 3

def initialize
  @items = {}
  @nested = false
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/kabutops/recipe.rb', line 8

def method_missing name, *args, &block
  if block_given?
    recipe = Recipe.new
    recipe.instance_eval &block
    @items[name] = RecipeItem.new(name, :recipe, recipe)
    @nested = true
  else
    type, value = args[0..1]
    @items[name] = RecipeItem.new(name, type, value)
  end
end

Instance Method Details

#nested?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/kabutops/recipe.rb', line 45

def nested?
  @nested
end

#process(resource, page) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/kabutops/recipe.rb', line 20

def process resource, page
  result = {}

  @items.each do |name, item|
    result[name] = case item.type
    when :var
      resource[item.value]
    when :recipe
      item.value.process(resource, page)
    when :css
      page.css(item.value).text
    when :xpath
      page.xpath(item.value).text
    when :lambda
      item.value.call(page)
    when :proc
      page.instance_eval &item.value
    else
      raise "unknown recipe item type '#{item.type}'"
    end
  end

  result
end