Ruby parse HTTP response
deeptishukla24 (2)

require 'net/http'
uri = URI('https://www.random.org/dice/?num=10')
Net::HTTP.get(uri)

I'm using the above code to call this random dice generator.
I'm able to get the entire http response using the get method above.
But I'm stuck on how to access the result from that big HTML string?
I'm just trying to access the individual dice values (10 in this case).
Thanks!

You are viewing a single comment. View All
Answered by heyitsmarcus (288) [earned 5 cycles]
View Answer
heyitsmarcus (288)

Why are you even trying to get this response? Are you just trying to get a random dice roll?

deeptishukla24 (2)

@heyitsmarcus thanks for responding. I was asked this question in a recent programming interview. The question was to call this uri, get individual dice values and print to stdout. But I was stuck on the parsing part.

If it was a normal JSON response, I'd have used JSON.parse(response.body).
I'm also trying to see if I can use httparty to get this to work.

Thanks!

heyitsmarcus (288)

@deeptishukla24 Did they give you freedom to use any libraries?

deeptishukla24 (2)

@heyitsmarcus yes. I was looking into using nokogiri to parse the HTML. Do you have any other suggestion in mind?