Naughty or Nice

Build Status Gem Version

Naughty or Nice simplifies the process of extracting domain information from a domain-like string (an email, a URL, etc.) and checking whether it meets criteria you specify.


Naughty or Nice doesn't do too much on its own. Out of the box, it can extract a domain from a domain-like string, and can verify that it is, in fact, a valid domain. It does this by leveraging the power of Addressable, the Public Suffix List, and the associated Ruby Gem.

The true power of Naughty or Nice comes when you include it into your own class.

Implementing Naughty or Nice

Let's say you have a list of three domains,,, and You'd spec out a class like so:

class Checker

  include NaughtyOrNice
  DOMAINS = %w[]

  def valid?
    DOMAINS.include? domain.to_s

That's it! Just overwrite the valid? method and Naughty or Nice takes care of the rest.

You can also get more complicated. Let's say you only wanted to allow .gov domains:

class Checker

  include NaughtyOrNice

  def valid?
    domain.tld == "gov"

Using the included methods

There are a handful of magic methods that your class automatically gets. You can throw any domain-like string at your new Checker class, and figure out if it's on the list. Here's a few examples:

Checker.valid? "" #=> true
Checker.valid? "" #=> false

Notice we're using the class method valid? That automatically calls for you. Cool, huh?

But you don't just need to give Checker crisp, clean domains. Let's get a bit trickier:

Checker.valid? "" #=> true
Checker.valid? "[email protected]" #=> true
Checker.valid? "foobar" => false

Extracting domain information

You can also you NaughtyOrNice to extract domain information for use elsewhere. Continuing our above example:

address = "[email protected]"
address.valid?           #=> true
address.domain.to_s      #=> ""
address.domain.tld       #=> "com"
address.domain.sld       #=> "bar"

See it in action

Take a look at Gman to see Naughty or Nice in action. Gman uses a crowd-sourced list of government domains to check if a given email address is a government email.