我的代码解释不好或者我不明白怎么做 让它运作。
基本上我有这个要求:
def create_request
return {} unless !@person.nil?
req = {
ProfileIDVersion: {
ID: ENV['GBGROUP_PROFILE_ID'],
Version: ENV['GBGROUP_PROFILE_VERSION']
},
CustomerReference: reference,
InputData: {
Personal: {
PersonalDetails: {
Title: @person.title,
Forename: @person.first_name,
MiddleName: @person.middle_name,
Surname: @person.last_name,
Gender: @person.gender,
DOBDay: @person.dob.nil? ? '' : @person.dob.day,
DOBMonth: @person.dob.nil? ? '' : @person.dob.month,
DOBYear: @person.dob.nil? ? '' : @person.dob.year
}
},
Addresses: []
}
}
address_count = 0
@person.addresses.each do |address|
Rails.logger.debug 'Adding address'
if address_count == 0
Rails.logger.debug 'Adding current address'
req[:InputData][:Addresses] << {
CurrentAddress: {
Country: address.country,
ZipPostcode: address.postcode,
AddressLine1: address.line1,
AddressLine2: address.line2,
AddressLine3: address.town_city,
FirstYearOfResidence: address.from_year,
LastYearOfResidence: address.to_year
}
}
else
Rails.logger.debug 'Adding previous address'
req[:InputData][:Addresses] << {
"PreviousAddress#{address_count}" => {
Country: address.country,
ZipPostcode: address.postcode,
AddressLine1: address.line1,
AddressLine2: address.line2,
AddressLine3: address.town_city,
FirstYearOfResidence: address.from_year,
LastYearOfResidence: address.to_year
}
}
end
address_count += 1
end
end
我期望输出中的内容:
<ns:Addresses>
<ns:CurrentAddress>
<ns:Country>United Kingdom</ns:Country>
<ns:ZipPostcode>SP9 7EW</ns:ZipPostcode>
<ns:AddressLine1>3 Bayeux Mews</ns:AddressLine1>
<ns:AddressLine2>St Georges Road</ns:AddressLine2>
<ns:AddressLine3>Tidworth</ns:AddressLine3>
<ns:FirstYearOfResidence>2015</ns:FirstYearOfResidence>
<ns:LastYearOfResidence>2015</ns:LastYearOfResidence>
</ns:CurrentAddress>
<ns:PreviousAddress1>
<ns:Country>United Kingdom</ns:Country>
<ns:ZipPostcode>SP9 7EW</ns:ZipPostcode>
<ns:AddressLine1>3 Bayeux Mews</ns:AddressLine1>
<ns:AddressLine2>St Georges Road</ns:AddressLine2>
<ns:AddressLine3>Tidworth</ns:AddressLine3>
<ns:FirstYearOfResidence>2012</ns:FirstYearOfResidence>
<ns:LastYearOfResidence>2015</ns:LastYearOfResidence>
</ns:PreviousAddress1>
</ns:Addresses>
相反,我得到了:
<ns:Addresses>
<ns:CurrentAddress>
<ns:Country>United Kingdom</ns:Country>
<ns:ZipPostcode>SP9 7EW</ns:ZipPostcode>
<ns:AddressLine1>3 Bayeux Mews</ns:AddressLine1>
<ns:AddressLine2>St Georges Road</ns:AddressLine2>
<ns:AddressLine3>Tidworth</ns:AddressLine3>
<ns:FirstYearOfResidence>2015</ns:FirstYearOfResidence>
<ns:LastYearOfResidence>2015</ns:LastYearOfResidence>
</ns:CurrentAddress>
</ns:Addresses>
<ns:Addresses>
<ns:PreviousAddress1>
<ns:Country>United Kingdom</ns:Country>
<ns:ZipPostcode>SP9 7EW</ns:ZipPostcode>
<ns:AddressLine1>3 Bayeux Mews</ns:AddressLine1>
<ns:AddressLine2>St Georges Road</ns:AddressLine2>
<ns:AddressLine3>Tidworth</ns:AddressLine3>
<ns:FirstYearOfResidence>2012</ns:FirstYearOfResidence>
<ns:LastYearOfResidence>2015</ns:LastYearOfResidence>
</ns:PreviousAddress1>
</ns:Addresses>
正如你所看到的,我有两个地址块,一个带有当前地址,另一个带有 以前的地址,但只需要一个地址块。
问题可能是多次调用:
req[:InputData][:Addresses] <<
但我实际上并不知道如何解决它,以便在一个区块中拥有所有地址。
这是我从Spec测试中得到的:
"Addresses": [
{
"CurrentAddress": {
"Country": "United Kingdom",
"ZipPostcode": "OX4 2QX",
"AddressLine1": "36 Brasenose Driftway",
"AddressLine2": "Oxford",
"AddressLine3": null,
"FirstYearOfResidence": 2002,
"LastYearOfResidence": 2014
}
},
{
"PreviousAddress1": {
"Country": "United Kingdom",
"ZipPostcode": "AB1 1AA",
"AddressLine1": "21 A Street",
"AddressLine2": "Aberdeen",
"AddressLine3": null,
"FirstYearOfResidence": 1998,
"LastYearOfResidence": 2002
}
}
]
感谢任何人花一分钟时间回复, 毛虫。
答案 0 :(得分:0)
在ruby hash和xml之间进行转换的savon是一个名为gyoku的gem。对于gyoku,数组值会产生多个元素(其名称由键给出)。如果你不想要那个,那么地址键的值必须是一个散列,你的输入应该是这样的
{"Addresses" => {
"CurrentAddress" => {
"Country" => "United Kingdom",
...
},
"Previous" => {
"Country" => "United Kingdom",
...
}
}