Class PublicSuffixService::RuleList
In: lib/public_suffix_service/rule_list.rb
Parent: Object

A {PublicSuffixService::RuleList} is a collection of one or more {PublicSuffixService::Rule}.

Given a {PublicSuffixService::RuleList}, you can add or remove {PublicSuffixService::Rule}, iterate all items in the list or search for the first rule which matches a specific domain name.

  # Create a new list
  list =  PublicSuffixService::RuleList.new

  # Push two rules to the list
  list << PublicSuffixService::Rule.factory("it")
  list << PublicSuffixService::Rule.factory("com")

  # Get the size of the list
  list.size
  # => 2

  # Search for the rule matching given domain
  list.find("example.com")
  # => #<PublicSuffixService::Rule::Normal>
  list.find("example.org")
  # => nil

You can create as many {PublicSuffixService::RuleList} you want. The {PublicSuffixService::RuleList.default} rule list is used to tokenize and validate a domain.

{PublicSuffixService::RuleList} implements Enumerable module.

Methods

<<   ==   add   clear   clear   create_index!   default   default=   default_definition   each   empty?   eql?   find   length   new   parse   reload   select   size   to_a  

Included Modules

Enumerable

Attributes

indexes  [R]  Gets the naive index, a hash that with the keys being the first label of every rule pointing to an array of integers (indexes of the rules in @list)

@return [Array]

list  [R]  Gets the list of rules.

@return [Array<PublicSuffixService::Rule::*>]

Public Class methods

Sets the default rule list to nil.

@return [self]

Gets the default rule list. Initializes a new {PublicSuffixService::RuleList} parsing the content of {PublicSuffixService::RuleList.default_definition}, if required.

@return [PublicSuffixService::RuleList]

Sets the default rule list to value.

@param [PublicSuffixService::RuleList] value

  The new rule list.

@return [PublicSuffixService::RuleList]

Gets the default definition list. Can be any IOStream including a File or a simple String. The object must respond to each_line.

@return [File]

Initializes an empty {PublicSuffixService::RuleList}.

@yield [self] Yields on self. @yieldparam [PublicSuffixService::RuleList] self The newly creates instance

Parse given input treating the content as Public Suffix List.

See publicsuffix.org/format/ for more details about input format.

@param [String] input The rule list to parse.

@return [Array<PublicSuffixService::Rule::*>]

Resets the default rule list and reinitialize it parsing the content of {PublicSuffixService::RuleList.default_definition}.

@return [PublicSuffixService::RuleList]

Public Instance methods

<<(rule, index = true)

Alias for add

Checks whether two lists are equal.

RuleList one is equal to two, if two is an instance of {PublicSuffixService::RuleList} and each +PublicSuffixService::Rule::*+ in list one is available in list two, in the same order.

@param [PublicSuffixService::RuleList] other

  The rule list to compare.

@return [Boolean]

 Adds the given object to the list

 and optionally refreshes the rule index.

 @param [PublicSuffixService::Rule::*] rule
   The rule to add to the list.
 @param [Boolean] index
   Set to true to recreate the rule index
   after the rule has been added to the list.

 @return [self]

 @see #create_index!

Removes all elements.

@return [self]

Creates a naive index for +@list+. Just a hash that will tell us where the elements of +@list+ are relative to its first {PublicSuffixService::Rule::Base#labels} element.

For instance if @list[5] and @list[4] are the only elements of the list where Rule#labels.first is ‘us’ @indexes[‘us’] #=> [5,4], that way in select we can avoid mapping every single rule against the candidate domain.

Iterates each rule in the list.

Checks whether the list is empty.

@return [Boolean]

eql?(other)

Alias for #==

Returns the most appropriate rule for domain.

From the Public Suffix List documentation:

  • If a hostname matches more than one rule in the file, the longest matching rule (the one with the most levels) will be used.
  • An exclamation mark (!) at the start of a rule marks an exception to a previous wildcard rule. An exception rule takes priority over any other matching rule.

Algorithm description

  • Match domain against all rules and take note of the matching ones.
  • If no rules match, the prevailing rule is "*".
  • If more than one rule matches, the prevailing rule is the one which is an exception rule.
  • If there is no matching exception rule, the prevailing rule is the one with the most labels.
  • If the prevailing rule is a exception rule, modify it by removing the leftmost label.
  • The public suffix is the set of labels from the domain which directly match the labels of the prevailing rule (joined by dots).
  • The registered domain is the public suffix plus one additional label.

@param [String, to_s] domain The domain name.

@return [PublicSuffixService::Rule::*, nil]

length()

Alias for size

Selects all the rules matching given domain.

Will use +@indexes+ to try only the rules that share the same first label, that will speed up things when using +RuleList.find(‘foo’)+ a lot.

@param [String, to_s] domain The domain name.

@return [Array<PublicSuffixService::Rule::*>]

Gets the number of elements in the list.

@return [Integer]

Gets the list as array.

@return [Array<PublicSuffixService::Rule::*>]

[Validate]