views:

458

answers:

1

I've been working with soap4r and trying to use the SOAP::Header::SimpleHandler, I'm trying to get it to put a custom header on the outgoing message, but I can't work out how to get it to include attributes rather than as subelements:

    class ServiceContext < SOAP::Header::SimpleHandler
  NAMESPACE = "http://context.core.datamodel.fs.documentum.emc.com/"
  def initialize()
    super(XSD::QName.new(NAMESPACE, 'ServiceContext'))
    XSD::QName.new(nil, "Identities")
  end

  def on_simple_outbound
    username = "username"
    password = "password"
    docbase = "Test"
    return {"Identities" => {"Username" => username, "Password" => password, "Docbase" => docbase}}
  end
end

which returns:

    <n1:ServiceContext xmlns:n1="http://context.core.datamodel.fs.documentum.emc.com/"
        env:mustUnderstand="0">
      <n1:Identities>
        <n1:Username>username</n1:Username>
        <n1:Password>password</n1:Password>
        <n1:Docbase>Test</n1:Docbase>
      </n1:Identities>
    </n1:ServiceContext>

what I need it to return is the following:

    <ServiceContext xmlns="http://context.core.datamodel.fs.documentum.emc.com/"&gt;
        <Identities xsi:type="RepositoryIdentity" userName="_USER_" password="_PWD_" repositoryName="_DOCBASE_" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/&gt;
    </ServiceContext>

Any help is greatly appreciated.

+1  A: 

soap4r is not very pretty. I poked around the rdocs abit and it looks like the simplest way to fix your problem would be to have on_simple_outbound return the string representation of the element you want to create.

so instead of

return {"Identities" => {"Username" => username, "Password" => password, "Docbase" => docbase}}

try

%Q(<Identities xsi:type="RepositoryIdentity" userName="#{user}" password="#{password}" repositoryName="#{docbase}" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/&gt;)

using something like builder, you could make it look more rubyish, but try that.

The other option would be to investigate newer soap libraries. handsoap looks interesting.

BaroqueBobcat