从ruby中的字符串中删除子域 [英] Remove subdomain from string in ruby

查看:134
本文介绍了从ruby中的字符串中删除子域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在循环一系列的URL,并想清理它们。我有以下代码:

 #解析url以删除http,路径和检查格式
o_url = URI.parse (node.attributes ['href'])

#删除www
new_url = o_url.host.gsub('www。','').strip

如何将其扩展为删除某些URL中存在的子域?

解决方案

我刚刚写了一个名为Domainatrix的图书馆。您可以在这里找到: http://github.com/pauldix/domainatrix

  require'rubygems'
require'domainatrix'

url = Domainatrix.parse(http:// www .pauldix.net)
url.public_suffix#=> net
url.domain#=> pauldix
url.canonical#=> net.pauldix

url = Domainatrix.parse(http://foo.bar.pauldix.co.uk/asdf.html?q=arg)
url.public_suffix #=> co.uk
url.domain#=> pauldix
url.subdomain#=> foo.bar
url.path#=> /asdf.html?q=arg
url.canonical#=> uk.co.pauldix.bar.foo/asdf.html?q=arg


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(node.attributes['href'])

# Remove www
new_url = o_url.host.gsub('www.', '').strip

How can I extend this to remove the subdomains that exist in some URLs?

解决方案

I just wrote a library to do this called Domainatrix. You can find it here: http://github.com/pauldix/domainatrix

require 'rubygems'
require 'domainatrix'

url = Domainatrix.parse("http://www.pauldix.net")
url.public_suffix       # => "net"
url.domain    # => "pauldix"
url.canonical # => "net.pauldix"

url = Domainatrix.parse("http://foo.bar.pauldix.co.uk/asdf.html?q=arg")
url.public_suffix       # => "co.uk"
url.domain    # => "pauldix"
url.subdomain # => "foo.bar"
url.path      # => "/asdf.html?q=arg"
url.canonical # => "uk.co.pauldix.bar.foo/asdf.html?q=arg"

这篇关于从ruby中的字符串中删除子域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆