Skip to main content
deleted 19 characters in body; edited tags
Source Link
200_success
  • 145.6k
  • 22
  • 191
  • 481

I got sick of every time I make an Http request validating that the response was a success. I did a lot of:

if response.class == Net::HTTPOK

or

if response.is_a? Net::HTTPSuccess

I wanted something a little prettier so I came up with the following. I don't write a lot of class extensions like this so I wanted some input. Thanks in advance!

require 'net/http'
class Net::HTTPResponse
  def success?
    return false if ! self.is_a? Net::HTTPSuccess
    return true
  end
end

Now once the file is included I can say:

if response.success?

I got sick of every time I make an Http request validating that the response was a success. I did a lot of:

if response.class == Net::HTTPOK

or

if response.is_a? Net::HTTPSuccess

I wanted something a little prettier so I came up with the following. I don't write a lot of class extensions like this so I wanted some input. Thanks in advance!

require 'net/http'
class Net::HTTPResponse
  def success?
    return false if ! self.is_a? Net::HTTPSuccess
    return true
  end
end

Now once the file is included I can say:

if response.success?

I got sick of every time I make an Http request validating that the response was a success. I did a lot of:

if response.class == Net::HTTPOK

or

if response.is_a? Net::HTTPSuccess

I wanted something a little prettier so I came up with the following. I don't write a lot of class extensions like this so I wanted some input.

require 'net/http'
class Net::HTTPResponse
  def success?
    return false if ! self.is_a? Net::HTTPSuccess
    return true
  end
end

Now once the file is included I can say:

if response.success?
Source Link

Creating a Basic Success Function for Http

I got sick of every time I make an Http request validating that the response was a success. I did a lot of:

if response.class == Net::HTTPOK

or

if response.is_a? Net::HTTPSuccess

I wanted something a little prettier so I came up with the following. I don't write a lot of class extensions like this so I wanted some input. Thanks in advance!

require 'net/http'
class Net::HTTPResponse
  def success?
    return false if ! self.is_a? Net::HTTPSuccess
    return true
  end
end

Now once the file is included I can say:

if response.success?