Class: ExtraLoop::ExtractionLoop

Inherits:
Object
  • Object
show all
Includes:
Hookable
Defined in:
lib/extraloop/extraction_loop.rb

Defined Under Namespace

Modules: Exceptions

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Hookable

#run_hook, #set_hook

Constructor Details

#initialize(loop_extractor, extractors = [], document = nil, hooks = {}, scraper = nil) ⇒ ExtractionLoop

Returns a new instance of ExtractionLoop.



12
13
14
15
16
17
18
19
20
# File 'lib/extraloop/extraction_loop.rb', line 12

def initialize(loop_extractor, extractors=[], document=nil, hooks = {}, scraper = nil)
  @loop_extractor = loop_extractor
  @extractors = extractors
  @document = document.is_a?(String) ? @loop_extractor.parse(document) : document
  @records = []
  @hooks = hooks
  @environment = ExtractionEnvironment.new(scraper, @document, @records)
  self
end

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



10
11
12
# File 'lib/extraloop/extraction_loop.rb', line 10

def children
  @children
end

#documentObject

Returns the value of attribute document.



10
11
12
# File 'lib/extraloop/extraction_loop.rb', line 10

def document
  @document
end

#environmentObject (readonly)

Returns the value of attribute environment.



9
10
11
# File 'lib/extraloop/extraction_loop.rb', line 9

def environment
  @environment
end

#extractorsObject

Returns the value of attribute extractors.



10
11
12
# File 'lib/extraloop/extraction_loop.rb', line 10

def extractors
  @extractors
end

#hooksObject

Returns the value of attribute hooks.



10
11
12
# File 'lib/extraloop/extraction_loop.rb', line 10

def hooks
  @hooks
end

#parentObject

Returns the value of attribute parent.



10
11
12
# File 'lib/extraloop/extraction_loop.rb', line 10

def parent
  @parent
end

#recordsObject (readonly)

Returns the value of attribute records.



9
10
11
# File 'lib/extraloop/extraction_loop.rb', line 9

def records
  @records
end

#scraperObject

Returns the value of attribute scraper.



10
11
12
# File 'lib/extraloop/extraction_loop.rb', line 10

def scraper
  @scraper
end

Instance Method Details

#runObject



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/extraloop/extraction_loop.rb', line 22

def run
  run_hook(:before, @document)

  get_nodelist.each do |node|
    run_hook(:before_extract, [node])
    @records << run_extractors(node)
    run_hook(:after_extract, [node, records.last])
  end

  run_hook(:after, @records)
  self
end