Sha256: 1bec8f1ff7ba4f50d07e3c976c2d87f053aba54ccc8db46a261a0b8b552b009d

Contents?: true

Size: 1.34 KB

Versions: 6

Compression:

Stored size: 1.34 KB

Contents

require 'nokogiri'

module RForce
  class SoapResponseNokogiri
    def initialize(content)
      @content = content
    end

    def parse
      doc = Nokogiri::XML(@content)
      body = doc.at_xpath("//soapenv:Body")
      to_hash(body)
    end

  private

    def to_hash(node)
      return parse_text(text) if node.text?

      children = node.children.reject {|c| c.text? && c.text.strip.empty? }

      return nil if children.empty?

      if (child = children.first).text?
        return parse_text(child.text)
      end

      elements = MethodHash.new

      children.each do |elem|
        name = elem.name.split(":").last.to_sym

        if !elements[name]
          # anything inside 'records' should be an array
          elements[name] = elem.name == 'records' ? [to_hash(elem)] : to_hash(elem)
        elsif Array === elements[name]
          elements[name] << to_hash(elem)
        else
          next if elem.name == "Id" # Id fields are duplicated
          elements[name] = [elements[name]] << to_hash(elem)
        end
      end

      return elements.empty? ? nil : elements
    end

    def parse_text(text)
      text.strip!

      return nil if text.empty?

      boolean?(text) ? boolean(text) : text
    end

    def boolean(string)
      string == "true"
    end

    def boolean?(string)
      %w{true false}.include?(string)
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rforce-0.15 lib/rforce/soap_response_nokogiri.rb
rforce-0.14 lib/rforce/soap_response_nokogiri.rb
rforce-0.13 lib/rforce/soap_response_nokogiri.rb
rforce-0.12 lib/rforce/soap_response_nokogiri.rb
rforce-0.11 lib/rforce/soap_response_nokogiri.rb
rforce-0.10 lib/rforce/soap_response_nokogiri.rb