validates_host 0.2.0 → 1.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.
Files changed (53) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +5 -4
  3. data/.rubocop.yml +53 -0
  4. data/.ruby-gemset +1 -0
  5. data/.travis.yml +31 -0
  6. data/Gemfile +3 -1
  7. data/README.md +24 -3
  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/lib/validates_host.rb +16 -5
  13. data/lib/validates_host/domain_name.rb +15 -0
  14. data/lib/validates_host/domain_name_validator.rb +3 -19
  15. data/lib/validates_host/host_name.rb +15 -0
  16. data/lib/validates_host/host_name_validator.rb +3 -19
  17. data/lib/validates_host/ip.rb +17 -0
  18. data/lib/validates_host/ip_validator.rb +3 -19
  19. data/lib/validates_host/{shoulda-matchers/domain_name_matcher.rb → require_a_valid_domain_name_matcher.rb} +9 -11
  20. data/lib/validates_host/{shoulda-matchers/host_name_matcher.rb → require_a_valid_host_name_matcher.rb} +9 -11
  21. data/lib/validates_host/{shoulda-matchers/ip_matcher.rb → require_a_valid_ip_matcher.rb} +9 -11
  22. data/lib/validates_host/require_a_valid_subnet_matcher.rb +35 -0
  23. data/lib/validates_host/subnet.rb +15 -0
  24. data/lib/validates_host/subnet_validator.rb +7 -0
  25. data/lib/validates_host/version.rb +3 -1
  26. data/spec/fake_app/server.rb +19 -5
  27. data/spec/fake_app/subnet.rb +11 -0
  28. data/spec/shoulda/matchers/active_model/require_a_valid_domain_name_matcher_spec.rb +23 -0
  29. data/spec/shoulda/matchers/active_model/require_a_valid_host_name_matcher_spec.rb +23 -0
  30. data/spec/shoulda/matchers/active_model/require_a_valid_ip_matcher_spec.rb +23 -0
  31. data/spec/shoulda/matchers/active_model/require_a_valid_subnet_matcher_spec.rb +23 -0
  32. data/spec/spec_helper.rb +11 -7
  33. data/spec/validates_host/domain_name_validator_spec.rb +41 -0
  34. data/spec/validates_host/host_name_validator_spec.rb +41 -0
  35. data/spec/validates_host/ip_validator_spec.rb +56 -0
  36. data/spec/validates_host/subnet_validator_spec.rb +41 -0
  37. data/validates_host.gemspec +22 -19
  38. metadata +89 -88
  39. data/lib/validates_host/remarkable.rb +0 -3
  40. data/lib/validates_host/remarkable/domain_name_matcher.rb +0 -29
  41. data/lib/validates_host/remarkable/host_name_matcher.rb +0 -29
  42. data/lib/validates_host/remarkable/ip_matcher.rb +0 -29
  43. data/lib/validates_host/shoulda-matchers.rb +0 -3
  44. data/spec/fake_app/db/migrations/create_servers.rb +0 -13
  45. data/spec/validates_host.rb/domain_name_validator_spec.rb +0 -40
  46. data/spec/validates_host.rb/host_name_validator_spec.rb +0 -40
  47. data/spec/validates_host.rb/ip_validator_spec.rb +0 -40
  48. data/spec/validates_host.rb/remarkable/domain_name_matcher_spec.rb +0 -20
  49. data/spec/validates_host.rb/remarkable/host_name_matcher_spec.rb +0 -20
  50. data/spec/validates_host.rb/remarkable/ip_matcher_spec.rb +0 -20
  51. data/spec/validates_host.rb/shoulda-matchers/domain_name_matcher_spec.rb +0 -20
  52. data/spec/validates_host.rb/shoulda-matchers/host_name_matcher_spec.rb +0 -20
  53. data/spec/validates_host.rb/shoulda-matchers/ip_matcher_spec.rb +0 -20
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f5b11543c72bb9ea5223f7f216d24f79606ca13e2f9d97967718132ef9589158
4
+ data.tar.gz: 3338ae72f989b7fa89cc546918519b69cd2000ef0c081a14afe465720ce0ae4f
5
+ SHA512:
6
+ metadata.gz: e4146e5334ec9aa078d6f715f225296e73625a10c3508bfa67bbaca3a85092326049873ae7cf8a7d0a5cec4bbbdd5a04c262bc1d7b70e6c100b8101d0263e755
7
+ data.tar.gz: 8d122306cf4753d4e077a0b76c92d5f8da7c9623f17cf1ff3c5f40368c42794f3da1ccb02e2cb66d68e923cd767e367fcfbfc63585bf5082780a7f97454b1530
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
+ *~
@@ -0,0 +1,53 @@
1
+ require: rubocop-rspec
2
+
3
+ Layout/EmptyLinesAroundAttributeAccessor:
4
+ Enabled: true
5
+
6
+ Layout/LineLength:
7
+ Max: 150
8
+
9
+ Layout/SpaceAroundMethodCallOperator:
10
+ Enabled: true
11
+
12
+ Lint/DeprecatedOpenSSLConstant:
13
+ Enabled: true
14
+
15
+ Lint/MixedRegexpCaptureTypes:
16
+ Enabled: true
17
+
18
+ Lint/RaiseException:
19
+ Enabled: true
20
+
21
+ Lint/StructNewOverride:
22
+ Enabled: true
23
+
24
+ Metrics/BlockLength:
25
+ Enabled: false
26
+
27
+ Style/Documentation:
28
+ Enabled: false
29
+
30
+ Style/ExponentialNotation:
31
+ Enabled: true
32
+
33
+ Style/HashEachMethods:
34
+ Enabled: true
35
+
36
+ Style/HashTransformKeys:
37
+ Enabled: true
38
+
39
+ Style/HashTransformValues:
40
+ Enabled: true
41
+
42
+ Style/MixinUsage:
43
+ Exclude:
44
+ - 'spec/spec_helper.rb'
45
+
46
+ Style/RedundantRegexpCharacterClass:
47
+ Enabled: true
48
+
49
+ Style/RedundantRegexpEscape:
50
+ Enabled: true
51
+
52
+ Style/SlicingWithRange:
53
+ Enabled: true
@@ -0,0 +1 @@
1
+ validates_host
@@ -0,0 +1,31 @@
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.6
28
+ gemfile: gemfiles/Gemfile.rails4
29
+ - rvm: 2.6
30
+ gemfile: gemfiles/Gemfile.rails5
31
+ 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,8 @@
1
- # ValidatesHost
1
+ # ValidatesHost
2
2
 
3
- TODO: Write a gem description
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
4
6
 
5
7
  ## Installation
6
8
 
@@ -18,7 +20,26 @@ Or install it yourself as:
18
20
 
19
21
  ## Usage
20
22
 
21
- TODO: Write usage instructions here
23
+ Just use like any other validator:
24
+
25
+ ```ruby
26
+ class Server < ActiveRecord::Base
27
+ validates :domain_name, domain_name: true
28
+ validates :host_name, host_name: true
29
+ validates :ip, ip: true
30
+ end
31
+
32
+ class Subnet < ActiveRecord::Base
33
+ validates :value, subnet: true
34
+ end
35
+ ```
36
+
37
+ ## Notes
38
+
39
+ It will load a macher to test automatically if the gem is below shoulda-matchers.
40
+
41
+ ## Mantainers
42
+ [@plribeiro3000](https://github.com/plribeiro3000)
22
43
 
23
44
  ## Contributing
24
45
 
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 :rubygems
2
+
3
+ gem 'activesupport', '3.2.14'
4
+ gem 'mime-types', '< 2.0'
5
+
6
+ gemspec :path => '../'
@@ -0,0 +1,5 @@
1
+ source :rubygems
2
+
3
+ gem 'activesupport', '>= 4.0.0', '< 5.0.0'
4
+
5
+ gemspec :path => '../'
@@ -0,0 +1,5 @@
1
+ source :rubygems
2
+
3
+ gem 'activesupport', '>= 5.0.0'
4
+
5
+ gemspec :path => '../'
@@ -1,10 +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
+ require 'validates_host/subnet_validator'
7
8
 
8
- #Rspec Matchers
9
- require "validates_host/shoulda-matchers" if defined?(::Shoulda)
10
- require "validates_host/remarkable" if defined?(::Remarkable)
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
15
+
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,7 @@
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
+ record.errors.add(attribute, :invalid, options) unless ValidatesHost::DomainName.new(value).valid?
5
6
  end
6
7
  end
7
-
8
- module ValidatesHost
9
- class DomainName
10
- def initialize(domain_name)
11
- @domain_name = domain_name
12
- end
13
-
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
21
- end
22
- end
23
- 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,7 @@
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
+ record.errors.add(attribute, :invalid, options) unless ValidatesHost::HostName.new(value).valid?
5
6
  end
6
7
  end
7
-
8
- module ValidatesHost
9
- class HostName
10
- def initialize(host_name)
11
- @host_name = host_name
12
- end
13
-
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
21
- end
22
- end
23
- 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,7 @@
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
+ record.errors.add(attribute, :invalid, options) unless ValidatesHost::Ip.new(value).valid?
5
6
  end
6
7
  end
7
-
8
- module ValidatesHost
9
- class Ip
10
- def initialize(ip)
11
- @ip = ip
12
- end
13
-
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
21
- end
22
- end
23
- 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
@@ -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_host_name(attribute = :host_name)
7
- HostNameMatcher.new(attribute)
9
+ RequireAValidHostNameMatcher.new(attribute)
8
10
  end
9
11
 
10
- class HostNameMatcher < ValidationMatcher
11
- def initialize(attribute)
12
- @attribute = attribute
13
- end
14
-
12
+ class RequireAValidHostNameMatcher < ValidationMatcher
15
13
  def description
16
14
  "require #{@attribute} to be a valid host 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("1bd")
26
+ disallows_value_of('1bd')
29
27
  end
30
28
 
31
29
  def allows_valid_value
32
- allows_value_of("bd01")
30
+ allows_value_of('bd01')
33
31
  end
34
32
  end
35
33
  end
36
34
  end
37
- end
35
+ end