Class: PublicSuffixService::Rule::Base

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

Overview

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 sholnd’t create a direct instance. The only purpose of this class is to expose a common interface for all the available subclasses.

  • PublicSuffixService::Rule::Normal

  • PublicSuffixService::Rule::Exception

  • PublicSuffixService::Rule::Wildcard

Properties

A rule is composed by 4 properties:

name - The name of the rule, corresponding to the rule definition

in the public suffic list

value - The value, a normalized version of the rule name.

The normalization process depends on rule tpe.

type - The rule type (:normal, :wildcard, :exception) labels - The canonicalized rule name

Here’s an example

PublicSuffixService::Rule.factory("*.google.com")
#<PublicSuffixService::Rule::Wildcard:0x1015c14b0 
    @labels=["com", "google"],
    @name="*.google.com",
    @type=:wildcard,
    @value="google.com"
>

Rule Creation

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

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

PublicSuffixService::Rule.factory("*.com")
# => PublicSuffixService::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 domain name into tld, sld and trd.

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

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

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

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

Rule order is significant. A domain 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 = PublicSuffixService::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

Instance Method Summary collapse

Constructor Details

#initialize(name, value = nil) ⇒ Base

Initializes a new rule with name and value. If value is nil, name also becomes the value for this rule.



137
138
139
140
141
142
# File 'lib/public_suffix_service/rule.rb', line 137

def initialize(name, value = nil)
  @name   = name.to_s
  @value  = value || @name
  @type   = self.class.name.split("::").last.downcase.to_sym
  @labels = Domain.domain_to_labels(@value)
end

Instance Attribute Details

#labelsObject (readonly)

Returns the value of attribute labels.



133
134
135
# File 'lib/public_suffix_service/rule.rb', line 133

def labels
  @labels
end

#nameObject (readonly)

Returns the value of attribute name.



133
134
135
# File 'lib/public_suffix_service/rule.rb', line 133

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



133
134
135
# File 'lib/public_suffix_service/rule.rb', line 133

def type
  @type
end

#valueObject (readonly)

Returns the value of attribute value.



133
134
135
# File 'lib/public_suffix_service/rule.rb', line 133

def value
  @value
end

Instance Method Details

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

Checks whether this rule is equal to other.

other - The PublicSuffixService::Rule::Base to compare.

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



150
151
152
153
154
# File 'lib/public_suffix_service/rule.rb', line 150

def ==(other)
  return false unless other.is_a?(self.class)
  self.equal?(other) ||
  self.name == other.name
end

#decompose(domain) ⇒ Object

Raises NotImplementedError.

Raises:

  • (NotImplementedError)


184
185
186
# File 'lib/public_suffix_service/rule.rb', line 184

def decompose(domain)
  raise NotImplementedError
end

#lengthObject

Gets the length of this rule for comparison. The length usually matches the number of rule parts. Subclasses might actually override this method.

Returns an Integer with the number of parts.



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

def length
  parts.length
end

#match?(domain) ⇒ Boolean

Checks whether this rule matches domain.

domain - The String domain name to check.

Returns Boolean.

Returns:

  • (Boolean)


163
164
165
166
167
# File 'lib/public_suffix_service/rule.rb', line 163

def match?(domain)
  l1 = labels
  l2 = Domain.domain_to_labels(domain)
  odiff(l1, l2).empty?
end

#partsObject

Raises NotImplementedError.

Raises:

  • (NotImplementedError)


179
180
181
# File 'lib/public_suffix_service/rule.rb', line 179

def parts
  raise NotImplementedError
end