Created
January 20, 2021 01:37
-
-
Save caius/055a8d866b9ac4a9e317d065a45226a3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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