Response_code_2xx.rb
This content was produced by an LLM and could include errors.
This script defines a Ruby method to test if a given URI returns a successful (2xx) HTTP status code. It utilizes the Net::HTTP library to perform a GET request and check the response code.
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
Ruby 4.0.3