class Gman::DomainList

Constants

COMMENT_REGEX

Attributes

contents[W]
data[W]
path[W]

Public Class Methods

current() click to toggle source

The current, government domain list

# File lib/gman/domain_list.rb, line 11
def current
  DomainList.new(path: Gman.list_path)
end
from_file(path) click to toggle source
# File lib/gman/domain_list.rb, line 15
def from_file(path)
  DomainList.new(path: path)
end
from_hash(hash) click to toggle source
# File lib/gman/domain_list.rb, line 19
def from_hash(hash)
  DomainList.new(data: hash)
end
from_public_suffix(string) click to toggle source
# File lib/gman/domain_list.rb, line 23
def from_public_suffix(string)
  DomainList.new(contents: string)
end
Also aliased as: from_string
from_string(string)
Alias for: from_public_suffix
new(path: nil, contents: nil, data: nil) click to toggle source
# File lib/gman/domain_list.rb, line 29
def initialize(path: nil, contents: nil, data: nil)
  @path     = path
  @contents = contents
  @data     = data.reject { |_, domains| domains.compact.empty? } if data
end

Public Instance Methods

alphabetize() click to toggle source

Alphabetize groups and domains within each group We need to ensure exceptions appear after their coresponding rules

# File lib/gman/domain_list.rb, line 86
def alphabetize
  @data = data.sort_by { |k, _v| k.downcase }.to_h
  @data.map do |_group, domains|
    domains.sort! { |a, b| sort_with_exceptions(a, b) }
    domains.uniq!
  end
end
contents() click to toggle source

Returns the raw content of the domain list as a string

# File lib/gman/domain_list.rb, line 36
def contents
  @contents ||= if path
                  File.new(path, 'r:utf-8').read
                else
                  to_s
                end
end
count() click to toggle source

Return the total number of domains in the list

# File lib/gman/domain_list.rb, line 80
def count
  domains.count
end
data() click to toggle source

Returns the parsed contents of the domain list as a hash in the form for group => domains

# File lib/gman/domain_list.rb, line 46
def data
  @data ||= string_to_hash(contents)
end
Also aliased as: to_h
domains() click to toggle source

Return an array of strings representing all domains on the list

# File lib/gman/domain_list.rb, line 75
def domains
  data.values.flatten.compact.sort.uniq
end
groups() click to toggle source

Returns an array of strings representing the list groups

# File lib/gman/domain_list.rb, line 70
def groups
  data.keys
end
parent_domain(domain) click to toggle source

Given a domain, find any domain on the list that includes that domain E.g., ‘fcc.gov` would be the parent of `data.fcc.gov`

# File lib/gman/domain_list.rb, line 117
def parent_domain(domain)
  domains.find { |c| domain =~ /\.#{Regexp.escape(c)}$/ }
end
path() click to toggle source

Returns the path to the domain list on disk

# File lib/gman/domain_list.rb, line 52
def path
  @path ||= Gman.list_path
end
public_suffix_list() click to toggle source

returns an instance of our custom public suffix list list behaves like PublicSuffix::List but is limited to our whitelisted domains

# File lib/gman/domain_list.rb, line 59
def public_suffix_list
  @public_suffix_list ||= PublicSuffix::List.parse(contents)
end
to_h()
Alias for: data
to_public_suffix()
Alias for: to_s
to_s() click to toggle source

The string representation of the domain list, in public suffix format

# File lib/gman/domain_list.rb, line 101
def to_s
  current_group = output = +''
  data.sort_by { |group, _| group.downcase }.each do |group, domains|
    if group != current_group
      output << "\n\n" unless current_group.empty? # first entry
      output << "// #{group}\n"
      current_group = group
    end
    output << domains.join("\n")
  end
  output
end
Also aliased as: to_public_suffix
valid?(domain) click to toggle source

domain is on the domain list

# File lib/gman/domain_list.rb, line 64
def valid?(domain)
  rule = public_suffix_list.find(domain, default: nil)
  !(rule.nil? || rule.is_a?(PublicSuffix::Rule::Exception))
end
write() click to toggle source

Write the domain list to disk

# File lib/gman/domain_list.rb, line 95
def write
  alphabetize
  File.write(path, to_public_suffix)
end

Private Instance Methods

array_to_hash(lines) click to toggle source
# File lib/gman/domain_list.rb, line 135
def array_to_hash(lines)
  domain_hash = {}
  group = ''
  lines.each do |line|
    if COMMENT_REGEX.match?(line)
      group = COMMENT_REGEX.match(line)[1]
    else
      safe_push(domain_hash, group, line.downcase)
    end
  end
  domain_hash
end
safe_push(hash, key, value) click to toggle source

Add a value to an array in a hash, creating the array if necessary hash - the hash key - the key within that hash to add the value to value - the single value to push into the array at hash

# File lib/gman/domain_list.rb, line 152
def safe_push(hash, key, value)
  return if value.empty?

  hash[key] ||= []
  hash[key].push value
end
sort_with_exceptions(left, right) click to toggle source
# File lib/gman/domain_list.rb, line 159
def sort_with_exceptions(left, right)
  if left.start_with?('!') && !right.start_with?('!')
    1
  elsif right.start_with?('!') && !left.start_with?('!')
    -1
  else
    left <=> right
  end
end
string_to_array(string) click to toggle source
# File lib/gman/domain_list.rb, line 131
def string_to_array(string)
  string.gsub(/\r\n?/, "\n").split("\n")
end
string_to_hash(string) click to toggle source

Parse a public-suffix formatted string into a hash of groups => [domains]

# File lib/gman/domain_list.rb, line 124
def string_to_hash(string)
  return unless string

  lines = string_to_array(string)
  array_to_hash(lines)
end