validates_host 0.1.0 → 0.2.0
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/lib/validates_host/domain_name_validator.rb +14 -11
- data/lib/validates_host/host_name_validator.rb +14 -11
- data/lib/validates_host/ip_validator.rb +14 -11
- data/lib/validates_host/version.rb +1 -1
- data/spec/validates_host.rb/domain_name_validator_spec.rb +2 -0
- data/spec/validates_host.rb/host_name_validator_spec.rb +2 -0
- data/spec/validates_host.rb/ip_validator_spec.rb +2 -0
- metadata +8 -2
@@ -1,20 +1,23 @@
|
|
1
1
|
class DomainNameValidator < ActiveModel::EachValidator
|
2
2
|
def validate_each(record, attribute, value)
|
3
|
-
|
3
|
+
key = :"activerecord.errors.models.#{record.class.name.downcase}.attributes.#{attribute.to_s}.invalid"
|
4
|
+
record.errors[attribute] << I18n.t(key, :default => :"activerecord.errors.messages.invalid") unless ValidatesHost::DomainName.new(value).valid?
|
4
5
|
end
|
5
6
|
end
|
6
7
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
8
|
+
module ValidatesHost
|
9
|
+
class DomainName
|
10
|
+
def initialize(domain_name)
|
11
|
+
@domain_name = domain_name
|
12
|
+
end
|
11
13
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
14
|
+
def valid?
|
15
|
+
return true if @domain_name.nil?
|
16
|
+
@domain_name =~ /^[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/
|
17
|
+
end
|
16
18
|
|
17
|
-
|
18
|
-
|
19
|
+
def domain_name
|
20
|
+
@domain_name
|
21
|
+
end
|
19
22
|
end
|
20
23
|
end
|
@@ -1,20 +1,23 @@
|
|
1
1
|
class HostNameValidator < ActiveModel::EachValidator
|
2
2
|
def validate_each(record, attribute, value)
|
3
|
-
|
3
|
+
key = :"activerecord.errors.models.#{record.class.name.downcase}.attributes.#{attribute.to_s}.invalid"
|
4
|
+
record.errors[attribute] << I18n.t(key, :default => :"activerecord.errors.messages.invalid") unless ValidatesHost::HostName.new(value).valid?
|
4
5
|
end
|
5
6
|
end
|
6
7
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
8
|
+
module ValidatesHost
|
9
|
+
class HostName
|
10
|
+
def initialize(host_name)
|
11
|
+
@host_name = host_name
|
12
|
+
end
|
11
13
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
14
|
+
def valid?
|
15
|
+
return true if @host_name.blank?
|
16
|
+
@host_name =~ /^[a-z][a-z0-9-]+$/
|
17
|
+
end
|
16
18
|
|
17
|
-
|
18
|
-
|
19
|
+
def host_name
|
20
|
+
@host_name
|
21
|
+
end
|
19
22
|
end
|
20
23
|
end
|
@@ -1,20 +1,23 @@
|
|
1
1
|
class IpValidator < ActiveModel::EachValidator
|
2
2
|
def validate_each(record, attribute, value)
|
3
|
-
|
3
|
+
key = :"activerecord.errors.models.#{record.class.name.downcase}.attributes.#{attribute.to_s}.invalid"
|
4
|
+
record.errors[attribute] << I18n.t(key, :default => :"activerecord.errors.messages.invalid") unless ValidatesHost::Ip.new(value).valid?
|
4
5
|
end
|
5
6
|
end
|
6
7
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
8
|
+
module ValidatesHost
|
9
|
+
class Ip
|
10
|
+
def initialize(ip)
|
11
|
+
@ip = ip
|
12
|
+
end
|
11
13
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
14
|
+
def valid?
|
15
|
+
return true if @ip.blank?
|
16
|
+
@ip =~ /^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$/
|
17
|
+
end
|
16
18
|
|
17
|
-
|
18
|
-
|
19
|
+
def ip
|
20
|
+
@ip
|
21
|
+
end
|
19
22
|
end
|
20
23
|
end
|
@@ -4,6 +4,8 @@ describe DomainNameValidator do
|
|
4
4
|
context "when domain_name is invalid" do
|
5
5
|
before :each do
|
6
6
|
@server = Server.new(:domain_name => "http://")
|
7
|
+
I18n.stub(:t).with(:"activerecord.errors.models.server.attributes.domain_name.invalid",
|
8
|
+
:default => :"activerecord.errors.messages.invalid").and_return("is invalid")
|
7
9
|
end
|
8
10
|
|
9
11
|
it "should set object as invalid" do
|
@@ -4,6 +4,8 @@ describe HostNameValidator do
|
|
4
4
|
context "when host_name is invalid" do
|
5
5
|
before :each do
|
6
6
|
@server = Server.new(:host_name => "http://")
|
7
|
+
I18n.stub(:t).with(:"activerecord.errors.models.server.attributes.host_name.invalid",
|
8
|
+
:default => :"activerecord.errors.messages.invalid").and_return("is invalid")
|
7
9
|
end
|
8
10
|
|
9
11
|
it "should set object as invalid" do
|
@@ -4,6 +4,8 @@ describe IpValidator do
|
|
4
4
|
context "when ip is invalid" do
|
5
5
|
before :each do
|
6
6
|
@server = Server.new(:ip => "127.0.0")
|
7
|
+
I18n.stub(:t).with(:"activerecord.errors.models.server.attributes.ip.invalid",
|
8
|
+
:default => :"activerecord.errors.messages.invalid").and_return("is invalid")
|
7
9
|
end
|
8
10
|
|
9
11
|
it "should set object as invalid" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: validates_host
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-08-06 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
@@ -158,12 +158,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
158
158
|
- - ! '>='
|
159
159
|
- !ruby/object:Gem::Version
|
160
160
|
version: '0'
|
161
|
+
segments:
|
162
|
+
- 0
|
163
|
+
hash: -391399657668278307
|
161
164
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
162
165
|
none: false
|
163
166
|
requirements:
|
164
167
|
- - ! '>='
|
165
168
|
- !ruby/object:Gem::Version
|
166
169
|
version: '0'
|
170
|
+
segments:
|
171
|
+
- 0
|
172
|
+
hash: -391399657668278307
|
167
173
|
requirements: []
|
168
174
|
rubyforge_project:
|
169
175
|
rubygems_version: 1.8.24
|