Sha256: 21f0f82fd932376b63bc79c8bf8b2c13920f0da761cfa56a6a6e7951c93568f2

Contents?: true

Size: 1.76 KB

Versions: 5

Compression:

Stored size: 1.76 KB

Contents

class Gman
  class DomainList

    attr_accessor :list
    alias_method :to_h, :list

    COMMENT_REGEX = /\/\/[\/\s]*(.*)$/i

    def initialize(list)
      @list = list.reject { |group, domains| domains.compact.empty? }
    end

    def groups
      list.keys
    end

    def domains
      list.values.flatten
    end

    def count
      domains.count
    end

    def alphabetize
      @list = @list.sort_by { |k,v| k.downcase }.to_h
      @list.each { |group, domains| domains.sort!.uniq! }
    end

    def write
      File.write(Gman.list_path, to_public_suffix)
    end

    def to_public_suffix
      current_group = ""
      output = ""
      list.sort_by { |group, domains| 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

    def self.current
      current = File.open(Gman::list_path).read
      DomainList.from_public_suffix(current)
    end

    def self.from_public_suffix(string)
      string = string.gsub(/\r\n?/, "\n").split("\n")
      hash = array_to_hash(string)
      DomainList.new(hash)
    end

    private

    # Given an array of comments/domains in public suffix format
    # Converts to a hash in the form of :group => [domain1, domain2...]
    def self.array_to_hash(domains)
      group = ""
      domain_hash = {}
      domains.each do |line|
        next if line.empty?
        if match = COMMENT_REGEX.match(line)
          group = match[1]
        else
          domain_hash[group] = [] if domain_hash[group].nil?
          domain_hash[group].push line.downcase
        end
      end
      domain_hash
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
gman-5.0.6 lib/gman/domain_list.rb
gman-5.0.5 lib/gman/domain_list.rb
gman-5.0.4 lib/gman/domain_list.rb
gman-5.0.3 lib/gman/domain_list.rb
gman-5.0.2 lib/gman/domain_list.rb