I want to be able to parse any url with ruby to get the main part of the domain without the www
(just the XXXX.com)
相关问题
- Question marks after images and js/css files in ra
- Correctly parse PDF paragraphs with Python
- Using :remote => true with hover event
- Eager-loading association count with Arel (Rails 3
- R: eval(parse()) error message: cannot ope
相关文章
- 如何通过命令行测试dns反向解析
- Ruby using wrong version of openssl
- Right way to deploy Rails + Puma + Postgres app to
- AWS S3 in rails - how to set the s3_signature_vers
- Difference between Thread#run and Thread#wakeup?
- how to call a active record named scope with a str
- How do I get from a type to the TryParse method?
- How to add a JSON column in MySQL with Rails 5 Mig
Just a short note: to overcome the second parsing of the url from Mischas second example, you could make a string comparison instead of URI.parse.
The downside of this approach is, that it is limiting the url to http(s) based urls, which is widely the standard. But if you will use it more general (f.e. for ftp links) you have to adjust accordingly.
Here's one that works better with .co.uk and .com.fr - type domains
if the URL is in format
http://www.google.com
, then you could do something like:Or
Please note there is no algorithmic method of finding the highest level at which a domain may be registered for a particular top-level domain (the policies differ with each registry), the only method is to create a list of all top-level domains and the level at which domains can be registered.
This is the reason why the Public Suffix List exists.
I'm the author of PublicSuffix, a Ruby library that decomposes a domain into the different parts.
Here's an example
Addressable is probably the right answer in 2018, especially uses the PublicSuffix gem to parse domains.
However, I need to do this kind of parsing in multiple places, from various data sources, and found it a bit verbose to use repeatedly. So I created a wrapper around it, Adomain:
I hope this helps others.
This should work with pretty much any URL:
Or:
You may have to
require 'uri'
.