Sha256: ac3803eaea54a09fb16092e221f324809ea0503f799a6a0e858d529403932be6

Contents?: true

Size: 1.91 KB

Versions: 4

Compression:

Stored size: 1.91 KB

Contents

require 'hpricot'
require 'cgi'


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.unescapeXML(string)
      CGI.unescapeHTML(string).gsub("'", "'")
    end

    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 SoapResponseHpricot.unescapeXML(node.inspect[1..-2])
      end

      if children.first.is_a?(Hpricot::Text)
        return SoapResponseHpricot.unescapeXML(children.first.inspect[1..-2])
      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

4 entries across 4 versions & 3 rubygems

Version Path
rforcedotcom-20.0.0 lib/rforce/soap_response_hpricot.rb
rforce-raygao-1.0.0 lib/rforce/soap_response_hpricot.rb
rforce-0.4.1 lib/rforce/soap_response_hpricot.rb
rforce-0.4 lib/rforce/soap_response_hpricot.rb