Module: Fletcher::Nokogiri::XML::NodeSet

Defined in:
lib/fletcher/nokogiri.rb

Instance Method Summary collapse

Instance Method Details

#attribute_arrayObject

convert nodeset models to an array of hashes

@doc.xpath("//img")).attribute_array # => [{:element => "img", :src => ".../someimage.png"}]


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/fletcher/nokogiri.rb', line 29

def attribute_array
  a = Array.new
  each do |node|
    temp_hash = Hash.new 
    case node 
    when ::Nokogiri::XML::Element
      temp_hash[:element] = node.name
      node.attributes.each do |key, value|
        case value
        when ::Nokogiri::XML::Attr
          temp_hash[key.to_sym] = value.value.sanitize
        end 
      end
    end 
    a << temp_hash            
  end
  return a          
end

#first_stringObject

get string from first nodeset model



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/fletcher/nokogiri.rb', line 15

def first_string
  node = first
  case node
  # xml/html element?
  when ::Nokogiri::XML::Element 
    return node.content.sanitize
  # xml/html attribute?
  when ::Nokogiri::XML::Attr
    return node.value.sanitize
  end
end