How do you add nested attributes in Savon 2.3?

拟墨画扇 提交于 2020-01-15 03:26:08

问题


Savon 2.1 used :attributes! to add attributes in nested xml tags. How is it done in 2.3? The exact same hash does not render the same xml.

{
  :person => {
    :address => "",
    :attributes! => { :address => { :id => 44 } }
  },
  :attributes! => { :person => { :id => 666 } }
}

creates

 <person>
    <address id="44"/>
  </person>
  <attributes>
    <person>
      <id>666</id>
    </person>
  </attributes>

instead of

<person id=\"666\"><address id=\"44\"></address></person>

For reference: How do I use savon nested attributes! hash?

Another example where even the inner attributes! fails

{
  'Objects' => [{
    'EmailAddress' => 'CreatedUser@test.com',
    :attributes! => {
      'EmailAddress' => { 'xsi:type' => "tns:email" }
    }
  }],
  :attributes! => {
    'Objects' => { 'xsi:type' => "tns:Subscriber" },
  }
}

Produces:

  <Objects>
    <EmailAddress>CreatedUser@test.com</EmailAddress>
    <attributes>
      <EmailAddress>
        <xsi:type>tns:email</xsi:type>
      </EmailAddress>
    </attributes>
  </Objects>
  <attributes>
    <Objects>
      <xsi:type>tns:Subscriber</xsi:type>
    </Objects>
  </attributes>

Later example and ultimate reasoning for issue is attempting to create a subscriber for ExactTarget.

https://webservice.exacttarget.com/etframework.wsdl

 61 def soap
 62   @soap_client ||= Savon.client(
 63     soap_header:  header,
 64     wsdl:           'https://webservice.exacttarget.com/etframework.wsdl',
 65     endpoint:     endpoint,
 66     wsse_auth:    ["*", "*"],
 67     raise_errors: false,
 68     log:          true,
 69     open_timeout: 180,
 70     read_timeout: 180,
 71     pretty_print_xml: true
 72   )
 73 end

 112 def create_subscriber
 113   soap.call :create, :message => {
 114     'Objects' => [{
 115       'EmailAddress' => 'CreatedUser@test.com'
 116     }],
 117     :attributes! => {
 118       'Objects' => { 'xsi:type' => "tns:Subscriber" },
 119     }
 120   }
 121 end

Header omitted since it contains credentials.


回答1:


I ended up getting like so:

   soap.call :create, :message => {
     'Objects' => [{
       'EmailAddress' => 'CreatedUser@test.com',
       :'@xsi:type'   => "tns:Subscriber"
     }]
   }

Attributes are flagged in the same level by using @



来源:https://stackoverflow.com/questions/21212367/how-do-you-add-nested-attributes-in-savon-2-3

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!