Class | PublicSuffixService::Rule::Base |
In: |
lib/public_suffix_service/rule.rb
|
Parent: | Object |
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.
A rule is composed by 4 properties:
name - The name of the rule, corresponding to the rule definition
in the public suffix 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" >
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.
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"]
@abstract
labels | [R] | |
name | [R] | |
type | [R] | |
value | [R] |
Initializes a new rule with name and value. If value is nil, name also becomes the value for this rule.
@param [String] name
The name of the rule
@param [String] value
The value of the rule. If nil, defaults to +name+.
Checks whether this rule is equal to other.
@param [PublicSuffixService::Rule::*] other
The rule to compare.
@return [Boolean]
Returns true if this rule and other are instances of the same class and has the same value, false otherwise.
Checks if this rule allows domain.
@param [String, to_s] domain
The domain name to check.
@return [Boolean]
@example
rule = Rule.factory("*.do") # => #<PublicSuffixService::Rule::Wildcard> rule.allow?("example.do") # => false rule.allow?("www.example.do") # => true
@param [String, to_s] domain
The domain name to decompose.
@return [Array<String, nil>]
@raise [NotImplementedError] @abstract