Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactors and addressed some issues. #19

Closed
wants to merge 26 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
6e26e8f
Allow specifying of custom/additional TLD names to be recognized
coreyward Apr 14, 2011
c3b508f
Remove deprecated call to has_rdoc from Gemspec
coreyward May 3, 2011
43bda05
Fix incorrect version in gemspec
Aug 16, 2011
af33fd1
Update with latest list from publicsuffix.org/
Aug 19, 2011
3add608
Merge remote-tracking branch 'jaimiecobb/master'
coreyward Oct 12, 2011
76c206c
Handle parse errors gracefully
coreyward Oct 12, 2011
b086aeb
Test esoteric utf-8 tlds
coreyward Oct 12, 2011
7499782
Removed white-spaces.
fivetwentysix Nov 21, 2011
22200d2
Removed a code duplication.
fivetwentysix Nov 21, 2011
3f96119
Removed duplication.
fivetwentysix Nov 21, 2011
d2affc7
Removed duplication.
fivetwentysix Nov 21, 2011
e1506b6
Removed duplication.
fivetwentysix Nov 21, 2011
8ae4751
Removed duplications.
fivetwentysix Nov 21, 2011
884a124
Split up a long method.
fivetwentysix Nov 21, 2011
7b24278
Simplified variable initialization.
fivetwentysix Nov 21, 2011
c73e4f9
Used a more descriptive variable name.
fivetwentysix Nov 21, 2011
7db90d9
Simplified condition.
fivetwentysix Nov 21, 2011
f4960a4
Resolved Issue #16
fivetwentysix Nov 21, 2011
a759498
Added valid? method to the Domainatrix::Url class.
fivetwentysix Nov 21, 2011
6c9616b
Increased test coverage of Url#valid? method.
fivetwentysix Nov 21, 2011
a6c0ea9
Removed unused exception.
fivetwentysix Nov 21, 2011
ea23799
Bumped to 0.0.12
fivetwentysix Nov 21, 2011
514cc7e
Url#canonical returns nil unless valid.
fivetwentysix Nov 21, 2011
186197a
Removed redundant code.
fivetwentysix Nov 21, 2011
c3511bf
Tested against issue #15.
fivetwentysix Nov 21, 2011
b0837c6
URL will default to the HTTP scheme if no scheme defined.
fivetwentysix Nov 21, 2011
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions domainatrix.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Gem::Specification.new do |s|
s.name = %q{domainatrix}
s.version = "0.0.9"
s.version = "0.0.12"

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Paul Dix"]
Expand All @@ -19,7 +19,6 @@ Gem::Specification.new do |s|
"spec/domainatrix_spec.rb",
"spec/domainatrix/domain_parser_spec.rb",
"spec/domainatrix/url_spec.rb"]
s.has_rdoc = true
s.homepage = %q{http://github.com/pauldix/domainatrix}
s.require_paths = ["lib"]
s.rubygems_version = %q{1.3.5}
Expand All @@ -35,4 +34,4 @@ Gem::Specification.new do |s|
end
else
end
end
end
6 changes: 5 additions & 1 deletion lib/domainatrix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ module Domainatrix
def self.parse(url)
Url.new(DOMAIN_PARSER.parse(url))
end
end

def self.recognize_tld(tld)
DOMAIN_PARSER.add_domain(tld)
end
end
73 changes: 41 additions & 32 deletions lib/domainatrix/domain_parser.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Domainatrix
class DomainParser
include Addressable

attr_reader :public_suffixes

def initialize(file_name)
Expand All @@ -16,62 +16,71 @@ def read_dat_file(file_name)
else
dat_file = File.open(file_name)
end

dat_file.each_line do |line|
line = line.strip
unless (line =~ /\/\//) || line.empty?
parts = line.split(".").reverse
add_domain(line) unless line.match(/\/\//)
end
end

sub_hash = @public_suffixes
parts.each do |part|
sub_hash = (sub_hash[part] ||= {})
end
end
def add_domain(name)
parts = name.split(".").reverse

sub_hash = @public_suffixes
parts.each do |part|
sub_hash = (sub_hash[part] ||= {})
end
end

def parse(url)
uri = URI.parse(url)
if uri.query
path = "#{uri.path}?#{uri.query}"
else
path = uri.path
url = "http://" + url unless url.match(/\/\//)
begin
uri = URI.parse(url)
rescue Addressable::URI::InvalidURIError
return {}
end
path = uri.path
query = uri.query
path += "?#{query}" if query

parse_domains_from_host(uri.host).merge({
:scheme => uri.scheme,
:host => uri.host,
:path => path,
:url => url
})
end

def parse_domains_from_host(host)
unless host
return {:public_suffix => "", :domain => "", :subdomain => "", :host => ""}
end
public_suffix = parse_public_suffix_from_host(host)
parts = host.gsub(public_suffix, '').split(".")
domain = parts.pop
subdomains = parts.join(".")
{:public_suffix => public_suffix, :domain => domain, :subdomain => subdomains, :host => host}
end

def parse_public_suffix_from_host(host)
parts = host.split(".").reverse
public_suffix = []
domain = ""
subdomains = []
sub_hash = @public_suffixes
parts.each_index do |i|
part = parts[i]
public_suffix = []

parts.each_with_index do |part, index|
sub_parts = sub_hash[part]
sub_hash = sub_parts
return "" unless sub_parts
public_suffix << part
next_part = parts[index + 1]
if sub_parts.has_key? "*"
public_suffix << part
public_suffix << parts[i+1]
domain = parts[i+2]
subdomains = parts.slice(i+3, parts.size)
public_suffix << next_part
break
elsif sub_parts.empty? || !sub_parts.has_key?(parts[i+1])
public_suffix << part
domain = parts[i+1]
subdomains = parts.slice(i+2, parts.size)
elsif sub_parts.empty? || !sub_parts.has_key?(next_part)
break
else
public_suffix << part
end
end
{:public_suffix => public_suffix.reverse.join("."), :domain => domain, :subdomain => subdomains.reverse.join(".")}

public_suffix.reverse.join(".")
end
end
end
end
4 changes: 4 additions & 0 deletions lib/domainatrix/url.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def initialize(attrs = {})
end

def canonical(options = {})
return nil unless valid?
public_suffix_parts = @public_suffix.split(".")
url = "#{public_suffix_parts.reverse.join(".")}.#{@domain}"
if @subdomain && [email protected]?
Expand All @@ -29,5 +30,8 @@ def domain_with_public_suffix
end
alias domain_with_tld domain_with_public_suffix

def valid?
@public_suffix and !@public_suffix.empty?
end
end
end
Loading