validates_host 0.3.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +5 -4
  3. data/.rubocop.yml +57 -0
  4. data/.ruby-gemset +1 -0
  5. data/.travis.yml +45 -4
  6. data/Gemfile +3 -1
  7. data/README.md +15 -9
  8. data/Rakefile +9 -4
  9. data/gemfiles/Gemfile.rails3 +6 -0
  10. data/gemfiles/Gemfile.rails4 +5 -0
  11. data/gemfiles/Gemfile.rails5 +5 -0
  12. data/gemfiles/Gemfile.rails6 +5 -0
  13. data/lib/validates_host.rb +14 -5
  14. data/lib/validates_host/domain_name.rb +15 -0
  15. data/lib/validates_host/domain_name_validator.rb +10 -17
  16. data/lib/validates_host/host_name.rb +15 -0
  17. data/lib/validates_host/host_name_validator.rb +10 -17
  18. data/lib/validates_host/ip.rb +17 -0
  19. data/lib/validates_host/ip_validator.rb +10 -17
  20. data/lib/validates_host/{shoulda-matchers/domain_name_matcher.rb → require_a_valid_domain_name_matcher.rb} +9 -11
  21. data/lib/validates_host/{shoulda-matchers/host_name_matcher.rb → require_a_valid_host_name_matcher.rb} +9 -11
  22. data/lib/validates_host/{shoulda-matchers/ip_matcher.rb → require_a_valid_ip_matcher.rb} +9 -11
  23. data/lib/validates_host/{shoulda-matchers/subnet_matcher.rb → require_a_valid_subnet_matcher.rb} +9 -11
  24. data/lib/validates_host/subnet.rb +15 -0
  25. data/lib/validates_host/subnet_validator.rb +10 -17
  26. data/lib/validates_host/version.rb +3 -1
  27. data/spec/fake_app/server.rb +19 -5
  28. data/spec/fake_app/subnet.rb +11 -3
  29. data/spec/shoulda/matchers/active_model/require_a_valid_domain_name_matcher_spec.rb +23 -0
  30. data/spec/shoulda/matchers/active_model/require_a_valid_host_name_matcher_spec.rb +23 -0
  31. data/spec/shoulda/matchers/active_model/require_a_valid_ip_matcher_spec.rb +23 -0
  32. data/spec/shoulda/matchers/active_model/require_a_valid_subnet_matcher_spec.rb +23 -0
  33. data/spec/spec_helper.rb +11 -8
  34. data/spec/validates_host/domain_name_validator_spec.rb +23 -22
  35. data/spec/validates_host/host_name_validator_spec.rb +23 -22
  36. data/spec/validates_host/ip_validator_spec.rb +38 -22
  37. data/spec/validates_host/subnet_validator_spec.rb +23 -22
  38. data/validates_host.gemspec +23 -19
  39. metadata +84 -95
  40. data/lib/validates_host/remarkable.rb +0 -4
  41. data/lib/validates_host/remarkable/domain_name_matcher.rb +0 -29
  42. data/lib/validates_host/remarkable/host_name_matcher.rb +0 -29
  43. data/lib/validates_host/remarkable/ip_matcher.rb +0 -29
  44. data/lib/validates_host/remarkable/subnet_matcher.rb +0 -29
  45. data/lib/validates_host/shoulda-matchers.rb +0 -4
  46. data/spec/fake_app/db/migrations/create_servers.rb +0 -13
  47. data/spec/fake_app/db/migrations/create_subnets.rb +0 -12
  48. data/spec/validates_host/remarkable/domain_name_matcher_spec.rb +0 -20
  49. data/spec/validates_host/remarkable/host_name_matcher_spec.rb +0 -20
  50. data/spec/validates_host/remarkable/ip_matcher_spec.rb +0 -20
  51. data/spec/validates_host/remarkable/subnet_matcher_spec.rb +0 -20
  52. data/spec/validates_host/shoulda-matchers/domain_name_matcher_spec.rb +0 -20
  53. data/spec/validates_host/shoulda-matchers/host_name_matcher_spec.rb +0 -20
  54. data/spec/validates_host/shoulda-matchers/ip_matcher_spec.rb +0 -20
  55. data/spec/validates_host/shoulda-matchers/subnet_matcher_spec.rb +0 -20
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 145ca9cab05675368ca33a513820cdaca5c50747a197635c48455b94464bf93b
4
+ data.tar.gz: 9dafd02e5e4481a18c05c475eaea0c70c438886efe13d9cf9ed3735f8fb1016f
5
+ SHA512:
6
+ metadata.gz: a99df100cd2ec1b958a3462bd8f314a75687f4e0d0949b80cc4d7dcb8b19a7cd8484d2c9c94444aca8c40bd3c1efff3a0ba8fbee0a9086a52d9615093c436507
7
+ data.tar.gz: d10314be5e20e31cc370d8245b42ce5c976d9ab4a4f4129e2d5de3627dbcf728d79d9819c54e0c95047b40b5b3107d38d8665a585ee3187045838170ac321404
data/.gitignore CHANGED
@@ -3,7 +3,8 @@
3
3
  .bundle
4
4
  .config
5
5
  .yardoc
6
- .rvmrc
6
+ .coveralls.yml
7
+ *.swp
7
8
  Gemfile.lock
8
9
  InstalledFiles
9
10
  _yardoc
@@ -13,6 +14,6 @@ lib/bundler/man
13
14
  pkg
14
15
  rdoc
15
16
  spec/reports
16
- test/tmp
17
- test/version_tmp
18
- tmp
17
+ tmp
18
+ .*~
19
+ *~
data/.rubocop.yml ADDED
@@ -0,0 +1,57 @@
1
+ require: rubocop-rspec
2
+
3
+ AllCops:
4
+ TargetRubyVersion: 2.0
5
+ NewCops: enable
6
+
7
+ Layout/EmptyLinesAroundAttributeAccessor:
8
+ Enabled: true
9
+
10
+ Layout/LineLength:
11
+ Max: 150
12
+
13
+ Layout/SpaceAroundMethodCallOperator:
14
+ Enabled: true
15
+
16
+ Lint/DeprecatedOpenSSLConstant:
17
+ Enabled: true
18
+
19
+ Lint/MixedRegexpCaptureTypes:
20
+ Enabled: true
21
+
22
+ Lint/RaiseException:
23
+ Enabled: true
24
+
25
+ Lint/StructNewOverride:
26
+ Enabled: true
27
+
28
+ Metrics/BlockLength:
29
+ Enabled: false
30
+
31
+ Style/Documentation:
32
+ Enabled: false
33
+
34
+ Style/ExponentialNotation:
35
+ Enabled: true
36
+
37
+ Style/HashEachMethods:
38
+ Enabled: true
39
+
40
+ Style/HashTransformKeys:
41
+ Enabled: true
42
+
43
+ Style/HashTransformValues:
44
+ Enabled: true
45
+
46
+ Style/MixinUsage:
47
+ Exclude:
48
+ - 'spec/spec_helper.rb'
49
+
50
+ Style/RedundantRegexpCharacterClass:
51
+ Enabled: true
52
+
53
+ Style/RedundantRegexpEscape:
54
+ Enabled: true
55
+
56
+ Style/SlicingWithRange:
57
+ Enabled: true
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ validates_host
data/.travis.yml CHANGED
@@ -1,4 +1,45 @@
1
- rvm:
2
- - 1.8.7
3
- - 1.9.2
4
- - 1.9.3
1
+ matrix:
2
+ include:
3
+ - rvm: 2.0
4
+ gemfile: gemfiles/Gemfile.rails3
5
+ - rvm: 2.0
6
+ gemfile: gemfiles/Gemfile.rails4
7
+ - rvm: 2.1
8
+ gemfile: gemfiles/Gemfile.rails3
9
+ - rvm: 2.1
10
+ gemfile: gemfiles/Gemfile.rails4
11
+ - rvm: 2.2
12
+ gemfile: gemfiles/Gemfile.rails4
13
+ - rvm: 2.2
14
+ gemfile: gemfiles/Gemfile.rails5
15
+ - rvm: 2.3
16
+ gemfile: gemfiles/Gemfile.rails4
17
+ - rvm: 2.3
18
+ gemfile: gemfiles/Gemfile.rails5
19
+ - rvm: 2.4
20
+ gemfile: gemfiles/Gemfile.rails4
21
+ - rvm: 2.4
22
+ gemfile: gemfiles/Gemfile.rails5
23
+ - rvm: 2.5
24
+ gemfile: gemfiles/Gemfile.rails4
25
+ - rvm: 2.5
26
+ gemfile: gemfiles/Gemfile.rails5
27
+ - rvm: 2.5
28
+ gemfile: gemfiles/Gemfile.rails6
29
+ - rvm: 2.6
30
+ gemfile: gemfiles/Gemfile.rails4
31
+ - rvm: 2.6
32
+ gemfile: gemfiles/Gemfile.rails5
33
+ - rvm: 2.6
34
+ gemfile: gemfiles/Gemfile.rails6
35
+ - rvm: 2.7
36
+ gemfile: gemfiles/Gemfile.rails4
37
+ - rvm: 2.7
38
+ gemfile: gemfiles/Gemfile.rails5
39
+ - rvm: 2.7
40
+ gemfile: gemfiles/Gemfile.rails6
41
+ - rvm: 3.0
42
+ gemfile: gemfiles/Gemfile.rails4
43
+ - rvm: 3.0
44
+ gemfile: gemfiles/Gemfile.rails6
45
+ script: rake complete
data/Gemfile CHANGED
@@ -1,4 +1,6 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source 'https://rubygems.org'
2
4
 
3
5
  # Specify your gem's dependencies in validates_host.gemspec
4
- gemspec
6
+ gemspec
data/README.md CHANGED
@@ -1,6 +1,9 @@
1
- # ValidatesHost [![Build Status](https://secure.travis-ci.org/plribeiro3000/validates_host.png?branch=master)](http://travis-ci.org/plribeiro3000/validates_host)
1
+ # ValidatesHost
2
2
 
3
- Rails gem to validate Host Related fields.
3
+ [![Gem Version](https://badge.fury.io/rb/validates_host.png)](http://badge.fury.io/rb/validates_host) [![Build Status](https://secure.travis-ci.org/plribeiro3000/validates_host.png?branch=master)](http://travis-ci.org/plribeiro3000/validates_host) [![Coverage Status](https://coveralls.io/repos/plribeiro3000/validates_host/badge.png?branch=master)](https://coveralls.io/r/plribeiro3000/validates_host) [![Code Climate](https://codeclimate.com/github/plribeiro3000/validates_host.png)](https://codeclimate.com/github/plribeiro3000/validates_host)
4
+
5
+ Validates host attributes and test it in a simple way.
6
+ Supports ruby 2.0+ and rails 3+
4
7
 
5
8
  ## Installation
6
9
 
@@ -18,23 +21,26 @@ Or install it yourself as:
18
21
 
19
22
  ## Usage
20
23
 
21
- Lets say you have a model that you want to have valid host fields. Just add this to your model:
24
+ Just use like any other validator:
22
25
 
23
26
  ```ruby
24
27
  class Server < ActiveRecord::Base
25
- validates :domain_name, :domain_name => true
26
- validates :host_name, :host_name => true
27
- validates :ip, :ip => true
28
+ validates :domain_name, domain_name: true
29
+ validates :host_name, host_name: true
30
+ validates :ip, ip: true
28
31
  end
29
32
 
30
33
  class Subnet < ActiveRecord::Base
31
- validates :value, :subnet => true
34
+ validates :value, subnet: true
32
35
  end
33
36
  ```
34
37
 
35
- ## Test
38
+ ## Notes
39
+
40
+ It will load a macher to test automatically if the gem is below shoulda-matchers.
36
41
 
37
- This gem has builtin matchers for shoulda-matchers and remarkable.
42
+ ## Mantainers
43
+ [@plribeiro3000](https://github.com/plribeiro3000)
38
44
 
39
45
  ## Contributing
40
46
 
data/Rakefile CHANGED
@@ -1,7 +1,12 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+ require 'rubocop/rake_task'
3
6
 
4
7
  RSpec::Core::RakeTask.new
8
+ RuboCop::RakeTask.new
5
9
 
6
- desc "Default Task"
7
- task :default => [ :spec ]
10
+ desc 'Default Task'
11
+ task default: [:spec]
12
+ task complete: %i[spec rubocop]
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'activesupport', '3.2.14'
4
+ gem 'mime-types', '< 2.0'
5
+
6
+ gemspec :path => '../'
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'activesupport', '>= 4.0.0', '< 5.0.0'
4
+
5
+ gemspec :path => '../'
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'activesupport', '>= 5.0.0', '< 6.0.0'
4
+
5
+ gemspec :path => '../'
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'activesupport', '>= 6.0.0'
4
+
5
+ gemspec :path => '../'
@@ -1,12 +1,21 @@
1
- require "validates_host/version"
1
+ # frozen_string_literal: true
2
2
 
3
- #ActiveModel Validators
3
+ require 'active_support/core_ext/array/wrap'
4
4
  require 'validates_host/host_name_validator'
5
5
  require 'validates_host/domain_name_validator'
6
6
  require 'validates_host/ip_validator'
7
7
  require 'validates_host/subnet_validator'
8
8
 
9
+ if defined?(::Shoulda)
10
+ require 'validates_host/require_a_valid_domain_name_matcher'
11
+ require 'validates_host/require_a_valid_host_name_matcher'
12
+ require 'validates_host/require_a_valid_ip_matcher'
13
+ require 'validates_host/require_a_valid_subnet_matcher'
14
+ end
9
15
 
10
- #Rspec Matchers
11
- require "validates_host/shoulda-matchers" if defined?(::Shoulda)
12
- require "validates_host/remarkable" if defined?(::Remarkable)
16
+ module ValidatesHost
17
+ autoload :Subnet, 'validates_host/subnet'
18
+ autoload :Ip, 'validates_host/ip'
19
+ autoload :HostName, 'validates_host/host_name'
20
+ autoload :DomainName, 'validates_host/domain_name'
21
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ValidatesHost
4
+ class DomainName
5
+ def initialize(domain_name)
6
+ @domain_name = domain_name
7
+ end
8
+
9
+ def valid?
10
+ return true if @domain_name.blank?
11
+
12
+ @domain_name =~ %r{^[a-z0-9]+([\-.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(/.*)?$}
13
+ end
14
+ end
15
+ end
@@ -1,23 +1,16 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class DomainNameValidator < ActiveModel::EachValidator
2
4
  def validate_each(record, attribute, value)
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?
5
- end
6
- end
5
+ return if ValidatesHost::DomainName.new(value).valid?
7
6
 
8
- module ValidatesHost
9
- class DomainName
10
- def initialize(domain_name)
11
- @domain_name = domain_name
12
- end
7
+ ruby_prior_version_three =
8
+ Gem::Version.new(RUBY_VERSION) < Gem::Version.new('3.0.0')
13
9
 
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
18
-
19
- def domain_name
20
- @domain_name
10
+ if ruby_prior_version_three
11
+ record.errors.add(attribute, :invalid, options)
12
+ else
13
+ record.errors.add(attribute, :invalid, **options)
21
14
  end
22
15
  end
23
- end
16
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ValidatesHost
4
+ class HostName
5
+ def initialize(host_name)
6
+ @host_name = host_name
7
+ end
8
+
9
+ def valid?
10
+ return true if @host_name.blank?
11
+
12
+ @host_name =~ /^[a-z][a-z0-9-]+$/
13
+ end
14
+ end
15
+ end
@@ -1,23 +1,16 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class HostNameValidator < ActiveModel::EachValidator
2
4
  def validate_each(record, attribute, value)
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?
5
- end
6
- end
5
+ return if ValidatesHost::HostName.new(value).valid?
7
6
 
8
- module ValidatesHost
9
- class HostName
10
- def initialize(host_name)
11
- @host_name = host_name
12
- end
7
+ ruby_prior_version_three =
8
+ Gem::Version.new(RUBY_VERSION) < Gem::Version.new('3.0.0')
13
9
 
14
- def valid?
15
- return true if @host_name.blank?
16
- @host_name =~ /^[a-z][a-z0-9-]+$/
17
- end
18
-
19
- def host_name
20
- @host_name
10
+ if ruby_prior_version_three
11
+ record.errors.add(attribute, :invalid, options)
12
+ else
13
+ record.errors.add(attribute, :invalid, **options)
21
14
  end
22
15
  end
23
- end
16
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'resolv'
4
+
5
+ module ValidatesHost
6
+ class Ip
7
+ def initialize(value)
8
+ @value = value
9
+ end
10
+
11
+ def valid?
12
+ return true if @value.blank?
13
+
14
+ @value =~ Regexp.union(Resolv::IPv4::Regex, Resolv::IPv6::Regex)
15
+ end
16
+ end
17
+ end
@@ -1,23 +1,16 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class IpValidator < ActiveModel::EachValidator
2
4
  def validate_each(record, attribute, value)
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?
5
- end
6
- end
5
+ return if ValidatesHost::Ip.new(value).valid?
7
6
 
8
- module ValidatesHost
9
- class Ip
10
- def initialize(ip)
11
- @ip = ip
12
- end
7
+ ruby_prior_version_three =
8
+ Gem::Version.new(RUBY_VERSION) < Gem::Version.new('3.0.0')
13
9
 
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
18
-
19
- def ip
20
- @ip
10
+ if ruby_prior_version_three
11
+ record.errors.add(attribute, :invalid, options)
12
+ else
13
+ record.errors.add(attribute, :invalid, **options)
21
14
  end
22
15
  end
23
- end
16
+ end
@@ -1,17 +1,15 @@
1
- require "shoulda-matchers"
1
+ # frozen_string_literal: true
2
+
3
+ require 'shoulda-matchers'
2
4
 
3
5
  module Shoulda
4
6
  module Matchers
5
7
  module ActiveModel
6
8
  def require_a_valid_domain_name(attribute = :domain_name)
7
- DomainNameMatcher.new(attribute)
9
+ RequireAValidDomainNameMatcher.new(attribute)
8
10
  end
9
11
 
10
- class DomainNameMatcher < ValidationMatcher
11
- def initialize(attribute)
12
- @attribute = attribute
13
- end
14
-
12
+ class RequireAValidDomainNameMatcher < ValidationMatcher
15
13
  def description
16
14
  "require #{@attribute} to be a valid domain name"
17
15
  end
@@ -19,19 +17,19 @@ module Shoulda
19
17
  def matches?(subject)
20
18
  super(subject)
21
19
 
22
- disallows_invalid_value and allows_valid_value
20
+ disallows_invalid_value && allows_valid_value
23
21
  end
24
22
 
25
23
  private
26
24
 
27
25
  def disallows_invalid_value
28
- disallows_value_of("example")
26
+ disallows_value_of('example')
29
27
  end
30
28
 
31
29
  def allows_valid_value
32
- allows_value_of("example.com")
30
+ allows_value_of('example.com')
33
31
  end
34
32
  end
35
33
  end
36
34
  end
37
- end
35
+ end