validates_ip_format_of 1.0.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/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "faker"
10
+ gem "activerecord", ">= 3.0.7"
11
+ gem "rspec", "~> 2.3.0"
12
+ gem "bundler", "~> 1.0.0"
13
+ gem "jeweler", "~> 1.6.2"
14
+ gem "rcov", ">= 0"
15
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Ryan Lovelett
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.markdown ADDED
@@ -0,0 +1,54 @@
1
+ # validates\_ip\_format\_of
2
+
3
+ Rails plugin that provides a `validates_ip_format_of` method to `ActiveRecord` models. IPs are validated by regexp.
4
+
5
+ Known to be compatible with Ruby 1.9.2.
6
+
7
+ Known to be compatible with ActiveRecord 3.0.7.
8
+
9
+
10
+ ## Usage
11
+
12
+ After installing the plugin, it's used like
13
+
14
+ class User < ActiveRecord::Base
15
+ validates_ip_format_of :dhcp,
16
+ :allow_nil => true,
17
+ :message => 'is completely unacceptable'
18
+ end
19
+
20
+ Takes the same arguments as [`validates_format_of`](http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#M001052) except for the `:with` regexp.
21
+
22
+ Please note that the regexp used to validate IPs is not perfect, but hopefully good enough. See the test suite. Patches are very welcome.
23
+
24
+ ## Limitations and design choices
25
+
26
+ Does not handle IPv6.
27
+
28
+ By design, the plugin does not allow RFC 1918 IPv4 addresses, e.g., 10.0.0.0, 172.16.0.0, 192.168.0.0, from being input. Also, prevents 127.0.0.1 and 0.0.0.0.
29
+
30
+ ## Credits and license
31
+
32
+ Based on my contributions made to `validates_url_format_of` written by [Henrik Nyh](https://github.com/henrik/validates_url_format_of), which is also released under the MIT license.
33
+
34
+ By [Ryan Lovelett](http://www.wahvee.com/) under the MIT license:
35
+
36
+ > Copyright (c) 2011 Ryan Lovelett
37
+ >
38
+ > Permission is hereby granted, free of charge, to any person obtaining a copy
39
+ > of this software and associated documentation files (the "Software"), to deal
40
+ > in the Software without restriction, including without limitation the rights
41
+ > to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
42
+ > copies of the Software, and to permit persons to whom the Software is
43
+ > furnished to do so, subject to the following conditions:
44
+ >
45
+ > The above copyright notice and this permission notice shall be included in
46
+ > all copies or substantial portions of the Software.
47
+ >
48
+ > THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
49
+ > IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
50
+ > FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
51
+ > AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
52
+ > LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
53
+ > OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
54
+ > THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "validates_ip_format_of"
18
+ gem.homepage = "http://github.com/RLovelett/validates_ip_format_of"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Validate the format of a IP by regexp in Ruby on Rails.}
21
+ gem.description = %Q{Rails plugin that provides a validates_ip_format_of method to ActiveRecord models. IPs are validated by regexp.}
22
+ gem.email = "ryan@wahvee.com"
23
+ gem.authors = ["Ryan Lovelett"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'rake/rdoctask'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "validates_ip_format_of #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
data/init.rb ADDED
@@ -0,0 +1,47 @@
1
+ require 'active_record'
2
+
3
+ # encoding: utf-8
4
+ module ValidatesIpFormatOf
5
+ IPv4_PART = /\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]/ # 0-255
6
+
7
+ # http://en.wikipedia.org/wiki/IP_address
8
+ # http://en.wikipedia.org/wiki/Default_route
9
+ # http://en.wikipedia.org/wiki/Localhost
10
+
11
+ # Blocks IPv4 10.0.0.0 - 10.255.255.255
12
+ TWENTY_FOUR_BIT_BLOCK = /(?!10\.((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.?)){3})/
13
+ # Blocks IPv4 172.16.0.0 - 172.31.255.255
14
+ TWENTY_BIT_BLOCK = /(?!172\.(0?3[0-1]|0?2[0-9]|0?1[6-9])\.((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.?)){2})/
15
+ # Blocks IPv4 192.168.0.0 - 192.168.255.255
16
+ SIXTEEN_BIT_BLOCK = /(?!192\.168\.((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.?)){2})/
17
+ # Blocks IPv4 127.0.0.1 (which is also localhost)
18
+ LOCALHOST_LOOPBACK = /(?!127\.0\.0\.[0-8])/
19
+ ALL_ROUTES = /(?!0\.0\.0\.0)/
20
+ RFC_1918_IPS = %r{#{TWENTY_FOUR_BIT_BLOCK}#{TWENTY_BIT_BLOCK}#{SIXTEEN_BIT_BLOCK}}
21
+
22
+ REGEXP = %r{
23
+ \A
24
+ #{LOCALHOST_LOOPBACK} # blocks 127.0.0.0/8
25
+ #{ALL_ROUTES} # blocks 0.0.0.0
26
+ #{RFC_1918_IPS} # blocks the use of RFC 1918 private network IPv4 addresses
27
+ #{IPv4_PART}(\.#{IPv4_PART}){3} # Allow IPv4
28
+ \Z
29
+ }iux
30
+
31
+ DEFAULT_MESSAGE = 'does not appear to be a valid IP Address'
32
+
33
+ def validates_ip_format_of(*attr_names)
34
+ options = { :allow_nil => false,
35
+ :allow_blank => false,
36
+ :message => DEFAULT_MESSAGE,
37
+ :with => REGEXP
38
+ }
39
+ options = options.merge(attr_names.pop) if attr_names.last.is_a?(Hash)
40
+ attr_names.each do |attr_name|
41
+ validates_format_of(attr_name, options)
42
+ end
43
+ end
44
+
45
+ end
46
+
47
+ ActiveRecord::Base.extend(ValidatesIpFormatOf)
@@ -0,0 +1,14 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'faker'
5
+ require "#{File.expand_path(File.dirname(__FILE__))}/../init.rb"
6
+ require 'active_record'
7
+
8
+ # Requires supporting files with custom matchers and macros, etc,
9
+ # in ./support/ and its subdirectories.
10
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
11
+
12
+ RSpec.configure do |config|
13
+
14
+ end
@@ -0,0 +1,76 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ class Model
4
+ include ActiveModel::Validations
5
+ extend ValidatesIpFormatOf
6
+
7
+ attr_accessor :dhcp
8
+ validates_ip_format_of :dhcp
9
+
10
+ attr_accessor :dns
11
+ validates_ip_format_of :dns
12
+
13
+ attr_accessor :custom_ip
14
+ validates_ip_format_of :custom_ip, :message => 'custom message'
15
+
16
+ end
17
+
18
+ describe ValidatesIpFormatOf do
19
+ before { @model = Model.new }
20
+ it "should allow valid IPs" do
21
+ 1000.times.each do
22
+ begin
23
+ ip = Faker::Internet.ip_v4_address
24
+ end while /^10.*|^172.*|^192.168.*/.match(ip)
25
+ @model.dhcp = ip
26
+ @model.valid?.should be_false
27
+ puts "#{@model.dhcp} fails validation" if @model.errors.has_key?(:dhcp)
28
+ @model.errors.should_not have_key(:dhcp)
29
+ end
30
+ end
31
+
32
+ it "should reject invalid IPs" do
33
+ [
34
+ nil, 1, "", " ", "url",
35
+ "www.example.com",
36
+ "http://ex ample.com",
37
+ "http://ex_ample.com",
38
+ "http://example.com/foo bar",
39
+ 'http://256.0.0.1',
40
+ 'http://u:u:u@example.com',
41
+ 'http://r?ksmorgas.com',
42
+
43
+ # These can all be valid local URLs, but should not be considered valid
44
+ # for public consumption.
45
+ "http://example",
46
+ "http://example.c",
47
+ 'http://example.toolongtld',
48
+
49
+ # These are not real ips
50
+ "54.5679.98.90"
51
+ ].each do |url|
52
+ @model.dns = url
53
+ @model.valid?.should be_false
54
+ @model.errors.should have_key(:dns)
55
+ end
56
+ end
57
+
58
+ it "should require publicly routable IPv4 addresses" do
59
+ private_ips_10 = 100.times.map { "10.#{Random.new.rand(0..255)}.#{Random.new.rand(0..255)}.#{Random.new.rand(0..255)}" }
60
+ private_ips_172 = 100.times.map { "172.#{Random.new.rand(16..31)}.#{Random.new.rand(0..255)}.#{Random.new.rand(0..255)}" }
61
+ private_ips_192 = 100.times.map { "192.168.#{Random.new.rand(0..255)}.#{Random.new.rand(0..255)}" }
62
+ reserved_ips = (private_ips_10 << private_ips_172 << private_ips_192 << "0.0.0.0" << "127.0.0.1").flatten
63
+ reserved_ips.each do |url|
64
+ @model.dhcp = url
65
+ @model.valid?.should be_false
66
+ @model.errors.should have_key(:dhcp)
67
+ end
68
+ end
69
+
70
+ it "should override defaults" do
71
+ @model.custom_ip = 'x'
72
+ @model.valid?.should be_false
73
+ @model.errors.should have_key(:custom_ip)
74
+ @model.errors[:custom_ip].should include('custom message')
75
+ end
76
+ end
@@ -0,0 +1,65 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{validates_ip_format_of}
8
+ s.version = "1.0.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Ryan Lovelett"]
12
+ s.date = %q{2011-06-01}
13
+ s.description = %q{Rails plugin that provides a validates_ip_format_of method to ActiveRecord models. IPs are validated by regexp.}
14
+ s.email = %q{ryan@wahvee.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.markdown"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rspec",
22
+ "Gemfile",
23
+ "LICENSE.txt",
24
+ "README.markdown",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "init.rb",
28
+ "spec/spec_helper.rb",
29
+ "spec/validates_ip_format_of_spec.rb",
30
+ "validates_ip_format_of.gemspec"
31
+ ]
32
+ s.homepage = %q{http://github.com/RLovelett/validates_ip_format_of}
33
+ s.licenses = ["MIT"]
34
+ s.require_paths = ["lib"]
35
+ s.rubygems_version = %q{1.6.2}
36
+ s.summary = %q{Validate the format of a IP by regexp in Ruby on Rails.}
37
+
38
+ if s.respond_to? :specification_version then
39
+ s.specification_version = 3
40
+
41
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
42
+ s.add_development_dependency(%q<faker>, [">= 0"])
43
+ s.add_development_dependency(%q<activerecord>, [">= 3.0.7"])
44
+ s.add_development_dependency(%q<rspec>, ["~> 2.3.0"])
45
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
46
+ s.add_development_dependency(%q<jeweler>, ["~> 1.6.2"])
47
+ s.add_development_dependency(%q<rcov>, [">= 0"])
48
+ else
49
+ s.add_dependency(%q<faker>, [">= 0"])
50
+ s.add_dependency(%q<activerecord>, [">= 3.0.7"])
51
+ s.add_dependency(%q<rspec>, ["~> 2.3.0"])
52
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
53
+ s.add_dependency(%q<jeweler>, ["~> 1.6.2"])
54
+ s.add_dependency(%q<rcov>, [">= 0"])
55
+ end
56
+ else
57
+ s.add_dependency(%q<faker>, [">= 0"])
58
+ s.add_dependency(%q<activerecord>, [">= 3.0.7"])
59
+ s.add_dependency(%q<rspec>, ["~> 2.3.0"])
60
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
61
+ s.add_dependency(%q<jeweler>, ["~> 1.6.2"])
62
+ s.add_dependency(%q<rcov>, [">= 0"])
63
+ end
64
+ end
65
+
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: validates_ip_format_of
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ryan Lovelett
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-06-01 00:00:00.000000000 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: faker
17
+ requirement: &69824927566980 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: *69824927566980
26
+ - !ruby/object:Gem::Dependency
27
+ name: activerecord
28
+ requirement: &69824927566500 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: 3.0.7
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: *69824927566500
37
+ - !ruby/object:Gem::Dependency
38
+ name: rspec
39
+ requirement: &69824927566020 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ version: 2.3.0
45
+ type: :development
46
+ prerelease: false
47
+ version_requirements: *69824927566020
48
+ - !ruby/object:Gem::Dependency
49
+ name: bundler
50
+ requirement: &69824927565540 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ~>
54
+ - !ruby/object:Gem::Version
55
+ version: 1.0.0
56
+ type: :development
57
+ prerelease: false
58
+ version_requirements: *69824927565540
59
+ - !ruby/object:Gem::Dependency
60
+ name: jeweler
61
+ requirement: &69824927565060 !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ~>
65
+ - !ruby/object:Gem::Version
66
+ version: 1.6.2
67
+ type: :development
68
+ prerelease: false
69
+ version_requirements: *69824927565060
70
+ - !ruby/object:Gem::Dependency
71
+ name: rcov
72
+ requirement: &69824927564580 !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ type: :development
79
+ prerelease: false
80
+ version_requirements: *69824927564580
81
+ description: Rails plugin that provides a validates_ip_format_of method to ActiveRecord
82
+ models. IPs are validated by regexp.
83
+ email: ryan@wahvee.com
84
+ executables: []
85
+ extensions: []
86
+ extra_rdoc_files:
87
+ - LICENSE.txt
88
+ - README.markdown
89
+ files:
90
+ - .document
91
+ - .rspec
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.markdown
95
+ - Rakefile
96
+ - VERSION
97
+ - init.rb
98
+ - spec/spec_helper.rb
99
+ - spec/validates_ip_format_of_spec.rb
100
+ - validates_ip_format_of.gemspec
101
+ has_rdoc: true
102
+ homepage: http://github.com/RLovelett/validates_ip_format_of
103
+ licenses:
104
+ - MIT
105
+ post_install_message:
106
+ rdoc_options: []
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ none: false
111
+ requirements:
112
+ - - ! '>='
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ segments:
116
+ - 0
117
+ hash: 1310148042613260488
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ none: false
120
+ requirements:
121
+ - - ! '>='
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ requirements: []
125
+ rubyforge_project:
126
+ rubygems_version: 1.6.2
127
+ signing_key:
128
+ specification_version: 3
129
+ summary: Validate the format of a IP by regexp in Ruby on Rails.
130
+ test_files: []