Last active
May 27, 2022 00:54
-
-
Save jonathandean/4ba109fc3a1af1be420c80f65a98e4a0 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
module UrlHelpers | |
def domain(url) | |
unless url =~ /^http(s)?:\/\// | |
url = "https://#{url}" | |
end | |
uri = URI.parse(url) | |
uri.host&.gsub(/^(www|m)\./, '') | |
rescue => e | |
puts "WARNING: failed to retrieve domain from URL #{url}: #{e.message}" | |
nil | |
end | |
def valid_url?(url) | |
begin | |
URI.parse(url) | |
rescue | |
return false | |
end | |
true | |
end | |
def url_without_query_params(url) | |
url.split('?').first | |
end | |
def basename_from_url(url) | |
url.split('/')[-1].split('?')[0] | |
end | |
def extract_url_from_string(text) | |
match_data = /(http(s)?:\/\/(\S)+)/.match(text) | |
if match_data | |
match_data[0] | |
else | |
nil | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment