uninitialized constant HTTPSuccess (NameError)

1.2k Views Asked by At

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 ?

1

There are 1 best solutions below

1
On BEST ANSWER

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.