2012-05-07 13 views
0

私はSavonを使用しています。複数のSOAP本体XMLタグを動的に生成する正しい方法は何ですか? 私はこの方法を考えています。それは正しい方法ではありません。Ruby - Rails - 構造SOAP XML本体

item_id = "abc,def,xyz" 

item_xml = "" 
item_id.split(",").each do |e| 
item_xml << 'ItemId' => "#{e}" #Sure this is a wrong way 
end 
begin 

myclient = Savon::Client.new do |wsdl, soap| 
wsdl.document = "http://somthing.com/service?wsdl" 
wsdl.soap_actions 
end 
result = myclient.request :v1, :update do |soap| 
soap.namespaces["xmlns:v1"] = "http://somthing.com/service?wsdl" 
end 


#This is how I do for manual single entry of ItemId 
soap.body = { 
'Body' => { 
      'ItemList' => { 
'ItemId' => "abc123" 
      } 
     } 
} 

#Want to generate soap body with multiple ItemId 
soap.body = { 
'Body' => { 
      'ItemList' => { 
item_xml 

#shall be equivalent as this 
#'ItemId' => "abc", 
#'ItemId' => "def", 
#'ItemId' => "xyz" 

      } 
     } 
} 

EDIT:item_id内の要素の数に基づいて、タグの配列を作成する方法について 方法は?サボンを使用してリストを作成する

item_id = "abc, def, xyz" 
n = item_id.split(,).length 

    #shall be equivalent as this 
    #ItemList shall be of n times 
soap.body = { 
    'Body' => { 
       'ItemList' => { 
    'ItemId' => "abc" 
       } 
       'ItemList' => { 
    'ItemId' => "def" 
       } 
       'ItemList' => { 
    'ItemId' => "xyz" 
       } 
      } 
    } 

答えて

0

は、特定のキーの値の配列を渡すのと同じくらい簡単です:

require 'savon' 

item_id = 'abc,def,xyz' 

client = Savon::Client.new do |wsdl, soap| 
    wsdl.endpoint = 'http://example.com' 
    wsdl.namespace = 'http://example.com' 
end 

response = client.request :v1, :update do |soap| 
    soap.body = { 
    'ItemList' => { 
     'ItemId' => item_id.split(',') 
    } 
    } 
end 
+0

どうもありがとう!しかし、この分割から要素数ごとに複数のタグを作成するにはどうすればよいですか?質問の編集をご覧ください。 –