Skip to content

Commit

Permalink
Merge pull request #1 from menno/ips
Browse files Browse the repository at this point in the history
Add support for parsing IP addresses
  • Loading branch information
marceldegraaf committed Aug 16, 2012
2 parents 913c22e + 40e53e8 commit 20def50
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.rvmrc
5 changes: 5 additions & 0 deletions lib/domainatrix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
require 'domainatrix/domain_parser.rb'
require 'domainatrix/url.rb'

begin
require 'uri'
rescue LoadError
end

module Domainatrix
VERSION = "0.0.8"

Expand Down
13 changes: 11 additions & 2 deletions lib/domainatrix/domain_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,13 @@ def split_domain(parts, tld_size)

def parse_domains_from_host(host)
parts = host.split(".").reverse
ip_address = false

if host == '*'
tld_size = 0
elsif !parts.map { |part| part.match(/\d+/) }.include?(nil)
# host is an ip address
ip_address = true
else
main_tld = parts.first

Expand Down Expand Up @@ -112,8 +116,13 @@ def parse_domains_from_host(host)
end
end

subdomain, domain, tld = split_domain(parts, tld_size)
{:public_suffix => tld, :domain => domain, :subdomain => subdomain}
if ip_address
subdomain, domain, tld = '', host, ''
else
subdomain, domain, tld = split_domain(parts, tld_size)
end

{:public_suffix => tld, :domain => domain, :subdomain => subdomain, :ip_address => ip_address}
end
end
end
3 changes: 2 additions & 1 deletion lib/domainatrix/url.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Domainatrix
class Url
attr_accessor :public_suffix, :domain, :subdomain, :path, :url, :scheme, :host
attr_accessor :public_suffix, :domain, :subdomain, :path, :url, :scheme, :host, :ip_address

def initialize(attrs = {})
@scheme = attrs[:scheme]
Expand All @@ -10,6 +10,7 @@ def initialize(attrs = {})
@domain = attrs[:domain]
@subdomain = attrs[:subdomain]
@path = attrs[:path]
@ip_address = attrs[:ip_address]
end

def canonical(options = {})
Expand Down
13 changes: 13 additions & 0 deletions spec/domainatrix/domain_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,18 @@
lambda { @domain_parser.parse("http:/") }.should raise_error(Domainatrix::ParseError)
end

it "parses an ip address" do
@domain_parser.parse("http://123.123.123.123/foo/bar")[:domain].should == "123.123.123.123"
@domain_parser.parse("http://123.123.123.123/foo/bar")[:path].should == "/foo/bar"
@domain_parser.parse("http://123.123.123.123/foo/bar")[:ip_address].should == true
end

it "parses a host with numeric domain" do
@domain_parser.parse("http://123.123.123.co.uk/foo/bar")[:subdomain].should == "123.123"
@domain_parser.parse("http://123.123.123.co.uk/foo/bar")[:domain].should == "123"
@domain_parser.parse("http://123.123.123.co.uk/foo/bar")[:public_suffix].should == "co.uk"
@domain_parser.parse("http://123.123.123.co.uk/foo/bar")[:ip_address].should == false
end

end
end
4 changes: 4 additions & 0 deletions spec/domainatrix/url_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
Domainatrix::Url.new(:path => "/asdf.html").path.should == "/asdf.html"
end

it "reports if it is an ip address" do
Domainatrix::Url.new(:ip_address => true).ip_address.should == true
end

it "canonicalizes the url" do
Domainatrix::Url.new(:domain => "pauldix", :public_suffix => "net").canonical.should == "net.pauldix"
Domainatrix::Url.new(:subdomain => "foo", :domain => "pauldix", :public_suffix => "net").canonical.should == "net.pauldix.foo"
Expand Down
12 changes: 10 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
require "rubygems"
require "spec"

begin
require "spec"
rescue LoadError
end

# gem install redgreen for colored test output
begin require "redgreen" unless ENV['TM_CURRENT_LINE']; rescue LoadError; end

path = File.expand_path(File.dirname(__FILE__) + "/../lib/")
$LOAD_PATH.unshift(path) unless $LOAD_PATH.include?(path)

require "lib/domainatrix"
begin
require "lib/domainatrix"
rescue LoadError
require 'domainatrix'
end

0 comments on commit 20def50

Please sign in to comment.