Class: PublicSuffix::Rule::Base Abstract
- Inherits:
-
Object
- Object
- PublicSuffix::Rule::Base
- Defined in:
- lib/public_suffix/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 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:
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
PublicSuffix::Rule.factory("*.google.com")
#<PublicSuffix::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 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 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 = PublicSuffix::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 = PublicSuffix::Rule.factory("com")
rule.decompose("google.com")
# => ["google", "com"]
rule.decompose("www.google.com")
# => ["www.google", "com"]
Instance Attribute Summary collapse
-
#labels ⇒ Object
readonly
Returns the value of attribute labels.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
-
.type ⇒ Symbol
The rule type name.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eql?)
Checks whether this rule is equal to
other
. -
#allow?(domain) ⇒ Boolean
Checks if this rule allows
domain
. - #decompose(domain) ⇒ Array<String, nil> abstract
-
#initialize(name, value = nil) ⇒ Base
constructor
Initializes a new rule with name and value.
-
#length ⇒ Integer
Gets the length of this rule for comparison.
-
#match?(domain) ⇒ Boolean
Checks if this rule matches
domain
. - #parts ⇒ Object abstract
- #type ⇒ Object
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.
120 121 122 123 124 |
# File 'lib/public_suffix/rule.rb', line 120 def initialize(name, value = nil) @name = name.to_s @value = value || @name @labels = Domain.domain_to_labels(@value) end |
Instance Attribute Details
#labels ⇒ Object (readonly)
Returns the value of attribute labels.
110 111 112 |
# File 'lib/public_suffix/rule.rb', line 110 def labels @labels end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
110 111 112 |
# File 'lib/public_suffix/rule.rb', line 110 def name @name end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
110 111 112 |
# File 'lib/public_suffix/rule.rb', line 110 def value @value end |
Class Method Details
.type ⇒ Symbol
The rule type name.
131 132 133 |
# File 'lib/public_suffix/rule.rb', line 131 def self.type @type ||= self.name.split("::").last.downcase.to_sym end |
Instance Method Details
#==(other) ⇒ Boolean Also known as: eql?
Checks whether this rule is equal to other
.
150 151 152 153 154 |
# File 'lib/public_suffix/rule.rb', line 150 def ==(other) return false unless other.is_a?(self.class) self.equal?(other) || self.name == other.name end |
#allow?(domain) ⇒ Boolean
Checks if this rule allows domain
.
193 194 195 |
# File 'lib/public_suffix/rule.rb', line 193 def allow?(domain) !decompose(domain).last.nil? end |
#decompose(domain) ⇒ Array<String, nil>
222 223 224 |
# File 'lib/public_suffix/rule.rb', line 222 def decompose(domain) raise(NotImplementedError,"#{self.class}##{__method__} is not implemented") end |
#length ⇒ Integer
Gets the length of this rule for comparison. The length usually matches the number of rule parts
.
Subclasses might actually override this method.
203 204 205 |
# File 'lib/public_suffix/rule.rb', line 203 def length parts.length end |
#match?(domain) ⇒ Boolean
Checks if this rule matches domain
.
172 173 174 175 176 |
# File 'lib/public_suffix/rule.rb', line 172 def match?(domain) l1 = labels l2 = Domain.domain_to_labels(domain) odiff(l1, l2).empty? end |
#parts ⇒ Object
210 211 212 |
# File 'lib/public_suffix/rule.rb', line 210 def parts raise(NotImplementedError,"#{self.class}##{__method__} is not implemented") end |
#type ⇒ Object
138 139 140 |
# File 'lib/public_suffix/rule.rb', line 138 def type self.class.type end |