Class: OEmbed::Formatters::LibXML

Inherits:
Object
  • Object
show all
Defined in:
lib/oembed_links/formatters/lib_xml.rb

Instance Method Summary collapse

Instance Method Details

#format(txt) ⇒ Object

This is an extremely naive XML doc to hash formatter. Cases like arrays represented in XML will not work; only strings, ints and floats will be converted.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/oembed_links/formatters/lib_xml.rb', line 15

def format(txt)
  parser = ::LibXML::XML::Parser.string(txt)
  doc = parser.parse
  h = { }
  doc.root.children.each do |node|
    unless node.name.strip.empty?
      c = node.content
      if c =~ /^[0-9]+$/
        c = c.to_i
      elsif c=~ /^[0-9]+\.[0-9]+$/
        c = c.to_f
      end
      h[node.name.strip] = c
    end
  end
  return h
end

#nameObject



7
8
9
# File 'lib/oembed_links/formatters/lib_xml.rb', line 7

def name
  "xml"
end