Ruby Hash de la respuesta de Sage Pay

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)

preguntado el 28 de agosto de 12 a las 11:08

@matthewnreid I would just like to access each individual param that was returned, instead of having a string of new-line separated values. -

This isn't really a Rails questions, it's nothing specific to Rails at all ;) -

@tarnfeld I have updated the title in response to your comment -

1 Respuestas

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 or haz tu propia pregunta.