1

I am trying to learn some ruby http request response code from this tutorial -

http://danknox.github.io/2013/01/27/using-rubys-native-nethttp-library/

Code so far -

require "net/http"
require "uri"

uri = URI.parse("http://api.random.com")
http = Net::HTTP.new(uri.host, uri.port)

# Continuing our example from above

request = Net::HTTP::Get.new("/search?question=somequestion")
response = http.request(request)

# response.code
# response.body

case response
when HTTPSuccess
  response.body
when HTTPRedirect
  follow_redirect(response) # you would need to implement this method
else
  raise StandardError, "Something went wrong :("
end

error -

Test.rb:16: uninitialized constant HTTPSuccess (NameError)

I saw the only stack overflow post on this issue. Did not help. Why could this be happening ?

stack1
  • 1,004
  • 2
  • 13
  • 28
  • 3
    Per this documentation (http://ruby-doc.org/stdlib-1.9.3/libdoc/net/http/rdoc/Net/HTTPSuccess.html), maybe you need to prefix it with Net:: (e.g., `when Net::HTTPSuccess`)? – Platinum Azure Dec 04 '14 at 22:41
  • @PlatinumAzure - Correct. I'll have to use namespace. How to do it without namespace ? – stack1 Dec 04 '14 at 23:26

1 Answers1

1

This is happening because HTTPSuccess has not been initialized. Try using Net::HTTPSuccess (and Net::HTTPRedirection) instead.

Also, change your case statement to case response.class. In your when statements you're checking for class equality.

jmera
  • 644
  • 4
  • 15