0

I am using HandSoap to make client code for JAX-WS SOAP API. I am able to extract XML from SoapResponse using Nokigiri gem but I am unable to convert that response to any particular class. Is there any way in ruby to convert Handsoap::SoapResponse to particular class?

Raheel
  • 4,953
  • 4
  • 34
  • 40
  • 1
    There won't be a way to coerce the data directly. Once you have the data extracted from the XML, use it to initialize a new instance of your custom class. Without sample XML and a sample of the class you want to create I can't be more helpful. – the Tin Man Feb 04 '13 at 15:56

1 Answers1

0

I have done some research and it seems that there is not other way to do it directly, so I am using XML to get a hash and traverse this hash recursively to initialize class instance.

To get XML and hash from SOAP response

  response = client.someSoapCall({:someParameter => "someValue"})
  doc = Nokogiri::XML.parse(response.document.to_raw)
  hash = Hash.from_xml(doc.to_s)

This function will cast hash to class

def recast(className, hash)
  hash.each_pair do |k, v|
    if (v.class==Hash)
      find(className, v)
    else
      membersArray = className.public_methods
      membersArray.each {
          |x|

        if (x.to_s==k.to_s)
          member =x.to_s
          className.send "#{member}=", v.to_s
        end

      }
    end
  end

end
Raheel
  • 4,953
  • 4
  • 34
  • 40