forked from pauldix/domainatrix
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added the basic file parsing and building of the domain tree
- Loading branch information
Showing
3 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |