Code Monkey home page Code Monkey logo

publicsuffix-ruby's Introduction

Public Suffix List - Ruby implementation

PublicSuffix is a Ruby domain name parser based on the Public Suffix List.

Build Status

What is the Public Suffix List?

The Public Suffix List is a cross-vendor initiative to provide an accurate list of domain name suffixes.

The Public Suffix List is an initiative of the Mozilla Project, but is maintained as a community resource. It is available for use in any software, but was originally created to meet the needs of browser manufacturers.

A "public suffix" is one under which Internet users can directly register names. Some examples of public suffixes are ".com", ".co.uk" and "pvt.k12.wy.us". The Public Suffix List is a list of all known public suffixes.

Source: http://publicsuffix.org

Why the Public Suffix List is better than any available Regular Expression parser?

Previously, browsers used an algorithm which basically only denied setting wide-ranging cookies for top-level domains with no dots (e.g. com or org). However, this did not work for top-level domains where only third-level registrations are allowed (e.g. co.uk). In these cases, websites could set a cookie for co.uk which will be passed onto every website registered under co.uk.

Clearly, this was a security risk as it allowed websites other than the one setting the cookie to read it, and therefore potentially extract sensitive information.

Since there is no algorithmic method of finding the highest level at which a domain may be registered for a particular top-level domain (the policies differ with each registry), the only method is to create a list of all top-level domains and the level at which domains can be registered. This is the aim of the effective TLD list.

As well as being used to prevent cookies from being set where they shouldn't be, the list can also potentially be used for other applications where the registry controlled and privately controlled parts of a domain name need to be known, for example when grouping by top-level domains.

Source: https://wiki.mozilla.org/Public_Suffix_List

Not convinced yet? Check out this real world example.

Requirements

  • Ruby >= 1.8.7

PublicSuffix requires Ruby 1.8.7 or greater. For older versions of Ruby, see the CHANGELOG.md file.

We also support several Ruby implementations.

Installation

The best way to install PublicSuffix is via RubyGems.

$ gem install public_suffix

You might need administrator privileges on your system to install the gem.

Basic Usage

Example domain without subdomains.

domain = PublicSuffix.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 = PublicSuffix.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.

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

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

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

Fully Qualified Domain Names

This library automatically recognizes Fully Qualified Domain Names. A FQDN is a domain name that end with a trailing dot.

# Parse a standard domain name
domain = PublicSuffix.parse("www.google.com")
# => #<PublicSuffix::Domain>
domain.tld
# => "com"

# Parse a fully qualified domain name
domain = PublicSuffix.parse("www.google.com.")
# => #<PublicSuffix::Domain>
domain.tld
# => "com"

Private domains

This library has support for switching off support for private (non-ICANN) domains

# Parse a domain on a private TLD
domain = PublicSuffix.parse("something.blogspot.com")
# => #<PublicSuffix::Domain>
domain.tld
# => "blogspot.com"

# Disable support for private TLDs
PublicSuffix::List.private_domains = false
# => #<PublicSuffix::List>
domain = PublicSuffix.parse("something.blogspot.com")
# => #<PublicSuffix::Domain>
domain.tld
# => "com"

Feedback and bug reports

If you use this library and find yourself missing any functionality, please let me know.

Pull requests are very welcome! Please include tests and/or feature coverage for every patch, and create a topic branch for every separate change you make.

Report issues or feature requests to GitHub Issues.

More

Changelog

See the CHANGELOG.md file for details.

License

Copyright (c) 2009-2013 Simone Carletti. This is Free Software distributed under the MIT license.

publicsuffix-ruby's People

Contributors

weppos avatar rb2k avatar jayeff avatar itspriddle avatar leereilly avatar thierryc avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.