lib/praxis/handlers/xml.rb
# Transform a Nokogiri DOM object into structured data.
Transform a Nokogiri DOM object into structured data.
Transform a Nokogiri DOM object into structured data .
def process(node, type_attribute)
        type = type_attribute.value if type_attribute
        case type
        when nil
          if (node.children.size == 1 && node.child.text?) || node.children.size == 0
            return node.content
          else
            return node.children.each_with_object({}) do |child, hash|
              next unless child.element? 
              hash[child.name.underscore] = process(child, child.attributes['type'])
            end
          end
        when "array"
          return node.children.each_with_object([]) do |child, arr|
            next unless child.element? 
            arr << process(child, child.attributes['type'])
          end
        when "integer"
          return Integer(node.content)
        when "symbol"
          return node.content.to_sym
        when "decimal"
          return BigDecimal.new(node.content)
        when "float"
          return Float(node.content)
        when "boolean"
          return ((node.content == "false") ? false : true)
        when "date"
          return Date.parse(node.content)
        when "dateTime"
          return DateTime.parse(node.content)
        else
          raise ArgumentError, "Unknown attribute type: #{type}"
        end
      end
def process ( node , type_attribute ) type = type_attribute . value if type_attribute case type when nil if ( node . children . size == 1 && node . child . text? ) || node . children . size == 0 return node . content else return node . children . each_with_object ( { } ) do | child , hash | next unless child . element? hash [ child . name . underscore ] = process ( child , child . attributes [ 'type' ] ) end end when "array" return node . children . each_with_object ( [ ] ) do | child , arr | next unless child . element? arr << process ( child , child . attributes [ 'type' ] ) end when "integer" return Integer ( node . content ) when "symbol" return node . content . to_sym when "decimal" return BigDecimal . new ( node . content ) when "float" return Float ( node . content ) when "boolean" return ( ( node . content == "false" ) ? false : true ) when "date" return Date . parse ( node . content ) when "dateTime" return DateTime . parse ( node . content ) else raise ArgumentError , "Unknown attribute type: #{type}" end end
Transform a Nokogiri DOM object into structured data.
Transform a Nokogiri DOM object into structured data .
# leaf text node # A hash # There might be text fragments like newlines...spaces # There might be text fragments like newlines...spaces
node
<not_specific>
type_attribute
<not_specific>