Skip to content

Instantly share code, notes, and snippets.

@caius
Created January 20, 2021 01:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caius/055a8d866b9ac4a9e317d065a45226a3 to your computer and use it in GitHub Desktop.
Save caius/055a8d866b9ac4a9e317d065a45226a3 to your computer and use it in GitHub Desktop.
require "net/http"
A_CHUNK_TOO_FAR = Class.new(StandardError)
# @param url [URI] uri to fetch title of
def url_title(url)
title = nil
Net::HTTP.start(url.host, url.port, use_ssl: (url.scheme == "https"), open_timeout: 5, read_timeout: 5) do |http|
request = Net::HTTP::Get.new(url)
http.request(request) do |response|
bytes_read = 0
response.read_body do |chunk|
raise(A_CHUNK_TOO_FAR) if bytes_read > 16_000
title = chunk[%r{<title>([^<]+)</title>}m, 1]&.strip
raise(A_CHUNK_TOO_FAR) if title
bytes_read += chunk.size
end
end
nil
end
rescue A_CHUNK_TOO_FAR
title
end
url_title(URI("https://caiustheory.com")) # => "Latest - CaiusTheory"
url_title(URI("https://google.com")) # => nil
url_title(URI("http://zomgsexandporn.caius.name/2020-08-18-desk.jpeg")) # => nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment