title: httparty response interpretation in ruby
body: <p>I have a service that returns (in an XML format) a list of networks. That list could be comprised of 1 network or 2+ networks. </p>
  
  <p>This is for example what is returned from a REST client where there is 1 network </p>
  
  <pre><code>&lt;Vdc status="1" name="VDC1" id="urn:vcloud:vdc:c66c192a-b58e-468c-bca8-4bfd5657f2ff" href= "https://uk-slough-1-6.vchs.vmware.com/api/compute/api/vdc/c66c192a-b58e-468c-bca8-4bfd5657f2ff" type="application/vnd.vmware.vcloud.vdc+xml" xsi:schemaLocation="http://www.vmware.com/vcloud/v1.5 http://uk-slough-1-6.vchs.vmware.com/api/compute/api/v1.5/schema/master.xsd"&gt;
  &lt;AvailableNetworks&gt;
  &lt;Network href= "https://uk-slough-1-6.vchs.vmware.com/api/compute/api/network/4c5703e5-4365-4408-8f8e-12ec3bfc716f" name="default-routed-network" type="application/vnd.vmware.vcloud.network+xml"/&gt;
  &lt;/AvailableNetworks&gt;
  &lt;/Vdc&gt;
  </code></pre>
  
  <p>And this is what is returned when there are 2 networks: </p>
  
  <pre><code>&lt;Vdc status="1" name="massimo" id="urn:vcloud:vdc:ec16bc23-659d-48b8-af7d-9dda6584122e" href= "https://uk-slough-1-6.vchs.vmware.com/api/compute/api/vdc/ec16bc23-659d-48b8-af7d-9dda6584122e" type="application/vnd.vmware.vcloud.vdc+xml" xsi:schemaLocation="http://www.vmware.com/vcloud/v1.5 http://uk-slough-1-6.vchs.vmware.com/api/compute/api/v1.5/schema/master.xsd"&gt;
  &lt;AvailableNetworks&gt;
  &lt;Network href= "https://uk-slough-1-6.vchs.vmware.com/api/compute/api/network/762cc1c7-d9cc-4eb8-9dfd-17bbc04ca607" name="default-routed-network" type="application/vnd.vmware.vcloud.network+xml"/&gt;
  &lt;Network href= "https://uk-slough-1-6.vchs.vmware.com/api/compute/api/network/8cf5b475-901b-4bed-9506-b394ed2505c2" name="massimo-network" type="application/vnd.vmware.vcloud.network+xml"/&gt;
  &lt;/AvailableNetworks&gt;
  &lt;/Vdc&gt;
  </code></pre>
  
  <p>If I use httparty to query the service this is what I get in the response when there is 1 network: </p>
  
  <pre><code>"AvailableNetworks" =&gt; {
          "Network" =&gt; {
              "href" =&gt; "https://uk-slough-1-6.vchs.vmware.com/api/compute/api/network/4c5703e5-4365-4408-8f8e-12ec3bfc716f",
              "name" =&gt; "default-routed-network",
              "type" =&gt; "application/vnd.vmware.vcloud.network+xml"
          }
      },
  </code></pre>
  
  <p>and this is what I get when there are 2 networks: </p>
  
  <pre><code> "AvailableNetworks" =&gt; {
          "Network" =&gt; [
              [0] {
                  "href" =&gt; "https://uk-slough-1-6.vchs.vmware.com/api/compute/api/network/762cc1c7-d9cc-4eb8-9dfd-17bbc04ca607",
                  "name" =&gt; "default-routed-network",
                  "type" =&gt; "application/vnd.vmware.vcloud.network+xml"
              },
              [1] {
                  "href" =&gt; "https://uk-slough-1-6.vchs.vmware.com/api/compute/api/network/8cf5b475-901b-4bed-9506-b394ed2505c2",
                  "name" =&gt; "massimo-network",
                  "type" =&gt; "application/vnd.vmware.vcloud.network+xml"
              }
          ]
      },
  </code></pre>
  
  <p>This makes managing the response very difficult. At the moment what I do is as follow: I run a check to see if <code>response["Vdc"]["Networks"]</code> is an array and if it is I iterate for its length and treat all its elements as independent hash items. If it is not an array I deal with <code>response["Vdc"]["Networks"]</code> as if it was a hash. </p>
  
  <p>Is there a better way to deal with this and to avoid to check? Ideally I'd like to see <code>response["Vdc"]["Networks"]</code> as always being an array (if there is 1 network I'd like to see an array with 1 element). </p>
  
  <p>Thanks! </p>