Class: PublicSuffix::List
- Inherits:
-
Object
- Object
- PublicSuffix::List
- Defined in:
- lib/public_suffix/list.rb
Overview
A List is a collection of one or more Rule.
Given a List, you can add or remove Rule, iterate all items in the list or search for the first rule which matches a specific domain name.
# Create a new list
list = PublicSuffix::List.new
# Push two rules to the list
list << PublicSuffix::Rule.factory("it")
list << PublicSuffix::Rule.factory("com")
# Get the size of the list
list.size
# => 2
# Search for the rule matching given domain
list.find("example.com")
# => #<PublicSuffix::Rule::Normal>
list.find("example.org")
# => nil
You can create as many List you want. The List.default rule list is used to tokenize and validate a domain.
Constant Summary collapse
- DEFAULT_LIST_PATH =
File.("../../data/list.txt", __dir__)
Class Method Summary collapse
-
.default(**options) ⇒ PublicSuffix::List
Gets the default rule list.
-
.default=(value) ⇒ PublicSuffix::List
Sets the default rule list to
value
. -
.parse(input, private_domains: true) ⇒ PublicSuffix::List
Parse given
input
treating the content as Public Suffix List.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eql?)
Checks whether two lists are equal.
-
#add(rule) ⇒ self
(also: #<<)
Adds the given object to the list and optionally refreshes the rule index.
-
#clear ⇒ self
Removes all rules.
-
#default_rule ⇒ PublicSuffix::Rule::*
Gets the default rule.
-
#each(&block) ⇒ Object
Iterates each rule in the list.
-
#empty? ⇒ Boolean
Checks whether the list is empty.
-
#find(name, default: default_rule, **options) ⇒ PublicSuffix::Rule::*
Finds and returns the rule corresponding to the longest public suffix for the hostname.
-
#initialize {|self| ... } ⇒ List
constructor
Initializes an empty List.
-
#size ⇒ Integer
Gets the number of rules in the list.
Constructor Details
#initialize {|self| ... } ⇒ List
Initializes an empty PublicSuffix::List.
106 107 108 109 |
# File 'lib/public_suffix/list.rb', line 106 def initialize @rules = {} yield(self) if block_given? end |
Class Method Details
.default(**options) ⇒ PublicSuffix::List
Gets the default rule list.
Initializes a new PublicSuffix::List parsing the content of default_list_content, if required.
50 51 52 |
# File 'lib/public_suffix/list.rb', line 50 def self.default(**) @default ||= parse(File.read(DEFAULT_LIST_PATH), **) end |
.default=(value) ⇒ PublicSuffix::List
Sets the default rule list to value
.
58 59 60 |
# File 'lib/public_suffix/list.rb', line 58 def self.default=(value) @default = value end |
.parse(input, private_domains: true) ⇒ PublicSuffix::List
Parse given input
treating the content as Public Suffix List.
See publicsuffix.org/format/ for more details about input format.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/public_suffix/list.rb', line 69 def self.parse(input, private_domains: true) comment_token = "//" private_token = "===BEGIN PRIVATE DOMAINS===" section = nil # 1 == ICANN, 2 == PRIVATE new do |list| input.each_line do |line| line.strip! case # rubocop:disable Style/EmptyCaseCondition # skip blank lines when line.empty? next # include private domains or stop scanner when line.include?(private_token) break if !private_domains section = 2 # skip comments when line.start_with?(comment_token) # rubocop:disable Lint/DuplicateBranch next else list.add(Rule.factory(line, private: section == 2)) end end end end |
Instance Method Details
#==(other) ⇒ Boolean Also known as: eql?
Checks whether two lists are equal.
List one
is equal to two
, if two
is an instance of PublicSuffix::List and each PublicSuffix::Rule::*
in list one
is available in list two
, in the same order.
120 121 122 123 124 |
# File 'lib/public_suffix/list.rb', line 120 def ==(other) return false unless other.is_a?(List) equal?(other) || @rules == other.rules end |
#add(rule) ⇒ self Also known as: <<
Adds the given object to the list and optionally refreshes the rule index.
141 142 143 144 |
# File 'lib/public_suffix/list.rb', line 141 def add(rule) @rules[rule.value] = rule_to_entry(rule) self end |
#clear ⇒ self
Removes all rules.
164 165 166 167 |
# File 'lib/public_suffix/list.rb', line 164 def clear @rules.clear self end |
#default_rule ⇒ PublicSuffix::Rule::*
Gets the default rule.
226 227 228 |
# File 'lib/public_suffix/list.rb', line 226 def default_rule PublicSuffix::Rule.default end |
#each(&block) ⇒ Object
Iterates each rule in the list.
128 129 130 131 132 133 134 |
# File 'lib/public_suffix/list.rb', line 128 def each(&block) Enumerator.new do |y| @rules.each do |key, node| y << entry_to_rule(node, key) end end.each(&block) end |
#empty? ⇒ Boolean
Checks whether the list is empty.
157 158 159 |
# File 'lib/public_suffix/list.rb', line 157 def empty? @rules.empty? end |
#find(name, default: default_rule, **options) ⇒ PublicSuffix::Rule::*
Finds and returns the rule corresponding to the longest public suffix for the hostname.
174 175 176 177 178 179 180 181 |
# File 'lib/public_suffix/list.rb', line 174 def find(name, default: default_rule, **) rule = select(name, **).inject do |l, r| return r if r.instance_of?(Rule::Exception) l.length > r.length ? l : r end rule || default end |
#size ⇒ Integer
Gets the number of rules in the list.
150 151 152 |
# File 'lib/public_suffix/list.rb', line 150 def size @rules.size end |