Class: WebCrawler::Parsers::Mapper
- Inherits:
-
Object
- Object
- WebCrawler::Parsers::Mapper
- Defined in:
- lib/web_crawler/parsers/mapper.rb
Defined Under Namespace
Instance Attribute Summary collapse
-
#element ⇒ Object
readonly
Returns the value of attribute element.
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#mapping ⇒ Object
readonly
Returns the value of attribute mapping.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#selector ⇒ Object
readonly
Returns the value of attribute selector.
Instance Method Summary collapse
- #build_map(selector, options = { }) ⇒ Object
- #callback(&block) ⇒ Object
- #collect(response) ⇒ Object
-
#initialize(name, binding, selector) ⇒ Mapper
constructor
A new instance of Mapper.
- #map(selector, options = { }, &block) ⇒ Object
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
#element ⇒ Object (readonly)
Returns the value of attribute element.
59 60 61 |
# File 'lib/web_crawler/parsers/mapper.rb', line 59 def element @element end |
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
59 60 61 |
# File 'lib/web_crawler/parsers/mapper.rb', line 59 def klass @klass end |
#mapping ⇒ Object (readonly)
Returns the value of attribute mapping.
59 60 61 |
# File 'lib/web_crawler/parsers/mapper.rb', line 59 def mapping @mapping end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
59 60 61 |
# File 'lib/web_crawler/parsers/mapper.rb', line 59 def name @name end |
#selector ⇒ Object (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, = { }) Map.new(selector, @binding, ) 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 |