domain_validator 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -3,6 +3,7 @@
3
3
  .bundle
4
4
  .config
5
5
  .yardoc
6
+ .rspec
6
7
  Gemfile.lock
7
8
  InstalledFiles
8
9
  _yardoc
data/.travis.yml CHANGED
@@ -4,6 +4,5 @@ rvm:
4
4
  - jruby-18mode # JRuby in 1.8 mode
5
5
  - jruby-19mode # JRuby in 1.9 mode
6
6
  - rbx-18mode
7
- - rbx-19mode
8
7
  - 1.8.7
9
8
  - 2.0.0
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # DomainValidator
2
2
 
3
3
  [![Build Status](https://travis-ci.org/kdayton-/domain_validator.png?branch=master)](https://travis-ci.org/kdayton-/domain_validator)
4
+ [![Code Climate](https://codeclimate.com/github/kdayton-/domain_validator.png)](https://codeclimate.com/github/kdayton-/domain_validator)
4
5
 
5
6
  Adds a DomainValidator to ActiveModel, allowing for easy validation of FQDNs
6
7
 
@@ -25,10 +26,23 @@ Add a domain validation to your ActiveModel enabled class
25
26
 
26
27
  ```ruby
27
28
  class User < ActiveRecord::Base
29
+ attr_accessible :domain
28
30
  validates :domain, :domain => true
29
31
  end
30
32
  ```
31
33
 
34
+ DomainValidator can also perform a DNS check using ruby's built-in Resolv library
35
+
36
+ ```ruby
37
+ class User < ActiveRecord::Base
38
+ attr_accessible :domain
39
+ validates :domain, :domain => {:verify_dns => true}
40
+
41
+ # Also supports a custom message when failing DNS check
42
+ # validates :domain, :domain => {:verify_dns => {:message => "DNS check failed"}}
43
+ end
44
+ ```
45
+
32
46
  ## Examples
33
47
 
34
48
  ```ruby
@@ -39,6 +53,14 @@ user.domain = 'invalid*characters.com'
39
53
  user.valid? # => false
40
54
  ```
41
55
 
56
+ ## Compatibility
57
+
58
+ DomainValidator is tested against:
59
+
60
+ MRI 1.8.7, 1.9.3, 2.0.0
61
+ JRuby 1.8, 1.9
62
+ Rubinus 1.8
63
+
42
64
  ## Contributing
43
65
 
44
66
  1. Fork it
data/Rakefile CHANGED
@@ -1,5 +1,5 @@
1
1
  require "bundler/gem_tasks"
2
- require 'rspec/core/rake_task'
2
+ require "rspec/core/rake_task"
3
3
 
4
4
  RSpec::Core::RakeTask.new(:spec)
5
5
 
@@ -1,7 +1,7 @@
1
1
  # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
2
+ lib = File.expand_path("../lib", __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'domain_validator/version'
4
+ require "domain_validator/version"
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "domain_validator"
@@ -1 +1,2 @@
1
- require 'domain_validator/validator'
1
+ require "domain_validator/dns_check"
2
+ require "domain_validator/validator"
@@ -0,0 +1,16 @@
1
+ require "resolv"
2
+
3
+ module DomainValidator
4
+ class DnsCheck
5
+
6
+ def self.has_record?(domain)
7
+ begin
8
+ Resolv::DNS.new.getaddress domain
9
+ return true
10
+ rescue Resolv::ResolvError => e
11
+ return false
12
+ end
13
+ end
14
+
15
+ end
16
+ end
@@ -1,5 +1,5 @@
1
- require 'active_model'
2
- require 'active_model/validator'
1
+ require "active_model"
2
+ require "active_model/validator"
3
3
 
4
4
  module DomainValidator
5
5
  class Validator < ActiveModel::EachValidator
@@ -7,14 +7,35 @@ module DomainValidator
7
7
  RE_DOMAIN = %r(^(?=.{1,255}$)[0-9A-Za-z](?:(?:[0-9A-Za-z]|-){0,61}[0-9A-Za-z])?(?:\.[0-9A-Za-z](?:(?:[0-9A-Za-z]|-){0,61}[0-9A-Za-z])?)+$)
8
8
 
9
9
  def validate_each(record, attr_name, value)
10
+ validate_domain_format(record, attr_name, value)
11
+ validate_domain_dns(record, attr_name, value) if options[:verify_dns]
12
+ end
13
+
14
+ def validate_domain_format(record, attr_name, value)
10
15
  valid_domain = is_valid_domain?(value)
11
- record.errors.add(attr_name, options[:message] || "is invalid") and return unless valid_domain
16
+ record.errors.add(attr_name, options[:message] || "is invalid") unless valid_domain
17
+ end
18
+
19
+ def validate_domain_dns(record, attr_name, value)
20
+ valid_domain = has_dns_record?(value)
21
+ record.errors.add(attr_name, verify_dns_message || "does not have a DNS record") unless valid_domain
12
22
  end
13
23
 
14
24
  def is_valid_domain?(domain)
15
25
  domain =~ RE_DOMAIN
16
26
  end
17
27
 
28
+ def has_dns_record?(domain)
29
+ options[:verify_dns] ? DnsCheck.has_record?(domain) : true
30
+ end
31
+
32
+ def verify_dns_message
33
+ verify_dns = options[:verify_dns]
34
+ if verify_dns.is_a? Hash
35
+ verify_dns[:message] || options[:message]
36
+ end
37
+ end
38
+
18
39
  end
19
40
  end
20
41
 
@@ -1,3 +1,3 @@
1
1
  module DomainValidator
2
- VERSION = [0, 0, 2].join "."
2
+ VERSION = [0, 1, 0].join "."
3
3
  end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ module DomainValidator
4
+ describe DnsCheck do
5
+ describe '#has_record?' do
6
+
7
+ context "given a domain without a DNS record" do
8
+ it "should return false" do
9
+ DnsCheck.has_record?("a.com").should be_false
10
+ end
11
+ end
12
+
13
+ context "given a domain with a DNS record" do
14
+ it "should return true" do
15
+ DnsCheck.has_record?("example.com").should be_true
16
+ end
17
+ end
18
+
19
+ end
20
+ end
21
+ end
@@ -1,28 +1,5 @@
1
1
  require 'spec_helper'
2
-
3
- class User < Model
4
- validates :domain, :domain => true
5
- end
6
-
7
- class UserAllowsNil < Model
8
- validates :domain, :domain => {:allow_nil => true}
9
- end
10
-
11
- class UserAllowsNilFalse < Model
12
- validates :domain, :domain => {:allow_nil => false}
13
- end
14
-
15
- class UserAllowsBlank < Model
16
- validates :domain, :domain => {:allow_blank => true}
17
- end
18
-
19
- class UserAllowsBlankFalse < Model
20
- validates :domain, :domain => {:allow_blank => false}
21
- end
22
-
23
- class UserWithMessage < Model
24
- validates :domain, :domain => {:message => "isn't quite right"}
25
- end
2
+ require 'support/models'
26
3
 
27
4
  describe DomainValidator do
28
5
  context "with valid domain" do
@@ -87,6 +64,54 @@ describe DomainValidator do
87
64
  subject.errors[:domain].should include "isn't quite right"
88
65
  end
89
66
  end
67
+
68
+ context "when :verify_dns has a :message option" do
69
+ subject { UserVerifyDNSMessage.new :domain => "a.com" }
70
+ before { subject.valid? }
71
+
72
+ it "should add the customized message" do
73
+ subject.errors[:domain].should include "failed DNS check"
74
+ end
75
+ end
76
+
77
+ context "when :verify_dns does not have a :message option" do
78
+ subject { UserVerifyDNS.new :domain => "a.com" }
79
+ before { subject.valid? }
80
+
81
+ it "should add the default message" do
82
+ subject.errors[:domain].should include "does not have a DNS record"
83
+ end
84
+ end
85
+ end
86
+
87
+ describe "DNS check" do
88
+ describe "a domain with a DNS record" do
89
+ it "should be valid when :verify_dns is true" do
90
+ UserVerifyDNS.new(:domain => "example.com").should be_valid
91
+ end
92
+
93
+ it "should be valid when :verify_dns is false" do
94
+ UserVerifyDNSFalse.new(:domain => "example.com").should be_valid
95
+ end
96
+
97
+ it "should be valid when :verify_dns is undefined" do
98
+ User.new(:domain => "example.com").should be_valid
99
+ end
100
+ end
101
+
102
+ describe "a domain without a DNS record" do
103
+ it "should not be valid when :verify_dns is true" do
104
+ UserVerifyDNS.new(:domain => "a.com").should_not be_valid
105
+ end
106
+
107
+ it "should be valid when :verify_dns is false" do
108
+ UserVerifyDNSFalse.new(:domain => "a.com").should be_valid
109
+ end
110
+
111
+ it "should be valid when :verify_dns is undefined" do
112
+ User.new(:domain => "a.com").should be_valid
113
+ end
114
+ end
90
115
  end
91
116
 
92
117
  end
@@ -0,0 +1,35 @@
1
+ class User < Model
2
+ validates :domain, :domain => true
3
+ end
4
+
5
+ class UserAllowsNil < Model
6
+ validates :domain, :domain => {:allow_nil => true}
7
+ end
8
+
9
+ class UserAllowsNilFalse < Model
10
+ validates :domain, :domain => {:allow_nil => false}
11
+ end
12
+
13
+ class UserAllowsBlank < Model
14
+ validates :domain, :domain => {:allow_blank => true}
15
+ end
16
+
17
+ class UserAllowsBlankFalse < Model
18
+ validates :domain, :domain => {:allow_blank => false}
19
+ end
20
+
21
+ class UserWithMessage < Model
22
+ validates :domain, :domain => {:message => "isn't quite right"}
23
+ end
24
+
25
+ class UserVerifyDNS < Model
26
+ validates :domain, :domain => {:verify_dns => true}
27
+ end
28
+
29
+ class UserVerifyDNSFalse < Model
30
+ validates :domain, :domain => {:verify_dns => false}
31
+ end
32
+
33
+ class UserVerifyDNSMessage < Model
34
+ validates :domain, :domain => {:verify_dns => {:message => "failed DNS check"}}
35
+ end
metadata CHANGED
@@ -1,69 +1,78 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: domain_validator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
+ prerelease:
5
6
  platform: ruby
6
7
  authors:
7
8
  - Kyle Dayton
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2013-06-08 00:00:00.000000000 Z
12
+ date: 2013-06-11 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: activemodel
15
16
  requirement: !ruby/object:Gem::Requirement
17
+ none: false
16
18
  requirements:
17
- - - '>='
19
+ - - ! '>='
18
20
  - !ruby/object:Gem::Version
19
21
  version: '0'
20
22
  type: :runtime
21
23
  prerelease: false
22
24
  version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
23
26
  requirements:
24
- - - '>='
27
+ - - ! '>='
25
28
  - !ruby/object:Gem::Version
26
29
  version: '0'
27
30
  - !ruby/object:Gem::Dependency
28
31
  name: bundler
29
32
  requirement: !ruby/object:Gem::Requirement
33
+ none: false
30
34
  requirements:
31
- - - '>='
35
+ - - ! '>='
32
36
  - !ruby/object:Gem::Version
33
37
  version: '0'
34
38
  type: :development
35
39
  prerelease: false
36
40
  version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
37
42
  requirements:
38
- - - '>='
43
+ - - ! '>='
39
44
  - !ruby/object:Gem::Version
40
45
  version: '0'
41
46
  - !ruby/object:Gem::Dependency
42
47
  name: rake
43
48
  requirement: !ruby/object:Gem::Requirement
49
+ none: false
44
50
  requirements:
45
- - - '>='
51
+ - - ! '>='
46
52
  - !ruby/object:Gem::Version
47
53
  version: '0'
48
54
  type: :development
49
55
  prerelease: false
50
56
  version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
51
58
  requirements:
52
- - - '>='
59
+ - - ! '>='
53
60
  - !ruby/object:Gem::Version
54
61
  version: '0'
55
62
  - !ruby/object:Gem::Dependency
56
63
  name: rspec
57
64
  requirement: !ruby/object:Gem::Requirement
65
+ none: false
58
66
  requirements:
59
- - - '>='
67
+ - - ! '>='
60
68
  - !ruby/object:Gem::Version
61
69
  version: '0'
62
70
  type: :development
63
71
  prerelease: false
64
72
  version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
65
74
  requirements:
66
- - - '>='
75
+ - - ! '>='
67
76
  - !ruby/object:Gem::Version
68
77
  version: '0'
69
78
  description: Adds ActiveModel validation for domain format.
@@ -81,36 +90,43 @@ files:
81
90
  - Rakefile
82
91
  - domain_validator.gemspec
83
92
  - lib/domain_validator.rb
93
+ - lib/domain_validator/dns_check.rb
84
94
  - lib/domain_validator/validator.rb
85
95
  - lib/domain_validator/version.rb
96
+ - spec/dns_check_spec.rb
86
97
  - spec/domain_validator_spec.rb
87
98
  - spec/spec_helper.rb
88
99
  - spec/support/domain_helpers.rb
100
+ - spec/support/models.rb
89
101
  homepage: http://github.com/kdayton-/domain_validator
90
102
  licenses:
91
103
  - MIT
92
- metadata: {}
93
104
  post_install_message:
94
105
  rdoc_options: []
95
106
  require_paths:
96
107
  - lib
97
108
  required_ruby_version: !ruby/object:Gem::Requirement
109
+ none: false
98
110
  requirements:
99
- - - '>='
111
+ - - ! '>='
100
112
  - !ruby/object:Gem::Version
101
113
  version: '0'
102
114
  required_rubygems_version: !ruby/object:Gem::Requirement
115
+ none: false
103
116
  requirements:
104
- - - '>='
117
+ - - ! '>='
105
118
  - !ruby/object:Gem::Version
106
119
  version: '0'
107
120
  requirements: []
108
121
  rubyforge_project:
109
- rubygems_version: 2.0.3
122
+ rubygems_version: 1.8.25
110
123
  signing_key:
111
- specification_version: 4
124
+ specification_version: 3
112
125
  summary: Adds ActiveModel validation for domain format.
113
126
  test_files:
127
+ - spec/dns_check_spec.rb
114
128
  - spec/domain_validator_spec.rb
115
129
  - spec/spec_helper.rb
116
130
  - spec/support/domain_helpers.rb
131
+ - spec/support/models.rb
132
+ has_rdoc:
checksums.yaml DELETED
@@ -1,7 +0,0 @@
1
- ---
2
- SHA1:
3
- metadata.gz: a951e441fa4015ac815c9ba5fee96609c22fc8a4
4
- data.tar.gz: ae1724471b31badf6553fd783e5da8c5e905a17b
5
- SHA512:
6
- metadata.gz: a82193dea64c492105c0b64e847f02f8214f8b2a82a0c7048a1c29b4fe5501280ed60aea2d329e23898b875f1c93e1d5be84947d13176e5b1856ba563d61a502
7
- data.tar.gz: 082544a780890525d87962ec301888a8842beb0d7696e6176ad26254142c0d34b1ecb1b08a66fc899b8d2e3085036c87f4c2f956d1f08411cf95d13c88104b13