Remove subdomain from string in ruby

前端 未结 8 1837
再見小時候
再見小時候 2020-12-17 00:32

I\'m looping over a series of URLs and want to clean them up. I have the following code:

# Parse url to remove http, path and check format
o_url = URI.parse(         


        
8条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-17 00:42

    For posterity, here's an update from Oct 2014:

    I was looking for a more up-to-date dependency to rely on and found the public_suffix gem (RubyGems) (GitHub). It's being actively maintained and handles all the top-level domain and nested-subdomain issues by maintaining a list of the known public suffixes.

    In combination with URI.parse for stripping protocol and paths, it works really well:

    ❯❯❯ 2.1.2 ❯ PublicSuffix.parse(URI.parse('https://subdomain.google.co.uk/path/on/path').host).domain
    => "google.co.uk"
    

提交回复
热议问题