Simplified domain parsing
Adomain["abc.xyz.com/path&key=value"]
# => "abc.xyz.com"
Adomain["https://abc.xyz.com"]
# => "abc.xyz.com"
Adomain["https://www.xyz.com"]
# => "xyz.com"
Adomain returns the URL host, stripping 'www' by default, scheming your URL if necessary (with "https://"). Optionally, you can strip subdomains or keep 'www' subdomains – see below.
Addressable::URI.parse
performs the parsing.
[]
is an alias for Adomain.subdomain()
Adomain["http://abc.xyz.com"] == Adomain.subdomain("http://abc.xyz.com")
# => true
Adomain only has a few other public methods:
Adomain.domain "http://abc.xyz.com" # => "xyz.com"
Adomain.subdomain_www "http://www.xyz.com" # => "www.xyz.com"
Optionally, each method accepts boolean values, allowing you to contort the method to do something other than what it says it does. Don't do this, unless you want to.
Because Adomain adds a scheme to any string passed to it, schemeless-but-otherwise valid strings do not raise errors. Additionally, Adomain rescues InvalidURIError errors, and instead returns nil.
Adomain["hola"] # => nil
Adomain["::::::::"] # => nil
Adomain[""] # => nil
Adomain["{}"] # => nil
With 0.2 Addressable::URI::InvalidURIError
s are now caught
and ignored. Nil is returned.
Gem::Version.new(Adomain::VERSION) >= Gem::Version.new(0.2) # => true
Adomain["hola"] # => nil
Adomain["::::::::"] # => nil
Adomain[""] # => nil (formerly raised a Addressable::URI::InvalidURIError)
Adomain["{}"] # => nil (formerly raised a Addressable::URI::InvalidURIError)
These methods are simply passed through to parsed Addressable URI objects, and respond accordingly. These are presented for your convenience, and will track with your version of Addressable.
Adomain.path "http://www.xyz.com/pages/123" # => "/pages/123"
Adomain.path "{{{{{" # => "{{{{{"
Adomain.scheme "http://www.xyz.com/pages/123" # => "http"
Adomain.scheme "harrison:ford" # => "harrison"
Adomain.query_values "https://maximumfun.org/donate?amt=20USD" # => {"amt"=>"20USD"}
Adomain.query_values "google.com?q=gagh.biz" # => {"q"=>"gagh.biz"}
However, they are rescued from InvalidURIError, like in other parts of the gem.
Adomain.path "::::::::::::" #=> nil
Adomain.scheme "::::::::::::" #=> nil
Adomain.query_values "::::::::::::" #=> nil
Add this line to your application's Gemfile:
gem 'adomain'
Or install it yourself as:
$ gem install adomain
File bugs against the GitHub issue tracker and pull requests to match, where possible.
Bug reports and pull requests are welcome on GitHub at https://github.com/samnissen/adomain. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
The gem is available as open source under the terms of the MIT License.
Everyone interacting in the Adomain project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.