Forum: Ruby SOAP: detecting nil element in the response

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
3066e9b6c3db5193ec47f00d324363c1?d=identicon&s=25 Mark Slater (Guest)
on 2007-01-03 09:43
(Received via mailing list)
I'm still getting used to Ruby, so there could be a very easy and
obvious answer to this one. Essentially, the server sends back nil
elements, which Ruby translates into SOAP::Mapping::Object instances.
But how do I determine when those SOAP::Mapping::Object instances
represent nil elements?

Here are the details. I'm calling a soap method to get profile
information about a user in the system I'm writing. The soap that
comes back looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap=""
xmlns:xsd="" xmlns:xsi="http://">
		<getUserInformationResponse xmlns="">
			<out xmlns="">
				<aboutText xmlns="">This is stuff
about me.</aboutText>
				<familyName xmlns="">Last</familyName>...
				<firstName xmlns="">First</firstName>...
				<gender xmlns="">0</gender>
				<namePrefix xmlns="">Dr.</namePrefix>
				<nameSuffix xmlns="">Esq</nameSuffix>
				<otherNames xmlns=""/>
				<previousNames xmlns="">
					<ns1:string xmlns:ns1="http://">previous_name_1</ns1:string>
					<ns1:string xmlns:ns1="http://">previous_name_2</ns1:string>

The WSDL (auto generated by XFire) for this particular type is:

<xsd:complexType name="UserInfoData">
		<xsd:element name="aboutText" type="xsd:string" minOccurs="0"
		<xsd:element name="familyName" type="xsd:string" minOccurs="0"
		<xsd:element name="firstName" type="xsd:string" minOccurs="0"
		<xsd:element name="gender" type="xsd:string" minOccurs="0"
		<xsd:element name="namePrefix" type="xsd:string" minOccurs="0"
		<xsd:element name="nameSuffix" type="xsd:string" minOccurs="0"
		<xsd:element name="otherNames" type="xsd:string" minOccurs="0"
		<xsd:element name="previousNames" type="tns:ArrayOfString"
minOccurs="0" nillable="true"/>

You'll notice that the otherNames element in the response is nil and
that its typed as a string in the WSDL. Users are not required to
enter any of this data, so having nil elements in the response is
valid semantically. In trying to figure out how Ruby SOAP clients
work, I'm doing the following when I get the results back:

   results = driver.getUserInformation( :in0 => user )
   puts '----- result data -----'
   puts "name: #{results.out.namePrefix} #{results.out.firstName} #
{results.out.otherNames} #{results.out.familyName}, #
   puts "gender: #{results.out.gender}"
   puts "about text: #{results.out.aboutText}"

   puts "first name type = #{results.out.firstName.class}"
   puts "other names type = #{results.out.otherNames.class}"
   puts "previous names type = #{results.out.previousNames.class}"

The output of that code gives me:

----- results -----
#<SOAP::Mapping::Object:0xa87a5a {}
out=#<SOAP::Mapping::Object:0xa87924 {}
aboutText="This is stuff about me." {}
familyName="Last" {}
firstName="First" {}gender="0" {http://}namePrefix="Dr." {}
nameSuffix="Esq" {}
otherNames=#<SOAP::Mapping::Object:0xa86a60> {http://}previousNames=#<SOAP::Mapping::Object:0xa85f84
----- result data -----
name: Dr. First #<SOAP::Mapping::Object:0xa86a60> Last, Esq
gender: "0"
about text: "This is stuff about me."
first name type = String
other names type = SOAP::Mapping::Object
previous names type = SOAP::Mapping::Object

Obviously if the element is assigned to an object, I can't just test
to see if its nil; I've also tried comparing it to an empty string.
Is there any way to do detect a nil element in the SOAP response?


This topic is locked and can not be replied to.