In my Rails controller, I have a URL that the user provides. I would like to extract the title from the <title>...</title> tag in the HTML of that URL, and assign it to a variable title. How can I do that?
EDIT: It would be a plus to do it without use of external package/library
I’ve just tried it with HTTParty to fetch the document, and Nokogiri to parse it:
Updated:
This works with Net:HTTP, which is part of the ruby standard library, and a regex:
Updated:
=~called “match” is a method onStringand is used to match a regular expression against a String. It’s basically the same asRegexp#matchbut the receiver and argument are inverted.What happens when the HTML document you fetch does not contain a title, or title tag?