uri_validator 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/CHANGELOG +16 -0
- data/Gemfile +4 -0
- data/MIT-LICENSE +20 -0
- data/README.textile +24 -0
- data/Rakefile +11 -0
- data/lib/uri_validator.rb +9 -0
- data/test/test_helper.rb +15 -0
- data/test/uri_validator_test.rb +53 -0
- data/validates_as_uri.gemspec +16 -0
- metadata +88 -0
data/CHANGELOG
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
0.2.0 [Fri Jul 22 2011]
|
2
|
+
|
3
|
+
* Rename to uri_validator
|
4
|
+
* Use invalid message instead of invalid_uri
|
5
|
+
|
6
|
+
0.1.1 [Sat Oct 30 2010]
|
7
|
+
|
8
|
+
* Fixed a bug when setting protocols option (thanks webhoernchen)
|
9
|
+
* Added default error message using a rails engine
|
10
|
+
* Added gemspec to repository
|
11
|
+
* Using bundler to manage dependencies
|
12
|
+
* Fixed and refactoring tests
|
13
|
+
|
14
|
+
0.1.0 [Sun Feb 28 2010]
|
15
|
+
|
16
|
+
* First version
|
data/Gemfile
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009-2011 Gabriel Sobrinho
|
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.textile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
h1. ValidatesAsUri
|
2
|
+
|
3
|
+
URI validation for ActiveModel
|
4
|
+
|
5
|
+
h2. Installation
|
6
|
+
|
7
|
+
gem install validates_as_uri
|
8
|
+
|
9
|
+
h2. Usage
|
10
|
+
|
11
|
+
Gemfile:
|
12
|
+
|
13
|
+
<pre>gem 'validate_as_uri'</pre>
|
14
|
+
|
15
|
+
Model:
|
16
|
+
|
17
|
+
<pre>class Repository < ActiveRecord::Base
|
18
|
+
validates :url, :uri => { :protocols => %w(git svn) }
|
19
|
+
validates :homepage, :uri => true # default protocols are %w(http https)
|
20
|
+
end</pre>
|
21
|
+
|
22
|
+
h2. License
|
23
|
+
|
24
|
+
Copyright (c) 2009-2011 Gabriel Sobrinho, released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
class UriValidator < ActiveModel::EachValidator
|
2
|
+
def validate_each(record, attribute, value)
|
3
|
+
protocols = options[:protocols] || %w(http https)
|
4
|
+
|
5
|
+
if value !~ %r{^(#{Array.wrap(protocols).join('|')})://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$}
|
6
|
+
record.errors.add(attribute, options[:message])
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'active_model'
|
4
|
+
require 'uri_validator'
|
5
|
+
require 'ostruct'
|
6
|
+
|
7
|
+
class Repository < OpenStruct
|
8
|
+
include ActiveModel::Validations
|
9
|
+
validates :uri, :uri => { :protocols => %w(git svn) }
|
10
|
+
end
|
11
|
+
|
12
|
+
class Site < OpenStruct
|
13
|
+
include ActiveModel::Validations
|
14
|
+
validates :uri, :uri => true
|
15
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class UriValidatorTest < Test::Unit::TestCase
|
4
|
+
def test_valid_protocols
|
5
|
+
assert_valid_repository 'git://github.com/sobrinho/uri_validator.git'
|
6
|
+
assert_valid_repository 'svn://github.com/sobrinho/uri_validator'
|
7
|
+
assert_valid_site 'http://github.com/'
|
8
|
+
assert_valid_site 'https://github.com/'
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_invalid_protocols
|
12
|
+
assert_invalid_repository 'http://github.com/'
|
13
|
+
assert_invalid_repository 'https://github.com/'
|
14
|
+
assert_invalid_site 'git://github.com/sobrinho/uri_validator.git'
|
15
|
+
assert_invalid_site 'svn://github.com/sobrinho/uri_validator'
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_ip
|
19
|
+
assert_valid_repository 'git://127.0.0.1/sobrinho/uri_validator.git'
|
20
|
+
assert_valid_site 'http://127.0.0.1/sobrinho/uri_validator'
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_port
|
24
|
+
assert_valid_site 'https://github.com:3000/'
|
25
|
+
assert_valid_site 'https://10.0.0.1:3000/'
|
26
|
+
end
|
27
|
+
|
28
|
+
protected
|
29
|
+
|
30
|
+
def assert_valid_repository(uri)
|
31
|
+
assert repository(:uri => uri).valid?
|
32
|
+
end
|
33
|
+
|
34
|
+
def assert_invalid_repository(uri)
|
35
|
+
assert repository(:uri => uri).invalid?
|
36
|
+
end
|
37
|
+
|
38
|
+
def assert_valid_site(uri)
|
39
|
+
assert site(:uri => uri).valid?
|
40
|
+
end
|
41
|
+
|
42
|
+
def assert_invalid_site(uri)
|
43
|
+
assert site(:uri => uri).invalid?
|
44
|
+
end
|
45
|
+
|
46
|
+
def repository(attributes)
|
47
|
+
Repository.new(attributes)
|
48
|
+
end
|
49
|
+
|
50
|
+
def site(attributes)
|
51
|
+
Site.new(attributes)
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = "uri_validator"
|
3
|
+
s.version = "0.2.0"
|
4
|
+
s.platform = Gem::Platform::RUBY
|
5
|
+
s.authors = ["Gabriel Sobrinho"]
|
6
|
+
s.email = ["gabriel.sobrinho@gmail.com"]
|
7
|
+
s.homepage = "http://github.com/sobrinho/uri_validator"
|
8
|
+
s.summary = %q{URI validation for ActiveModel}
|
9
|
+
s.files = `git ls-files`.split("\n")
|
10
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
11
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
12
|
+
s.require_paths = ["lib"]
|
13
|
+
|
14
|
+
s.add_dependency 'activemodel', '>= 3.0'
|
15
|
+
s.add_development_dependency 'rake', '>= 0.8.7'
|
16
|
+
end
|
metadata
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: uri_validator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.2.0
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Gabriel Sobrinho
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-07-22 00:00:00 -03:00
|
14
|
+
default_executable:
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: activemodel
|
18
|
+
prerelease: false
|
19
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
20
|
+
none: false
|
21
|
+
requirements:
|
22
|
+
- - ">="
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: "3.0"
|
25
|
+
type: :runtime
|
26
|
+
version_requirements: *id001
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
prerelease: false
|
30
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
31
|
+
none: false
|
32
|
+
requirements:
|
33
|
+
- - ">="
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: 0.8.7
|
36
|
+
type: :development
|
37
|
+
version_requirements: *id002
|
38
|
+
description:
|
39
|
+
email:
|
40
|
+
- gabriel.sobrinho@gmail.com
|
41
|
+
executables: []
|
42
|
+
|
43
|
+
extensions: []
|
44
|
+
|
45
|
+
extra_rdoc_files: []
|
46
|
+
|
47
|
+
files:
|
48
|
+
- .gitignore
|
49
|
+
- CHANGELOG
|
50
|
+
- Gemfile
|
51
|
+
- MIT-LICENSE
|
52
|
+
- README.textile
|
53
|
+
- Rakefile
|
54
|
+
- lib/uri_validator.rb
|
55
|
+
- test/test_helper.rb
|
56
|
+
- test/uri_validator_test.rb
|
57
|
+
- validates_as_uri.gemspec
|
58
|
+
has_rdoc: true
|
59
|
+
homepage: http://github.com/sobrinho/uri_validator
|
60
|
+
licenses: []
|
61
|
+
|
62
|
+
post_install_message:
|
63
|
+
rdoc_options: []
|
64
|
+
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
none: false
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: "0"
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: "0"
|
79
|
+
requirements: []
|
80
|
+
|
81
|
+
rubyforge_project:
|
82
|
+
rubygems_version: 1.5.2
|
83
|
+
signing_key:
|
84
|
+
specification_version: 3
|
85
|
+
summary: URI validation for ActiveModel
|
86
|
+
test_files:
|
87
|
+
- test/test_helper.rb
|
88
|
+
- test/uri_validator_test.rb
|