Ruby Hash de la respuesta de Sage Pay
Frecuentes
Visto 156 veces
0
I have a response @response
from Sage Pay:
VPSProtocol=2.23 Status=OK StatusDetail=Server transaction registered successfully. VPSTxId={C9B14A59-1EB7-4A56-A4B1-29B84BE4861B} SecurityKey=VGGPR12XC1 NextURL=https://test.sagepay.com/Simulator/VSPServerPaymentPage.asp?TransactionID={C9B14A59-1EB7-4A56-A4B1-XXXXXXXXXXXXX}
How can I split this into a hash of:
@response['VPSProtocol'] = "2.23"
@response['Status'] = "OK"
....
?
(Params in the response are newline separate \r\n
)
1 Respuestas
1
Podrías hacer algo como esto ...
response = "VPSProtocol=2.23\r\nStatus=OK\r\nStatusDetail=Server transaction registered successfully."
response_hash = {}
response.split("\r\n").map do |value|
key, value = value.split("=")
response_hash[key] = value
end
print response_hash
I'm sure there's a handy method in ruby to do this kind of mapping, but thats a quick solution. Would love to hear some better ideas from the community!
Respondido 28 ago 12, 11:08
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas ruby-on-rails hash split or haz tu propia pregunta.
Te refieres a algo como el String scan example on the Rails documentation - matthewnreid
@matthewnreid I would just like to access each individual param that was returned, instead of having a string of new-line separated values. - Harry
This isn't really a Rails questions, it's nothing specific to Rails at all ;) - tarnfeld
@tarnfeld I have updated the title in response to your comment - Harry