Sha256: 62110a4a413fe98aca6469837c1d0666585f86959e2b8726d56fd01e842c8043

Contents?: true

Size: 1.17 KB

Versions: 18

Compression:

Stored size: 1.17 KB

Contents

module GoogleSafeBrowsing
  class TopLevelDomain

    def self.from_host(host)
      components = host.split('.')

      tlds = parse_tld_to_hash

      tld = components.pop
      components.reverse.each do |comp|
        next_tld = "#{comp}.#{tld}"

        if tlds[next_tld]
          tld = next_tld
        else
          break
        end
      end

      tld
    end

    # return array of host components (www, example, com from www.example.com)
    # taking into account of top level domains
    # e.g. 'sub.domain.example.co.uk' => [ 'sub', 'domain', 'example', 'co.uk' ]
    def self.split_from_host(host)
      components = host.split('.')

      tlds = parse_tld_to_hash

      next_tld = components[-2..-1].join('.')
      while tlds[next_tld]
        tmp = components.pop
        components[-1] = components.last + '.' + tmp
        next_tld = components[-2..-1].join('.')
      end

      components
    end


    private 

    def self.parse_tld_to_hash
      hash = Hash.new(nil)
      f = File.open(File.dirname(__FILE__) + '/effective_tld_names.dat.txt', 'r')
      while(line = f.gets)
        hash[line.chomp] = true unless line[0..1] == '//'
      end
      hash
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
google_safe_browsing-0.6.3 lib/google_safe_browsing/top_level_domain.rb
google_safe_browsing-0.6.2 lib/google_safe_browsing/top_level_domain.rb
google_safe_browsing-0.6.1 lib/google_safe_browsing/top_level_domain.rb
google_safe_browsing-0.6.0 lib/google_safe_browsing/top_level_domain.rb
google_safe_browsing-0.5.1 lib/google_safe_browsing/top_level_domain.rb
google_safe_browsing-0.5.0 lib/google_safe_browsing/top_level_domain.rb
google_safe_browsing-0.4.2 lib/google_safe_browsing/top_level_domain.rb
google_safe_browsing-0.4.1 lib/google_safe_browsing/top_level_domain.rb
google_safe_browsing-0.4.0 lib/google_safe_browsing/top_level_domain.rb
google_safe_browsing-0.3.9 lib/google_safe_browsing/top_level_domain.rb
google_safe_browsing-0.3.8 lib/google_safe_browsing/top_level_domain.rb
google_safe_browsing-0.3.7 lib/google_safe_browsing/top_level_domain.rb
google_safe_browsing-0.3.6 lib/google_safe_browsing/top_level_domain.rb
google_safe_browsing-0.3.5 lib/google_safe_browsing/top_level_domain.rb
google_safe_browsing-0.3.4 lib/google_safe_browsing/top_level_domain.rb
google_safe_browsing-0.3.3 lib/google_safe_browsing/top_level_domain.rb
google_safe_browsing-0.3.2 lib/google_safe_browsing/top_level_domain.rb
google_safe_browsing-0.3.1 lib/google_safe_browsing/top_level_domain.rb