Heads up: This description was created by AI and might not be 100% accurate.

response_code_2xx.rb

This Ruby code snippet demonstrates a method called rc2xx? that checks whether the HTTP status code of a given URL is within the range of 200-299, which indicates success. The method takes a string parameter representing the URL and returns a boolean value indicating whether the request was successful or not.

The method first parses the input URL using the URI class to extract the hostname, port number, and scheme (HTTP or HTTPS). It then creates an instance of the Net::HTTP class and sets the use_ssl property to true if the scheme is “https”.

The method then constructs a Get request object using the Net::HTTP::Get.new method and sends it to the server using the request method. The response from the server is then captured in the response variable.

Finally, the status code of the response is extracted using the code method and converted to an integer value using the to_i method. The method returns true if the status code is within the range of 200-299, and false otherwise.

Ruby code snippet

require "net/http"
#=> true

def rc2xx?(uri)
    url = URI.parse(uri)
    http = Net::HTTP.new(url.host, url.port)
    http.use_ssl = (url.scheme == "https")
    request = Net::HTTP::Get.new(url.path)
    
    response = http.request(request)
    response_code = response.code.to_i
    response_code >= 200 && response_code < 300
end
#=> :rc2xx?

rc2xx?("https://yumayx.github.io/")
#=> true

Executed with Ruby 3.4.4.