Module: Raspar::Parser::InstanceMethods

Included in:
DynamicParser
Defined in:
lib/raspar/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



83
84
85
# File 'lib/raspar/parser.rb', line 83

def attributes
  @attributes
end

Instance Method Details

#attr_parser(doc, attr_map) ⇒ Object

Parse doc: html node accroding to attr selector If selector is :self then input doc is a selected doc Select first



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/raspar/parser.rb', line 88

def attr_parser(doc, attr_map)
  attrs = {}
 
  attr_map.each do |attr_name, opts|
    ele = doc

    if opts[:select]
      if opts[:as] == :array
        attrs[attr_name] = doc.search(opts[:select]).collect{|e| process_ele(e, opts)}
      else
        opts[:select].each do |s|
          ele = doc.search(s).first
          break if ele
        end
        attrs[attr_name] = process_ele(ele, opts) if ele
      end
    else
      attrs[attr_name] = process_ele(ele, opts) if ele
    end

    #attrs[opts[:as]] ||= attrs[attr_name] if opts[:as]
  end

  attrs
end

#process(html, klass = nil) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/raspar/parser.rb', line 114

def process(html, klass = nil)
  @results = []
  doc = Nokogiri::HTML(html)
  klass = self.class unless klass

  common_attrs = attr_parser(doc, klass.common_attrs)

  klass.collections.each do |name, collection|
    doc.search(collection[:select]).each do |ele|
       attrs = attr_parser(ele, collection[:attrs]).merge!(common_attrs)
       @results << Result.new(name, attrs, klass.domain)
    end
  end

  @results << Result.new(:default, common_attrs, klass.domain) if @results.none?
  @results
end