Sha256: ff7626560ff73d54193eecf84d4f478f1520aa4a90fee367c27c57c87327f4e7

Contents?: true

Size: 1.76 KB

Versions: 1

Compression:

Stored size: 1.76 KB

Contents

require 'hpricot'


module RForce
  class SoapResponseHpricot
    # Parses an XML string into structured data.
    def initialize(content)
      @content = content
    end

    # Digests an XML DOM node into nested Ruby types.
    def parse
      document = Hpricot.XML(@content)
      node = document % 'soapenv:Body'
      self.class.node_to_ruby node
    end
    
    private
    
    def self.node_to_ruby(node)
      # Convert text nodes into simple strings.
      children = (node.children || []).reject do |c|
        c.is_a?(Hpricot::Text) && c.to_s.strip.empty?
      end

      if node.is_a?(Hpricot::Text)
        return node.inner_text.to_s
      end
      
      if children.first.is_a?(Hpricot::Text)
        return children.first.to_s
      end

      # Convert nodes with children into MethodHashes.
      elements = OpenHash.new({})

      # Add all the element's children to the hash.
      children.each do |e|
        next if e.is_a?(Hpricot::Text) && e.to_s.strip.empty?
        name = e.name
        
        if name.include? ':'
          name = name.split(':').last
        end
        
        name = name.to_sym

        case elements[name]
          # The most common case: unique child element tags.
        when NilClass then elements[name] = node_to_ruby(e)

          # Non-unique child elements become arrays:

          # We've already created the array: just
          # add the element.
        when Array then elements[name] << node_to_ruby(e)

          # We haven't created the array yet: do so,
          # then put the existing element in, followed
          # by the new one.
        else
          elements[name] = [elements[name]]
          elements[name] << node_to_ruby(e)
        end
      end

      return elements.empty? ? nil : elements
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rforce-0.3 lib/rforce/soap_response_hpricot.rb