How do you generate a request with ArrayOf constructs ?

105 views
Skip to first unread message

T Mark

unread,
Oct 16, 2013, 9:31:12 AM10/16/13
to sav...@googlegroups.com

I had previously posted this on GitHub but as per the kind suggestion of another user, moved it here.

I need to form a request with (among others) elements defined as such:

<xsd:element maxOccurs="1" minOccurs="1" name="procedures" nillable="true" type="tns:ArrayOfString"/>

I believe my output should look like:

<tns:procedures>
    <tns:string>7498</tns:string>
    <tns:string>9492</tns:string>
</tns:procedures>

and

<tns:procedures>
</tns:procedures>

when the list of procedures is empty.

What's the proper way to do this ? I've tried by passing the procedures key an empty list, but this seems to produce the following in the request instead:

<tns:procedures>7498</tns:procedures>
<tns:procedures>9492</tns:procedures>

Passing nil as the value also does not work.

Any help would be greatly appreciated. I couldn't learn much online about ArrayOf constructs, nor could I seem to find relevant examples.

Thanks so much - with Savon I've already gone way beyond what I was capable of doing before with SOAP - though clearly I have a lot to learn.


T Mark

unread,
Oct 16, 2013, 10:22:53 AM10/16/13
to sav...@googlegroups.com
I realized my original post got bungled.

I *have* figured out how to pass an empty list: adding a nil element to the array (instead of the array being empty) will generate the XML with a (empty) tag.  If the array is empty, Savon seems to omit the tag completely.  

My problem is figuring out how to nest ArrayOf elements as I understand they should be.

In my original example, I need to generate, e.g.
<tns:procedures>
    <tns:string>7498</tns:string>
    <tns:string>9492</tns:string>
</tns:procedures>
BUT, so far I have only been able to generate:

<tns:procedures>7498</tns:procedures>
<tns:procedures>9492</tns:procedures>

The code I am using to do this looks something like this:
proc_array = [7498, 9492]
client.call(:get_acts, message:  { 

                        "theProcedureCodes" => proc_array})

T Mark

unread,
Oct 16, 2013, 4:07:50 PM10/16/13
to sav...@googlegroups.com
I tried to read the code to see if I could patch myself, but there were a few dependencies to understand and my boss won't allow the time.  

I'm not really sure if anyone is reading this as all the views seem to be my own, but I did find a (very ugly) solution, I think:

out = client.call(:get_acts, message:  { "procedures" => proc_array.empty?? Array[nil]:Hash["string" => proc_array]})

This generates the surrounding <procedures> tag if the array is empty.  If the array is not empty, then each element gets added as a child with the tag "string".

thanks again for Savon.
Reply all
Reply to author
Forward
0 new messages