Module: Cardflex::Xml::Parser

Defined in:
lib/cardflex/xml/parser.rb

Class Method Summary collapse

Class Method Details

._parse_children(elem) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/cardflex/xml/parser.rb', line 13

def self._parse_children(elem)
  if elem.has_elements?
    children = {}
    elem.each_element do |child|
      children[_snakecase(child.name)] = _parse_children(child)
    end
    children
  else
    elem.text
  end
end

._snakecase(key) ⇒ Object



25
26
27
# File 'lib/cardflex/xml/parser.rb', line 25

def self._snakecase(key)
  key.tr('-', '_').to_sym
end

.xml_to_hash(xml) ⇒ Object



6
7
8
9
10
11
# File 'lib/cardflex/xml/parser.rb', line 6

def self.xml_to_hash(xml)
  return {} if xml.nil?

  document = REXML::Document.new(xml)
  { _snakecase(document.root.name) => _parse_children(document.root) }
end