Dominatrix

Extract the registered domain name from a URI.

Installation

Add this line to your application's Gemfile:

gem 'dominatrix'

And then execute:

$ bundle

Or install it yourself as:

$ gem install dominatrix

Usage

Parsing Domains

There are two approaches to parsing domains with Dominatrix: Dominatrix.parse and Dominatrix#parse.

Dominatrix.parse

Call the Dominatrix.parse method with a URI object or parseable URI string.

Dominatrix.parse('http://www.google.com')
# => "google.com"

uri = URI.parse('http://maps.google.co.uk/map?foo=bar')
Dominatrix.parse(uri)
# => "google.co.uk"

Dominatrix.parse('http://10.0.0.1')
# => Dominatrix::NotFoundError: no matching domain for "http://10.0.0.1"

A second parameter can alternatively be passed to define the known extensions. This parameter should be an Enumerable (optimally a Set) in which each extension contains the leading dot. By default, this value is set to Dominatrix.default_extensions.

extensions = %w[.com .net .co.uk].to_set

Dominatrix.parse('http://www.google.com', extensions)
# => "google.com"

Dominatrix.parse('http://www.google.ca', extensions)
# => Dominatrix::NotFoundError: no matching domain for "http://www.google.ca"

If a domain is found, then a string will be returned. Otherwise, one of the following errors will be raised:

  • ArgumentError: the URI param was not passed in as a String or URI, or the URI param does not include a host, or the extensions param was not passed in as an Enumerable.
  • URI::InvalidURIError: the URI was passed in as a String and cannot be parsed.
  • Dominatrix::NotFoundError: the URI does not contain a valid domain name.

Dominatrix#parse

Instantiate a Dominatrix object by passing in an optional set of valid domain extensions. By default, this value is set to Dominatrix.default_extensions.

dominatrix = Dominatrix.new
# => #<Dominatrix:0x007fda8428a8f0 @extensions=#<Set: {".ac", ".com.ac", ".gov.ac", ".mil.ac", ... }>>

dominatrix = Dominatrix.new(%w[.com .net].to_set)
# => #<Dominatrix:0x007fda823861c0 @extensions=#<Set: {".com", ".net"}>>

Once instantiated, call the #parse method with a URI object or parseable URI string.

dominatrix.parse('http://www.google.com')
# => "google.com"

dominatrix.parse('http://www.google.foo')
# => Dominatrix::NotFoundError: no matching domain for "http://www.google.foo"

Domain Extensions

To see the list of domain extensions built into the gem, call the Dominatrix.default_extensions method.

Dominatrix.default_extensions
# => #<Set: {".ac", ".com.ac", ".gov.ac", ".mil.ac", ... }>

You can manually add to and remove from this list via the Set#add (Set#<<) and Set#delete methods.

Dominatrix.default_extensions.include? '.foo'
# => false
Dominatrix.default_extensions.include? '.com'
# => true

Dominatrix.default_extensions << '.foo'
Dominatrix.default_extensions.delete('.com')

Dominatrix.default_extensions.include? '.foo'
# => true
Dominatrix.default_extensions.include? '.com'
# => false

Known Issues

This gem does not currently work with domains that require right-to-left language support.

Contributing

  1. Fork it ( https://github.com/[my-github-username]/dominatrix/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request