Adomain

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.

Other methods and options

[] 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:

Stripping all subdomains

Adomain.domain "http://abc.xyz.com" # => "xyz.com"

Keeping all subdomains, including 'www'

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.

A gotcha: Adomain returns nil for some invalid values

Because Adomain adds a scheme to any string passed to it Addressable does not error out. So, Adomain returns nil when presented with otherwise-valid strings for which Addressable cannot find a domain.

Adomain["hola"]     # => nil
Adomain["::::::::"] # => nil
Adomain[""]         # => Addressable::URI::InvalidURIError
Adomain["{}"]       # => Addressable::URI::InvalidURIError
Gem::Version.new(Adomain::VERSION) >= Gem::Version.new(0.2) # => false

WARNING: All InvalidURIError will be caught in >= version 0.2

When 0.2 is released, Addressable::URI::InvalidURIErrors will be caught and ignored. Nil will be returned.

Gem::Version.new(Adomain::VERSION) >= Gem::Version.new(0.2) # => true
Adomain["hola"]     # => nil
Adomain["::::::::"] # => nil
Adomain[""]         # => nil
Adomain["{}"]       # => nil

Installation

Add this line to your application's Gemfile:

gem 'adomain'

Or install it yourself as:

$ gem install adomain

Development

File bugs against the GitHub issue tracker and pull requests to match, where possible.

Contributing

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.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the Adomain project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.