28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/scrapify/base.rb', line 28
def attribute(name, options={}, &block)
add_attribute(name)
options = options.symbolize_keys
parser = options[:xpath] ? :xpath : :css
selector = options[parser]
matcher = /#{options[:regex]}/ if options[:regex]
to_array = options[:array]
define_singleton_method "#{name}_values" do
self.doc ||= parse_html
self.doc.send(parser, selector).map do |element|
if block
yield element
else
content = element.content
if matcher
match_data = content.scan(matcher).map &:first
options[:array] ? match_data : match_data.first
else
content.strip
end
end
end
end
end
|