Class: WebCrawler::Parsers::Mapper

Inherits:
Object
  • Object
show all
Defined in:
lib/web_crawler/parsers/mapper.rb

Defined Under Namespace

Classes: Filter, Map

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, binding, selector) ⇒ Mapper

Returns a new instance of Mapper.



61
62
63
64
65
66
# File 'lib/web_crawler/parsers/mapper.rb', line 61

def initialize(name, binding, selector)
  @name     = name
  @binding  = binding
  @selector = selector
  @mapping  = { }
end

Instance Attribute Details

#elementObject (readonly)

Returns the value of attribute element.



59
60
61
# File 'lib/web_crawler/parsers/mapper.rb', line 59

def element
  @element
end

#klassObject (readonly)

Returns the value of attribute klass.



59
60
61
# File 'lib/web_crawler/parsers/mapper.rb', line 59

def klass
  @klass
end

#mappingObject (readonly)

Returns the value of attribute mapping.



59
60
61
# File 'lib/web_crawler/parsers/mapper.rb', line 59

def mapping
  @mapping
end

#nameObject (readonly)

Returns the value of attribute name.



59
60
61
# File 'lib/web_crawler/parsers/mapper.rb', line 59

def name
  @name
end

#selectorObject (readonly)

Returns the value of attribute selector.



59
60
61
# File 'lib/web_crawler/parsers/mapper.rb', line 59

def selector
  @selector
end

Instance Method Details

#build_map(selector, options = { }) ⇒ Object



76
77
78
# File 'lib/web_crawler/parsers/mapper.rb', line 76

def build_map(selector, options = { })
  Map.new(selector, @binding, options)
end

#callback(&block) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/web_crawler/parsers/mapper.rb', line 68

def callback(&block)
  if block_given?
    @callback = block
  else
    @callback
  end
end

#collect(response) ⇒ Object



84
85
86
87
88
89
90
91
92
93
# File 'lib/web_crawler/parsers/mapper.rb', line 84

def collect(response)
  doc = Hpricot(response.to_s, :xml => response.xml?)
  [].tap do |collected|
    doc.search(selector).each do |context|
      collected << { }
      collect_with_mapping(context, collected) unless mapping.empty?
      collect_with_callback(context, collected) if callback
    end
  end
end

#map(selector, options = { }, &block) ⇒ Object



80
81
82
# File 'lib/web_crawler/parsers/mapper.rb', line 80

def map(selector, options = { }, &block)
  @mapping[selector] = Map.new(selector, @binding, options, &block)
end