validates_url_format_of 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown ADDED
@@ -0,0 +1,49 @@
1
+ # validates\_url\_format\_of
2
+
3
+ Rails plugin that provides a `validates_url_format_of` method to `ActiveRecord` models. URLs are validated by regexp.
4
+
5
+ ## Usage
6
+
7
+ After installing the plugin, it's used like
8
+
9
+ class User < ActiveRecord::Base
10
+ validates_url_format_of :url,
11
+ :allow_nil => true,
12
+ :message => 'is completely unacceptable'
13
+ end
14
+
15
+ Takes the same arguments as [`validates_format_of`](http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#M001052) except for the `:with` regexp.
16
+
17
+ The default `:message` is different depending on whether the attribute name contains the word "URL". So you will get "Homepage URL does not appear to be valid" but "Homepage does not appear to be a valid URL" without having to customize the `:message`.
18
+
19
+ Please note that the regexp used to validate URLs is not perfect, but hopefully good enough. See the test suite. Patches are very welcome.
20
+
21
+ ## Limitations and design choices
22
+
23
+ Does not handle IPv6.
24
+
25
+ By design, the plugin does not allow e.g. "http://localhost" or "http://my.localurl", which are valid URLs but not suitable in most web apps. It also requires a "http://" or "https://" prefix, so just "example.com" is not valid. Fix that in the setter.
26
+
27
+ ## Credits and license
28
+
29
+ By [Henrik Nyh](http://henrik.nyh.se/) under the MIT license:
30
+
31
+ > Copyright (c) 2008 Henrik Nyh
32
+ >
33
+ > Permission is hereby granted, free of charge, to any person obtaining a copy
34
+ > of this software and associated documentation files (the "Software"), to deal
35
+ > in the Software without restriction, including without limitation the rights
36
+ > to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
37
+ > copies of the Software, and to permit persons to whom the Software is
38
+ > furnished to do so, subject to the following conditions:
39
+ >
40
+ > The above copyright notice and this permission notice shall be included in
41
+ > all copies or substantial portions of the Software.
42
+ >
43
+ > THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
44
+ > IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
45
+ > FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
46
+ > AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
47
+ > LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
48
+ > OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
49
+ > THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,58 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "validates_url_format_of"
8
+ gem.summary = %Q{ActiveRecord URL Validation}
9
+ gem.description = %Q{Rails plugin that provides a validates_url_format_of method to ActiveRecord models. URLs are validated by regexp.}
10
+ gem.email = "conickal@gmail.com"
11
+ gem.homepage = "http://github.com/conickal/validates_url_format_of"
12
+ gem.authors = ["Henrik Nyh", "Josh Nichols", "Nicholas Silva"]
13
+ gem.add_dependency('activerecord', '~> 2.3.4')
14
+ gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
19
+ end
20
+
21
+ require 'rake/testtask'
22
+ Rake::TestTask.new(:test) do |test|
23
+ test.libs << 'lib' << 'test'
24
+ test.pattern = 'test/**/*_test.rb'
25
+ test.verbose = true
26
+ end
27
+
28
+ begin
29
+ require 'rcov/rcovtask'
30
+ Rcov::RcovTask.new do |test|
31
+ test.libs << 'test'
32
+ test.pattern = 'test/**/*_test.rb'
33
+ test.verbose = true
34
+ end
35
+ rescue LoadError
36
+ task :rcov do
37
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
38
+ end
39
+ end
40
+
41
+
42
+ task :default => :test
43
+
44
+ require 'rake/rdoctask'
45
+ Rake::RDocTask.new do |rdoc|
46
+ if File.exist?('VERSION.yml')
47
+ config = YAML.load(File.read('VERSION.yml'))
48
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
49
+ else
50
+ version = ""
51
+ end
52
+
53
+ rdoc.rdoc_dir = 'rdoc'
54
+ rdoc.title = "validates_url_format_of #{version}"
55
+ rdoc.rdoc_files.include('README*')
56
+ rdoc.rdoc_files.include('lib/**/*.rb')
57
+ end
58
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.1
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'validates_url_format_of'
@@ -0,0 +1,30 @@
1
+ module ValidatesUrlFormatOf
2
+ IPv4_PART = /\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]/ # 0-255
3
+ REGEXP = %r{
4
+ \A
5
+ https?:// # http:// or https://
6
+ ([^\s:@]+:[^\s:@]*@)? # optional username:pw@
7
+ ( (xn--)?[^\W_]+([-.][^\W_]+)*\.[a-z]{2,6}\.? | # domain (including Punycode/IDN)...
8
+ #{IPv4_PART}(\.#{IPv4_PART}){3} ) # or IPv4
9
+ (:\d{1,5})? # optional port
10
+ ([/?]\S*)? # optional /whatever or ?whatever
11
+ \Z
12
+ }iux
13
+
14
+ DEFAULT_MESSAGE = 'does not appear to be a valid URL'
15
+ DEFAULT_MESSAGE_URL = 'does not appear to be valid'
16
+
17
+ def validates_url_format_of(*attr_names)
18
+ options = { :allow_nil => false,
19
+ :allow_blank => false,
20
+ :with => REGEXP }
21
+ options = options.merge(attr_names.pop) if attr_names.last.is_a?(Hash)
22
+
23
+ attr_names.each do |attr_name|
24
+ message = attr_name.to_s.match(/(_|\b)URL(_|\b)/i) ? DEFAULT_MESSAGE_URL : DEFAULT_MESSAGE
25
+ validates_format_of(attr_name, { :message => message }.merge(options))
26
+ end
27
+ end
28
+ end
29
+
30
+ ActiveRecord::Base.extend(ValidatesUrlFormatOf)
@@ -0,0 +1,36 @@
1
+ # encoding: utf-8
2
+ class Test::Unit::TestCase
3
+ def self.should_validate_url_format_of(attribute, options = {})
4
+ should_allow_values_for attribute,
5
+ 'http://example.com',
6
+ 'http://example.com/',
7
+ 'http://www.example.com/',
8
+ 'http://sub.domain.example.com/',
9
+ 'http://bbc.co.uk',
10
+ 'http://example.com?foo',
11
+ 'http://example.com?url=http://example.com',
12
+ 'http://example.com:8000',
13
+ 'http://www.sub.example.com/page.html?foo=bar&baz=%23#anchor',
14
+ 'http://user:pass@example.com',
15
+ 'http://user:@example.com',
16
+ 'http://example.com/~user',
17
+ 'http://example.xy', # Not a real TLD, but we're fine with anything of 2-6 chars
18
+ 'http://example.museum',
19
+ 'http://1.0.255.249',
20
+ 'http://1.2.3.4:80',
21
+ 'HttP://example.com',
22
+ 'https://example.com',
23
+ 'http://räksmörgås.nu', # IDN
24
+ 'http://xn--rksmrgs-5wao1o.nu', # Punycode
25
+ 'http://example.com.', # Explicit TLD root period
26
+ 'http://example.com./foo'
27
+
28
+ should_not_allow_values_for attribute,
29
+ "www.example.com",
30
+ "http://ex ample.com",
31
+ "http://example.com/foo bar",
32
+ 'http://256.0.0.1',
33
+ 'http://u:u:u@example.com',
34
+ 'http://r?ksmorgas.com'
35
+ end
36
+ end
@@ -0,0 +1,100 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'test/unit'
5
+ require 'active_record'
6
+ require "#{File.dirname(__FILE__)}/../init"
7
+
8
+ ActiveRecord::Base.establish_connection(
9
+ :adapter => 'sqlite3',
10
+ :database => ':memory:')
11
+
12
+ ActiveRecord::Schema.define(:version => 0) do
13
+ create_table :models, :force => true do |t|
14
+ t.string :homepage
15
+ t.string :my_UrL_hooray
16
+ t.string :custom_url
17
+ end
18
+ end
19
+
20
+ class Model < ActiveRecord::Base
21
+ validates_url_format_of :homepage
22
+ validates_url_format_of :my_UrL_hooray
23
+ validates_url_format_of :custom_url, :message => 'custom message'
24
+ end
25
+
26
+ class ValidatesUrlFormatOfTest < Test::Unit::TestCase
27
+
28
+ def setup
29
+ @model = Model.new
30
+ end
31
+
32
+ def test_should_allow_valid_urls
33
+ [
34
+ 'http://example.com',
35
+ 'http://example.com/',
36
+ 'http://www.example.com/',
37
+ 'http://sub.domain.example.com/',
38
+ 'http://bbc.co.uk',
39
+ 'http://example.com?foo',
40
+ 'http://example.com?url=http://example.com',
41
+ 'http://example.com:8000',
42
+ 'http://www.sub.example.com/page.html?foo=bar&baz=%23#anchor',
43
+ 'http://user:pass@example.com',
44
+ 'http://user:@example.com',
45
+ 'http://example.com/~user',
46
+ 'http://example.xy', # Not a real TLD, but we're fine with anything of 2-6 chars
47
+ 'http://example.museum',
48
+ 'http://1.0.255.249',
49
+ 'http://1.2.3.4:80',
50
+ 'HttP://example.com',
51
+ 'https://example.com',
52
+ 'http://räksmörgås.nu', # IDN
53
+ 'http://xn--rksmrgs-5wao1o.nu', # Punycode
54
+ 'http://example.com.', # Explicit TLD root period
55
+ 'http://example.com./foo'
56
+ ].each do |url|
57
+ @model.homepage = url
58
+ @model.save
59
+ assert !@model.errors.on(:homepage), "#{url.inspect} should have been accepted"
60
+ end
61
+ end
62
+
63
+ def test_should_reject_invalid_urls
64
+ [
65
+ nil, 1, "", " ", "url",
66
+ "www.example.com",
67
+ "http://ex ample.com",
68
+ "http://example.com/foo bar",
69
+ 'http://256.0.0.1',
70
+ 'http://u:u:u@example.com',
71
+ 'http://r?ksmorgas.com',
72
+
73
+ # These can all be valid local URLs, but should not be considered valid
74
+ # for public consumption.
75
+ "http://example",
76
+ "http://example.c",
77
+ 'http://example.toolongtld'
78
+ ].each do |url|
79
+ @model.homepage = url
80
+ @model.save
81
+ assert @model.errors.on(:homepage), "#{url.inspect} should have been rejected"
82
+ end
83
+ end
84
+
85
+ def test_different_defaults_based_on_attribute_name
86
+ @model.homepage = 'x'
87
+ @model.my_UrL_hooray = 'x'
88
+ @model.save
89
+ assert_not_equal ValidatesUrlFormatOf::DEFAULT_MESSAGE, ValidatesUrlFormatOf::DEFAULT_MESSAGE_URL
90
+ assert_equal ValidatesUrlFormatOf::DEFAULT_MESSAGE, @model.errors.on(:homepage)
91
+ assert_equal ValidatesUrlFormatOf::DEFAULT_MESSAGE_URL, @model.errors.on(:my_UrL_hooray)
92
+ end
93
+
94
+ def test_can_override_defaults
95
+ @model.custom_url = 'x'
96
+ @model.save
97
+ assert_equal 'custom message', @model.errors.on(:custom_url)
98
+ end
99
+
100
+ end
@@ -0,0 +1,52 @@
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_url_format_of}
8
+ s.version = "0.1.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Henrik Nyh", "Josh Nichols", "Nicholas Silva"]
12
+ s.date = %q{2011-01-31}
13
+ s.description = %q{Rails plugin that provides a validates_url_format_of method to ActiveRecord models. URLs are validated by regexp.}
14
+ s.email = %q{conickal@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "README.markdown"
17
+ ]
18
+ s.files = [
19
+ "README.markdown",
20
+ "Rakefile",
21
+ "VERSION",
22
+ "init.rb",
23
+ "lib/validates_url_format_of.rb",
24
+ "shoulda_macros/validates_url_format_of_macros.rb",
25
+ "test/validates_url_format_of_test.rb",
26
+ "validates_url_format_of.gemspec"
27
+ ]
28
+ s.homepage = %q{http://github.com/conickal/validates_url_format_of}
29
+ s.require_paths = ["lib"]
30
+ s.rubygems_version = %q{1.3.7}
31
+ s.summary = %q{ActiveRecord URL Validation}
32
+ s.test_files = [
33
+ "test/validates_url_format_of_test.rb"
34
+ ]
35
+
36
+ if s.respond_to? :specification_version then
37
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
38
+ s.specification_version = 3
39
+
40
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
41
+ s.add_runtime_dependency(%q<activerecord>, ["~> 2.3.4"])
42
+ s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
43
+ else
44
+ s.add_dependency(%q<activerecord>, ["~> 2.3.4"])
45
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
46
+ end
47
+ else
48
+ s.add_dependency(%q<activerecord>, ["~> 2.3.4"])
49
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
50
+ end
51
+ end
52
+
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: validates_url_format_of
3
+ version: !ruby/object:Gem::Version
4
+ hash: 25
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 1
10
+ version: 0.1.1
11
+ platform: ruby
12
+ authors:
13
+ - Henrik Nyh
14
+ - Josh Nichols
15
+ - Nicholas Silva
16
+ autorequire:
17
+ bindir: bin
18
+ cert_chain: []
19
+
20
+ date: 2011-01-31 00:00:00 -05:00
21
+ default_executable:
22
+ dependencies:
23
+ - !ruby/object:Gem::Dependency
24
+ name: activerecord
25
+ prerelease: false
26
+ requirement: &id001 !ruby/object:Gem::Requirement
27
+ none: false
28
+ requirements:
29
+ - - ~>
30
+ - !ruby/object:Gem::Version
31
+ hash: 11
32
+ segments:
33
+ - 2
34
+ - 3
35
+ - 4
36
+ version: 2.3.4
37
+ type: :runtime
38
+ version_requirements: *id001
39
+ - !ruby/object:Gem::Dependency
40
+ name: thoughtbot-shoulda
41
+ prerelease: false
42
+ requirement: &id002 !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ hash: 3
48
+ segments:
49
+ - 0
50
+ version: "0"
51
+ type: :development
52
+ version_requirements: *id002
53
+ description: Rails plugin that provides a validates_url_format_of method to ActiveRecord models. URLs are validated by regexp.
54
+ email: conickal@gmail.com
55
+ executables: []
56
+
57
+ extensions: []
58
+
59
+ extra_rdoc_files:
60
+ - README.markdown
61
+ files:
62
+ - README.markdown
63
+ - Rakefile
64
+ - VERSION
65
+ - init.rb
66
+ - lib/validates_url_format_of.rb
67
+ - shoulda_macros/validates_url_format_of_macros.rb
68
+ - test/validates_url_format_of_test.rb
69
+ - validates_url_format_of.gemspec
70
+ has_rdoc: true
71
+ homepage: http://github.com/conickal/validates_url_format_of
72
+ licenses: []
73
+
74
+ post_install_message:
75
+ rdoc_options: []
76
+
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ hash: 3
85
+ segments:
86
+ - 0
87
+ version: "0"
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ hash: 3
94
+ segments:
95
+ - 0
96
+ version: "0"
97
+ requirements: []
98
+
99
+ rubyforge_project:
100
+ rubygems_version: 1.3.7
101
+ signing_key:
102
+ specification_version: 3
103
+ summary: ActiveRecord URL Validation
104
+ test_files:
105
+ - test/validates_url_format_of_test.rb