Class: Scrapula::Scraper

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/scrapula/scraper.rb,
lib/scrapula/_old_scraper.rb

Instance Method Summary collapse

Constructor Details

#initialize(page, &block) ⇒ Scraper

Returns a new instance of Scraper.



11
12
13
14
15
# File 'lib/scrapula/scraper.rb', line 11

def initialize page, &block
 @page, @data = page, {}

  (block.arity == 0 ? instance_eval(&block) : block.call(@page)) if block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/scrapula/scraper.rb', line 32

def method_missing name, *args, &block

  if args.empty?

    # Used as nested block (TODO it's not compatible with using a query)
    if block_given?
      @data[name] = self.class.new @page, &block

    # Invoked as an attribute
    elsif @data.has_key? name
      return @data[name]

    else
      raise 'Unknown case!'
    end

  else

    @data[name] = @page.txt! *args
  end

end

Instance Method Details

#data!Object

TODO remove this method



18
19
20
21
22
23
24
# File 'lib/scrapula/scraper.rb', line 18

def data!
  result = {}
  @data.each_pair do |key, value|
    result[key] = value.is_a?(self.class) ? value.data! : value
  end
  result
end

#execute {|@page| ... } ⇒ Object

delegate @page

Yields:

  • (@page)


14
15
16
17
18
# File 'lib/scrapula/_old_scraper.rb', line 14

def execute &block
  yield @page if block_given?

  self
end

#respond_to?(name) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/scrapula/scraper.rb', line 26

def respond_to? name
  true
end