Skip to content

Commit

Permalink
added the basic file parsing and building of the domain tree
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldix committed Dec 10, 2009
1 parent 1af9ca6 commit 7f7f16b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/domainatrix.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
$LOAD_PATH.unshift(File.dirname(__FILE__)) unless $LOAD_PATH.include?(File.dirname(__FILE__))

require 'domainatrix/domain_parser.rb'

module Domainatrix
VERSION = "0.0.1"
end
24 changes: 24 additions & 0 deletions lib/domainatrix/domain_parser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module Domainatrix
class DomainParser
attr_reader :tlds

def initialize(file_name)
@tlds = {}
read_dat_file(file_name)
end

def read_dat_file(file_name)
File.readlines(file_name).each do |line|
line = line.strip
unless line.start_with? "//" || line.empty?
parts = line.split(".").reverse

sub_hash = @tlds
parts.each do |part|
sub_hash = (sub_hash[part] ||= {})
end
end
end
end
end
end
25 changes: 25 additions & 0 deletions spec/domainatrix/domain_parser_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require File.dirname(__FILE__) + '/../spec_helper'

describe "domain parser" do
before(:all) do
@domain_parser = Domainatrix::DomainParser.new("#{File.dirname(__FILE__)}/../../lib/effective_tld_names.dat")
end

describe "reading the dat file" do
it "create a trie of the domain names" do
@domain_parser.tlds.should be_a Hash
end

it "should have the first level of the tree" do
@domain_parser.tlds.should have_key("com")
end

it "should have the first level of the tree even when the first doesn't appear on a line by itself" do
@domain_parser.tlds.should have_key("uk")
end

it "should have lower levels of the tree" do
@domain_parser.tlds["jp"].should have_key("ac")
end
end
end

0 comments on commit 7f7f16b

Please sign in to comment.