LazyDomain

Build Status Code Climate Gem Version

Parse varying urls with PublicSuffix the easy way.

Description

The PublicSuffix gem works great if you give it nice, clean urls WITHOUT a transfer protocol. But what if your application has one (or multiple) urls with or without protocols?

LazyDomain assumes it could be either way and uses URI to make up the difference. Because URI can only parse a url WITH a protocol, Lazy Domain automatically adds 'http://' if it is not already present in a url. Now, you can easily use PublicSuffix for varying urls.

Installation

$ gem install lazy_domain

Usage

Because LazyDomain is simply a wrapper for PublicSuffix, the usage is exactly the same.

Basic Usage

Example domain without subdomains.

domain = LazyDomain.parse("google.com")
# => #<PublicSuffix::Domain>
domain.tld
# => "com"
domain.sld
# => "google"
domain.trd
# => nil
domain.domain
# => "google.com"
domain.subdomain
# => nil

Example domain with subdomains.

domain = LazyDomain.parse("www.google.com")
# => #<PublicSuffix::Domain>
domain.tld
# => "com"
domain.sld
# => "google"
domain.trd
# => "www"
domain.domain
# => "google.com"
domain.subdomain
# => "www.google.com"

Simple validation example.

LazyDomain.valid?("google.com")
# => true

LazyDomain.valid?("www.google.com")
# => true

LazyDomain.valid?("x.yz")
# => false

Advanced Usage

You can do everything mentioned above, but the magic of LazyDomain is you can now pass different types of urls!

For example, all of this...

domain = LazyDomain.parse("google.com")

domain = LazyDomain.parse("http://google.com")

domain = LazyDomain.parse("ftp://google.com")   

domain = LazyDomain.parse("somethingcrazy://google.com")

domain = LazyDomain.parse("http://abc.defg.hijk.google.com")

domain = LazyDomain.parse("http://abc.defg.hijk.google.com/asubdirectory/andanother")

...returns something like this.


# => #<PublicSuffix::Domain:0x0055c2344f0168 @sld="google", @tld="com", @trd="abc.defg.hijk">

Enjoy!