domain_name_validator 0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/ChangeLog.md ADDED
@@ -0,0 +1,15 @@
1
+ 0.2 (2013-06-24)
2
+ ----------------
3
+
4
+ First version built and deployed as a gem on a real project, for general
5
+ evaluation and testing purposes.
6
+
7
+ * Added more RSpec tests for improved test coverage.
8
+ * Enhanced documentation.
9
+
10
+
11
+ 0.1 (2013-06-18)
12
+ ----------------
13
+
14
+ Initial working version of the code, with minimal RSpec tests. Released on
15
+ GitHub for review, but not yet published as a gem to the Ruby community.
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,24 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ domain_name_validator (0.1)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.2.4)
10
+ rspec (2.13.0)
11
+ rspec-core (~> 2.13.0)
12
+ rspec-expectations (~> 2.13.0)
13
+ rspec-mocks (~> 2.13.0)
14
+ rspec-core (2.13.1)
15
+ rspec-expectations (2.13.0)
16
+ diff-lcs (>= 1.1.3, < 2.0)
17
+ rspec-mocks (2.13.1)
18
+
19
+ PLATFORMS
20
+ x86-mingw32
21
+
22
+ DEPENDENCIES
23
+ domain_name_validator!
24
+ rspec
data/LICENSE.txt ADDED
@@ -0,0 +1,23 @@
1
+ Copyright (c) 2013 David Keener
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+
data/README.md ADDED
@@ -0,0 +1,66 @@
1
+ domain_name_validator
2
+ =====================
3
+
4
+ Ever needed to validate a domain name? This gem will validate any domain name
5
+ represented in ASCII or UTF-8.
6
+
7
+ Domain names provide a unique, memorizable name to represent numerically
8
+ addressable Internet resources. They also provide a level of abstraction that
9
+ allows the underlying Internet address to be changed while still referencing
10
+ a resource by its domain name. The domain name space is managed by the
11
+ Internet Corporation for Assigned Names and Numbers (ICANN).
12
+
13
+ The right-most label of a domain name is referred to as the top-level domain, or
14
+ TLD. A limited set of top-level domain names, and two-character country codes,
15
+ have been standardized. The Internet Assigned Numbers Authority (IANA)
16
+ maintains an annotated list of top-level domains, as well as a list of
17
+ "special use," or reserved, top-level domain names.
18
+
19
+ * http://www.iana.org/domains/root/db/
20
+ * http://www.iana.org/assignments/special-use-domain-names/special-use-domain-names.xml
21
+
22
+ Domain names follow some very detailed rules:
23
+
24
+ 1. The maximum length of a domain name is 253 characters.
25
+
26
+ 2. A domain name is divided into "labels" separated by periods. The maximum
27
+ number of labels is 127.
28
+
29
+ 3. The maximum length of any label within a domain name is 63 characters.
30
+
31
+ 4. No label, including TLDs, can begin or end with a dash.
32
+
33
+ 5. Top-level domain names cannot be all numeric.
34
+
35
+ 6. The right-most label must be either a recognized TLD or a 2-letter country
36
+ code. The only exception is for international domain names, for which
37
+ TLD checking is currently bypassed.
38
+
39
+
40
+ Internationalized Domain Names
41
+ ------------------------------
42
+
43
+ What about internationalized domain names? ICANN approved the Internationalized
44
+ Domain Name (IDNA) system in 2003. This standard allows for Unicode domain
45
+ names to be encoded into ASCII using Punycode. Essentially, a label may contain
46
+ "xn--" as a prefix, followed by the Punycode representation of a Unicode string,
47
+ resulting in domain names such as xn--kbenhavn-54.eu. Note that there are also
48
+ some approved Unicode TLDs.
49
+
50
+ The process of rendering an internationalized domain name in ASCII via
51
+ Punycode is called normalization. This gem will validate a normalized domain
52
+ name, but not a Unicode domain name. Note, however, that it currently does not
53
+ validate normalized TLDs against ICANN's list of valid TLDs.
54
+
55
+ It's also unclear whether the "xn--" prefix should count against the label
56
+ size limit of 63 characters. In the absence of specific guidelines, and because
57
+ I've never actually seen an overly long label, I have chosen to apply the limit
58
+ irregardless of the presence of the "xn--" prefix within a label.
59
+
60
+ YOUR SUPPORT
61
+ ------------
62
+
63
+ Please help me make this gem as useful as possible for its admittedly limited
64
+ purpose. If ICANN's rules change, a clearer rules interpretation becomes
65
+ available, or new valid TLDs are added, please help out by notifying me of
66
+ potentially useful changes, or by submitting a pull request via GitHub.
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+ # or....
4
+ # require 'bundler/gem_tasks'
@@ -0,0 +1,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "domain_name_validator/version"
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.name = "domain_name_validator"
7
+ gem.version = DomainNameValidator::VERSION
8
+ gem.date = '2013-06-17'
9
+ gem.platform = Gem::Platform::RUBY
10
+ gem.authors = ["David Keener"]
11
+ gem.email = ["dkeener@keenertech.com"]
12
+ gem.homepage = "http://www.keenertech.com"
13
+ gem.summary = %q{Domain Name Validator}
14
+ gem.description = %q{Checks the validity of domain names.}
15
+
16
+ gem.add_development_dependency "rspec"
17
+
18
+ gem.rubyforge_project = "domain_name_validator"
19
+
20
+ gem.files = `git ls-files`.split("\n")
21
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
22
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
23
+
24
+ #gem.rdoc_options = ["--charset=UTF-8"]
25
+ #gem.extra_rdoc_files = %w[README.rdoc LICENSE Changelog.rdoc]
26
+
27
+ gem.require_paths = ["lib"]
28
+ end
@@ -0,0 +1,49 @@
1
+ # The purpose of this class is to provide a simple capability for validating
2
+ # domain names represented in ASCII or UTF-8, a feature that seems to be
3
+ # missing from other more wide-ranging domain-related gems.
4
+
5
+ class DomainNameValidator
6
+
7
+ MAX_DOMAIN_LENGTH = 253
8
+ MAX_LABEL_LENGTH = 63
9
+ MAX_LEVELS = 127
10
+ MIN_LEVELS = 2
11
+
12
+ ERR_MAX_DOMAIN_SIZE = 'Maximum domain length of 253 exceeded'
13
+ ERR_MAX_LABEL_SIZE = 'Maximum domain label length of 63 exceeded'
14
+ ERR_MAX_LEVEL_SIZE = 'Maximum domain level limit of 127 exceeded'
15
+ ERR_MIN_LEVEL_SIZE = 'Minimum domain level limit of 2 not achieved'
16
+ ERR_LABEL_DASH_BEGIN = 'No domain label may begin with a dash'
17
+ ERR_LABEL_DASH_END = 'No domain label may end with a dash'
18
+ ERR_TOP_NUMERICAL = 'The top-level domain (the extension) cannot be numerical'
19
+ ERR_ILLEGAL_CHARS = 'Domain label contains an illegal character'
20
+
21
+ # Validates the proper formatting of a normalized domain name, i.e. - a
22
+ # domain that is represented in ASCII. Thus, international domain names are
23
+ # supported and validated, if they have undergone the required IDN
24
+ # conversion to ASCII. The validation rules are:
25
+ #
26
+ # 1. The maximum length of a domain name is 253 characters.
27
+ # 2. A domain name is divided into "labels" separated by periods. The maximum
28
+ # number of labels (including the top-level domain as a label) is 127.
29
+ # 3. The maximum length of any label within a domain name is 63 characters.
30
+ # 4. No label, including top-level domains, can begin or end with a dash.
31
+ # 5. Top-level names cannot be all numeric.
32
+
33
+ def validate(dn, errs = [])
34
+ errs << ERR_MAX_DOMAIN_SIZE if dn.size > MAX_DOMAIN_LENGTH
35
+ parts = dn.split('.')
36
+ errs << ERR_MAX_LEVEL_SIZE if parts.size > MAX_LEVELS
37
+ errs << ERR_MIN_LEVEL_SIZE if parts.size < MIN_LEVELS
38
+ parts.each do |p|
39
+ errs << ERR_MAX_LABEL_SIZE if p.size > MAX_LABEL_LENGTH
40
+ errs << ERR_LABEL_DASH_BEGIN if p[0] == '-'
41
+ errs << ERR_LABEL_DASH_END if p[-1] == '-'
42
+ errs << ERR_ILLEGAL_CHARS unless p.match(/^[a-z0-9\-\_]+$/)
43
+ end
44
+ errs << ERR_TOP_NUMERICAL if parts.last.match(/^[0-9]+$/)
45
+
46
+ errs.size == 0 # TRUE if valid, FALSE otherwise
47
+ end
48
+
49
+ end
@@ -0,0 +1,3 @@
1
+ class DomainNameValidator
2
+ VERSION = "0.2"
3
+ end
@@ -0,0 +1,5 @@
1
+ require 'domain_name_validator/validator'
2
+ require 'domain_name_validator/version'
3
+
4
+ class DomainNameValidator
5
+ end
@@ -0,0 +1,67 @@
1
+ require 'spec_helper'
2
+
3
+ describe DomainNameValidator do
4
+
5
+ before(:each) do
6
+ @validator = DomainNameValidator.new
7
+ end
8
+
9
+ describe 'Basic Validation' do
10
+
11
+ it 'should pass a valid domain name' do
12
+ response = @validator.validate('keenertech.com')
13
+ response.should be == true
14
+ end
15
+
16
+ it 'should fail when it finds a numeric top-level extension' do
17
+ response = @validator.validate('keenertech.123')
18
+ response.should be == false
19
+ end
20
+
21
+ it 'should fail when the domain name max size is exceeded' do
22
+ domain = "a"*250 + ".com" # 254 chars; max is 253
23
+ response = @validator.validate(domain)
24
+ response.should be == false
25
+ end
26
+
27
+ it 'should fail when the max number of levels is exceeded' do
28
+ domain = "a."*127 + "com" # 128 levels; max is 127
29
+ response = @validator.validate(domain)
30
+ response.should be == false
31
+ end
32
+
33
+ it 'should fail when a label exceeds the max label length' do
34
+ domain = "a"*64 + "b.c.com" # 64 chars; max is 63 for a label
35
+ response = @validator.validate(domain)
36
+ response.should be == false
37
+ end
38
+
39
+ it 'should fail when a label begins with a dash' do
40
+ domain = "a.-b.c.com"
41
+ response = @validator.validate(domain)
42
+ response.should be == false
43
+ end
44
+
45
+ it 'should fail when a TLD begins with a dash' do
46
+ domain = "a.b.c.-com"
47
+ response = @validator.validate(domain)
48
+ response.should be == false
49
+ end
50
+ end
51
+
52
+ describe 'Internationalized (normalized) domain names' do
53
+
54
+ it 'should fail when a TLD begins with a dash' do
55
+ domain = "xn--kbenhavn-54.eu"
56
+ response = @validator.validate(domain)
57
+ response.should be == true
58
+ end
59
+
60
+ end
61
+
62
+ # TODO: (2013/06/24) TLD checking will be added soon....
63
+
64
+ describe 'TLD Checking' do
65
+ end
66
+
67
+ end
@@ -0,0 +1,13 @@
1
+ require File.expand_path(
2
+ File.join(File.dirname(__FILE__), %w[.. lib domain_name_validator]))
3
+
4
+ RSpec.configure do |config|
5
+ # == Mock Framework
6
+ #
7
+ # RSpec uses it's own mocking framework by default. If you prefer to
8
+ # use mocha, flexmock or RR, uncomment the appropriate line:
9
+ #
10
+ # config.mock_with :mocha
11
+ # config.mock_with :flexmock
12
+ # config.mock_with :rr
13
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: domain_name_validator
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.2'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - David Keener
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-06-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &30848232 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *30848232
25
+ description: Checks the validity of domain names.
26
+ email:
27
+ - dkeener@keenertech.com
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - ChangeLog.md
33
+ - Gemfile
34
+ - Gemfile.lock
35
+ - LICENSE.txt
36
+ - README.md
37
+ - Rakefile
38
+ - domain_name_validator.gemspec
39
+ - lib/domain_name_validator.rb
40
+ - lib/domain_name_validator/validator.rb
41
+ - lib/domain_name_validator/version.rb
42
+ - spec/domain_name_validator_spec.rb
43
+ - spec/spec_helper.rb
44
+ homepage: http://www.keenertech.com
45
+ licenses: []
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubyforge_project: domain_name_validator
64
+ rubygems_version: 1.8.16
65
+ signing_key:
66
+ specification_version: 3
67
+ summary: Domain Name Validator
68
+ test_files: []