Class: PublicSuffix::Rule::Base Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/public_suffix/rule.rb

Overview

This class is abstract.

Abstract rule class

This represent the base class for a Rule definition in the Public Suffix List.

This is intended to be an Abstract class and you shouldn’t create a direct instance. The only purpose of this class is to expose a common interface for all the available subclasses.

## Properties

A rule is composed by 4 properties:

value - A normalized version of the rule name.

The normalization process depends on rule tpe.

Here’s an example

PublicSuffix::Rule.factory("*.google.com")
#<PublicSuffix::Rule::Wildcard:0x1015c14b0
    @value="google.com"
>

## Rule Creation

The best way to create a new rule is passing the rule name to the PublicSuffix::Rule.factory method.

PublicSuffix::Rule.factory("com")
# => PublicSuffix::Rule::Normal

PublicSuffix::Rule.factory("*.com")
# => PublicSuffix::Rule::Wildcard

This method will detect the rule type and create an instance from the proper rule class.

## Rule Usage

A rule describes the composition of a domain name and explains how to tokenize the name into tld, sld and trd.

To use a rule, you first need to be sure the name you want to tokenize can be handled by the current rule. You can use the #match? method.

rule = PublicSuffix::Rule.factory("com")

rule.match?("google.com")
# => true

rule.match?("google.com")
# => false

Rule order is significant. A name can match more than one rule. See the Public Suffix Documentation to learn more about rule priority.

When you have the right rule, you can use it to tokenize the domain name.

rule = PublicSuffix::Rule.factory("com")

rule.decompose("google.com")
# => ["google", "com"]

rule.decompose("www.google.com")
# => ["www.google", "com"]

Direct Known Subclasses

Exception, Normal, Wildcard

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value:, length: nil, private: false) ⇒ Base

Initializes a new rule.

Parameters:

  • value (String)
  • private (Boolean) (defaults to: false)


126
127
128
129
130
# File 'lib/public_suffix/rule.rb', line 126

def initialize(value:, length: nil, private: false)
  @value    = value.to_s
  @length   = length || @value.count(DOT) + 1
  @private  = private
end

Instance Attribute Details

#lengthString (readonly)

Returns the length of the rule.

Returns:

  • (String)

    the length of the rule



108
109
110
# File 'lib/public_suffix/rule.rb', line 108

def length
  @length
end

#privateBoolean (readonly)

Returns true if the rule is a private domain.

Returns:

  • (Boolean)

    true if the rule is a private domain



111
112
113
# File 'lib/public_suffix/rule.rb', line 111

def private
  @private
end

#valueString (readonly)

Returns the rule definition.

Returns:

  • (String)

    the rule definition



105
106
107
# File 'lib/public_suffix/rule.rb', line 105

def value
  @value
end

Class Method Details

.build(content, private: false) ⇒ Object

Initializes a new rule from the content.

Parameters:

  • content (String)

    the content of the rule

  • private (Boolean) (defaults to: false)


118
119
120
# File 'lib/public_suffix/rule.rb', line 118

def self.build(content, private: false)
  new(value: content, private: private)
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Checks whether this rule is equal to other.

Parameters:

Returns:

  • (Boolean)

    Returns true if this rule and other are instances of the same class and has the same value, false otherwise.



138
139
140
# File 'lib/public_suffix/rule.rb', line 138

def ==(other)
  equal?(other) || (self.class == other.class && value == other.value)
end

#decomposeArray<String, nil>

This method is abstract.

Parameters:

  • name (String, #to_s)

    The domain name to decompose

Returns:

  • (Array<String, nil>)

Raises:

  • (NotImplementedError)


181
182
183
# File 'lib/public_suffix/rule.rb', line 181

def decompose(*)
  raise NotImplementedError
end

#match?(name) ⇒ Boolean

Checks if this rule matches name.

A domain name is said to match a rule if and only if all of the following conditions are met:

  • When the domain and rule are split into corresponding labels, that the domain contains as many or more labels than the rule.

  • Beginning with the right-most labels of both the domain and the rule, and continuing for all labels in the rule, one finds that for every pair, either they are identical, or that the label from the rule is “*”.

Examples:

PublicSuffix::Rule.factory("com").match?("example.com")
# => true
PublicSuffix::Rule.factory("com").match?("example.net")
# => false

Parameters:

  • name (String)

    the domain name to check

Returns:

  • (Boolean)

See Also:



164
165
166
167
168
169
170
171
# File 'lib/public_suffix/rule.rb', line 164

def match?(name)
  # Note: it works because of the assumption there are no
  # rules like foo.*.com. If the assumption is incorrect,
  # we need to properly walk the input and skip parts according
  # to wildcard component.
  diff = name.chomp(value)
  diff.empty? || diff.end_with?(DOT)
end

#partsObject

This method is abstract.

Raises:

  • (NotImplementedError)


174
175
176
# File 'lib/public_suffix/rule.rb', line 174

def parts
  raise NotImplementedError
end