module RForce

Constants

SoapResponse

Use the fastest XML parser available.

VERSION

Public Instance Methods

expand(builder, args, xmlns = nil) click to toggle source

Expand Ruby data structures into XML.

# File lib/rforce.rb, line 54
def expand(builder, args, xmlns = nil)
  # Nest arrays: [:a, 1, :b, 2] => [[:a, 1], [:b, 2]]
  if args.is_a?(Array)
    args = args.each_slice(2).to_a
  end

  args.each do |key, value|
    attributes = xmlns ? {:xmlns => xmlns} : {}

    # If the XML tag requires attributes,
    # the tag name will contain a space
    # followed by a string representation
    # of a hash of attributes.
    #
    # e.g. 'sObject {"xsi:type" => "Opportunity"}'
    # becomes <sObject xsi:type="Opportunity>...</sObject>
    if key.is_a? String
      key, modifier = key.split(' ', 2)

      attributes.merge!(eval(modifier)) if modifier
    end

    # Create an XML element and fill it with this
    # value's sub-items.
    case value
    when Hash, Array then
      builder.tag!(key, attributes) do expand builder, value; end

    when String then
      builder.tag!(key, attributes) { builder.text! value }
    end
  end
end