lib/public_suffix/rule.rb in public_suffix-2.0.5 vs lib/public_suffix/rule.rb in public_suffix-3.0.0
- old
+ new
@@ -17,10 +17,13 @@
# PublicSuffix::Rule.factory("ar")
# # => #<PublicSuffix::Rule::Normal>
#
module Rule
+ # @api internal
+ Entry = Struct.new(:type, :length, :private)
+
# = Abstract rule class
#
# This represent the base class for a Rule definition
# in the {Public Suffix List}[https://publicsuffix.org].
#
@@ -97,20 +100,32 @@
class Base
# @return [String] the rule definition
attr_reader :value
+ # @return [String] the length of the rule
+ attr_reader :length
+
# @return [Boolean] true if the rule is a private domain
attr_reader :private
- # Initializes a new rule with name and value.
- # If value is +nil+, name also becomes the value for this rule.
+ # Initializes a new rule from the content.
#
- # @param value [String] the value of the rule
- def initialize(value, private: false)
+ # @param content [String] the content of the rule
+ # @param private [Boolean]
+ def self.build(content, private: false)
+ new(value: content, private: private)
+ end
+
+ # Initializes a new rule.
+ #
+ # @param value [String]
+ # @param private [Boolean]
+ def initialize(value:, length: nil, private: false)
@value = value.to_s
+ @length = length || @value.count(DOT) + 1
@private = private
end
# Checks whether this rule is equal to <tt>other</tt>.
#
@@ -135,37 +150,32 @@
# either they are identical, or that the label from the rule is "*".
#
# @see https://publicsuffix.org/list/
#
# @example
- # Rule.factory("com").match?("example.com")
+ # PublicSuffix::Rule.factory("com").match?("example.com")
# # => true
- # Rule.factory("com").match?("example.net")
+ # PublicSuffix::Rule.factory("com").match?("example.net")
# # => false
#
- # @param name [String, #to_s] The domain name to check.
+ # @param name [String] the domain name to check
# @return [Boolean]
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[-1] == "."
+ diff.empty? || diff[-1] == DOT
end
# @abstract
def parts
raise NotImplementedError
end
# @abstract
- def length
- raise NotImplementedError
- end
-
- # @abstract
# @param [String, #to_s] name The domain name to decompose
# @return [Array<String, nil>]
def decompose(*)
raise NotImplementedError
end
@@ -198,31 +208,30 @@
# @return [Array<String>]
def parts
@value.split(DOT)
end
- # Gets the length of this rule for comparison,
- # represented by the number of dot-separated parts in the rule.
- #
- # @return [Integer] The length of the rule.
- def length
- @length ||= parts.length
- end
-
end
# Wildcard represents a wildcard rule (e.g. *.co.uk).
class Wildcard < Base
- # Initializes a new rule from +definition+.
+ # Initializes a new rule from the content.
#
- # The wildcard "*" is removed from the value, as it's common
- # for each wildcard rule.
+ # @param content [String] the content of the rule
+ # @param private [Boolean]
+ def self.build(content, private: false)
+ new(value: content.to_s[2..-1], private: private)
+ end
+
+ # Initializes a new rule.
#
- # @param definition [String] the rule as defined in the PSL
- def initialize(definition, private: false)
- super(definition.to_s[2..-1], private: private)
+ # @param value [String]
+ # @param private [Boolean]
+ def initialize(value:, length: nil, private: false)
+ super(value: value, length: length, private: private)
+ length or @length += 1 # * counts as 1
end
# Gets the original rule definition.
#
# @return [String] The rule definition.
@@ -246,32 +255,21 @@
# @return [Array<String>]
def parts
@value.split(DOT)
end
- # Gets the length of this rule for comparison,
- # represented by the number of dot-separated parts in the rule
- # plus 1 for the *.
- #
- # @return [Integer] The length of the rule.
- def length
- @length ||= parts.length + 1 # * counts as 1
- end
-
end
# Exception represents an exception rule (e.g. !parliament.uk).
class Exception < Base
- # Initializes a new rule from +definition+.
+ # Initializes a new rule from the content.
#
- # The bang ! is removed from the value, as it's common
- # for each wildcard rule.
- #
- # @param definition [String] the rule as defined in the PSL
- def initialize(definition, private: false)
- super(definition.to_s[1..-1], private: private)
+ # @param content [String] the content of the rule
+ # @param private [Boolean]
+ def self.build(content, private: false)
+ new(value: content.to_s[1..-1], private: private)
end
# Gets the original rule definition.
#
# @return [String] The rule definition.
@@ -300,18 +298,10 @@
# @return [Array<String>]
def parts
@value.split(DOT)[1..-1]
end
- # Gets the length of this rule for comparison,
- # represented by the number of dot-separated parts in the rule.
- #
- # @return [Integer] The length of the rule.
- def length
- @length ||= parts.length
- end
-
end
# Takes the +name+ of the rule, detects the specific rule class
# and creates a new instance of that class.
@@ -337,10 +327,10 @@
Wildcard
when BANG
Exception
else
Normal
- end.new(content, private: private)
+ end.build(content, private: private)
end
# The default rule to use if no rule match.
#
# The default rule is "*". From https://publicsuffix.org/list/: