module HS::XMLParser

Public Instance Methods

get_text(object) click to toggle source

Text content of the node. Note, that we don't go deep into element tree.

# File lib/hs/loaders/xml_parser.rb, line 18
def get_text(object)
  object[:_children].map { |obj| obj[:text] }.compact.join(' ')
end
parse_xml(xml) click to toggle source
# File lib/hs/loaders/xml_parser.rb, line 8
def parse_xml(xml)
  doc = Nokogiri::XML(xml) do |config|
    config.options = Nokogiri::XML::ParseOptions::STRICT
  end

  to_object_notation(doc.root)
end

Private Instance Methods

attributes_hash(xml) click to toggle source
# File lib/hs/loaders/xml_parser.rb, line 24
def attributes_hash(xml)
  xml.attributes.map do |key, attr|
    [key.underscore.to_sym, attr.value]
  end.to_h
end
cdata_to_object_notation(xml) click to toggle source
# File lib/hs/loaders/xml_parser.rb, line 43
def cdata_to_object_notation(xml)
  text_to_object_notation(xml)
end
comment_to_object_notation(_xml) click to toggle source
# File lib/hs/loaders/xml_parser.rb, line 58
def comment_to_object_notation(_xml)
  nil
end
element_to_object_notation(xml) click to toggle source
# File lib/hs/loaders/xml_parser.rb, line 35
def element_to_object_notation(xml)
  data = attributes_hash(xml)
  data[:_name] = xml.name.underscore.to_sym
  data[:_children] = to_object_notation(xml.children)

  data
end
nodeset_to_object_notation(xml) click to toggle source
# File lib/hs/loaders/xml_parser.rb, line 54
def nodeset_to_object_notation(xml)
  xml.map { |node| to_object_notation(node) }.compact
end
text_to_object_notation(xml) click to toggle source
# File lib/hs/loaders/xml_parser.rb, line 47
def text_to_object_notation(xml)
  {
    _name: :text,
    text: xml.text
  }
end
to_object_notation(xml) click to toggle source
# File lib/hs/loaders/xml_parser.rb, line 30
def to_object_notation(xml)
  class_name = xml.class.name.split('::').last.downcase
  send("#{class_name}_to_object_notation", xml)
end