Skip to content

Instantly share code, notes, and snippets.

@Valiev
Created December 17, 2014 16:19
Show Gist options
  • Save Valiev/2fc854e225f8a62bc8c8 to your computer and use it in GitHub Desktop.
Save Valiev/2fc854e225f8a62bc8c8 to your computer and use it in GitHub Desktop.
Ruby String URL checker
require "net/https"
def http_response_head(url)
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
if uri.scheme == "https"
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
request = Net::HTTP::Head.new(uri.request_uri)
http.request(request)
end
def http_response_code(url)
head = http_response_head url
head.code
end
class String
def to_url
code = http_response_code self
raise "No object at #{self} url" if code.to_i != 200
self
end
end
# Okay:
# "http://example.com".to_url
# Not okay
# "http://example.com1".to_url
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment