rails_uri_validator 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ gem "activemodel", ">= 3.0.0"
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 "bundler", "~> 1.0.0"
10
+ gem "jeweler", "~> 1.5.2"
11
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,24 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activemodel (3.0.3)
5
+ activesupport (= 3.0.3)
6
+ builder (~> 2.1.2)
7
+ i18n (~> 0.4)
8
+ activesupport (3.0.3)
9
+ builder (2.1.2)
10
+ git (1.2.5)
11
+ i18n (0.5.0)
12
+ jeweler (1.5.2)
13
+ bundler (~> 1.0.0)
14
+ git (>= 1.2.5)
15
+ rake
16
+ rake (0.8.7)
17
+
18
+ PLATFORMS
19
+ ruby
20
+
21
+ DEPENDENCIES
22
+ activemodel (>= 3.0.0)
23
+ bundler (~> 1.0.0)
24
+ jeweler (~> 1.5.2)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Marco Scholl
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.rdoc ADDED
@@ -0,0 +1,43 @@
1
+ = rails_uri_validator
2
+
3
+ == Installation
4
+ Add the following line to your Gemfile
5
+ gem "rails_uri_validator"
6
+
7
+ If you want to use it as plugin
8
+ rails plugin install git://github.com/traxanos/rails_uri_validator.git
9
+
10
+ == Using
11
+ Use to validate uri
12
+ validates :uri, :uri => true
13
+
14
+ Allow only http and https uris (http://examples.net)
15
+ validates :uri, :uri => { :schemes => [:http, :https] }
16
+
17
+ Allow only mailto uris (mailto:test@examples.net)
18
+ validates :uri, :uri => { :schemes => :mailto }
19
+
20
+ Allow only http and https uris and addition custom validation to forbidden uris with user infos
21
+ validates :uri, :uri => { :schemes => [:http, :https], :custom => Proc.new { |uri| uri.userinfo == nil} }
22
+
23
+ == Features
24
+
25
+ * uri validation based on ruby's uri from stdlib
26
+ * schemes restrictions
27
+ * custom support
28
+ * tests for validator
29
+
30
+ == Contributing to rails_email_validator
31
+
32
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
33
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
34
+ * Fork the project
35
+ * Start a feature/bugfix branch
36
+ * Commit and push until you are happy with your contribution
37
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
38
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
39
+
40
+ == Copyright
41
+
42
+ Copyright (c) 2010 Marco Scholl. See LICENSE.txt for further details.
43
+
data/Rakefile ADDED
@@ -0,0 +1,46 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "rails_uri_validator"
16
+ gem.homepage = "http://github.com/traxanos/rails_uri_validator"
17
+ gem.license = "MIT"
18
+ gem.summary = %Q{an uri validator for rails}
19
+ gem.description = %Q{a class to validate uris. it builded for rails}
20
+ gem.email = "develop@marco-scholl.de"
21
+ gem.authors = ["Marco Scholl"]
22
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
23
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
24
+ gem.add_runtime_dependency 'activemodel', '>= 3.0.0'
25
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rake/testtask'
30
+ Rake::TestTask.new(:test) do |test|
31
+ test.libs << 'lib' << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+
36
+ task :default => :test
37
+
38
+ require 'rake/rdoctask'
39
+ Rake::RDocTask.new do |rdoc|
40
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
41
+
42
+ rdoc.rdoc_dir = 'rdoc'
43
+ rdoc.title = "rails_uri_validator #{version}"
44
+ rdoc.rdoc_files.include('README*')
45
+ rdoc.rdoc_files.include('lib/**/*.rb')
46
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'rails_uri_validator'
@@ -0,0 +1,42 @@
1
+ # encoding:utf-8
2
+
3
+ require 'active_model'
4
+ require 'uri'
5
+
6
+ # Validator for uri
7
+ class UriValidator < ActiveModel::EachValidator
8
+
9
+ # main validator for uri
10
+ def validate_each(record, attribute, value)
11
+
12
+ unless value.blank?
13
+ # pre var
14
+ valid = true
15
+
16
+ begin
17
+ uri = URI.parse(value)
18
+
19
+ # need schemes validation?
20
+ unless options[:schemes].nil?
21
+ if options[:schemes].is_a? Array
22
+ valid = false unless options[:schemes].include? uri.scheme.to_sym
23
+ else
24
+ valid = false unless options[:schemes] == uri.scheme.to_sym
25
+ end
26
+ end
27
+
28
+ # custom
29
+ if options[:custom].is_a? Proc
30
+ valid = false unless options[:custom].call(uri)
31
+ end
32
+
33
+ rescue
34
+ valid = false
35
+ end
36
+
37
+ # ip valid
38
+ record.errors.add(attribute, :invalid) unless valid
39
+ end
40
+ end
41
+
42
+ 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{rails_uri_validator}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Marco Scholl"]
12
+ s.date = %q{2011-01-22}
13
+ s.description = %q{a class to validate uris. it builded for rails}
14
+ s.email = %q{develop@marco-scholl.de}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ "Gemfile",
22
+ "Gemfile.lock",
23
+ "LICENSE.txt",
24
+ "README.rdoc",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "init.rb",
28
+ "lib/rails_uri_validator.rb",
29
+ "rails_uri_validator.gemspec",
30
+ "test/helper.rb",
31
+ "test/test_rails_uri_validator.rb"
32
+ ]
33
+ s.homepage = %q{http://github.com/traxanos/rails_uri_validator}
34
+ s.licenses = ["MIT"]
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = %q{1.3.7}
37
+ s.summary = %q{an uri validator for rails}
38
+ s.test_files = [
39
+ "test/helper.rb",
40
+ "test/test_rails_uri_validator.rb"
41
+ ]
42
+
43
+ if s.respond_to? :specification_version then
44
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
45
+ s.specification_version = 3
46
+
47
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
48
+ s.add_runtime_dependency(%q<activemodel>, [">= 3.0.0"])
49
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
50
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
51
+ s.add_runtime_dependency(%q<activemodel>, [">= 3.0.0"])
52
+ else
53
+ s.add_dependency(%q<activemodel>, [">= 3.0.0"])
54
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
55
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
56
+ s.add_dependency(%q<activemodel>, [">= 3.0.0"])
57
+ end
58
+ else
59
+ s.add_dependency(%q<activemodel>, [">= 3.0.0"])
60
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
61
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
62
+ s.add_dependency(%q<activemodel>, [">= 3.0.0"])
63
+ end
64
+ end
65
+
data/test/helper.rb ADDED
@@ -0,0 +1,19 @@
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 'test/unit'
13
+
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
16
+ require 'rails_uri_validator'
17
+
18
+ class Test::Unit::TestCase
19
+ end
@@ -0,0 +1,136 @@
1
+ require 'helper'
2
+
3
+ class ValidateUri
4
+ include ActiveModel::Validations
5
+ attr_accessor :uri
6
+ end
7
+
8
+ class ValidateDefaultUri < ValidateUri
9
+ validates :uri, :uri => true
10
+ end
11
+
12
+ class ValidateHttpUri < ValidateUri
13
+ validates :uri, :uri => {:schemes => [:http, :https]}
14
+ end
15
+
16
+ class ValidateMailtoUri < ValidateUri
17
+ validates :uri, :uri => {:schemes => :mailto}
18
+ end
19
+
20
+ class ValidateCustomUri < ValidateUri
21
+ validates :uri, :uri => {:schemes => [:http, :https], :custom => Proc.new { |uri| uri.userinfo == nil }}
22
+ end
23
+
24
+ class TestRailsUriValidator < Test::Unit::TestCase
25
+ def test_valid_uri
26
+ instance = ValidateDefaultUri.new
27
+ [
28
+ "http://user:pass@www.web.de:80/directory/?querystring#anker",
29
+ "https://user:pass@www.web.de:80/directory/?querystring#anker",
30
+ "http://www.web.de:80/directory/?querystring#anker",
31
+ "https://www.web.de:80/directory/?querystring#anker",
32
+ "http://www.web.de/directory/?querystring#anker",
33
+ "https://www.web.de/directory/?querystring#anker",
34
+ "http://www.web.de/directory",
35
+ "https://www.web.de/directory",
36
+ "http://de.wikipedia.org/wiki/Uniform_Resource_Identifier",
37
+ "ftp://ftp.is.co.za/rfc/rfc1808.txt",
38
+ "file:///C:/Dokumente%20und%20Einstellungen/Benutzer/Desktop/Uniform%20Resource%20Identifier.html",
39
+ "geo:48.33,14.122;u=22.5",
40
+ "ldap://[2001:db8::7]/c=GB?objectClass?one",
41
+ "gopher://gopher.floodgap.com",
42
+ "mailto:John.Doe@example.com",
43
+ "sip:911@pbx.mycompany.com",
44
+ "news:comp.infosystems.www.servers.unix",
45
+ # "data:text/plain;charset=iso-8859-7,%be%fg%be", not supported
46
+ "tel:+1-816-555-1212",
47
+ "telnet://192.0.2.16:80/",
48
+ "urn:oasis:names:specification:docbook:dtd:xml:4.1.2"
49
+ ].each do |uri|
50
+ instance.uri = uri
51
+ assert instance.valid?, uri
52
+ end
53
+ end
54
+
55
+ def test_http_and_https_only
56
+ instance = ValidateHttpUri.new
57
+ [
58
+ "http://user:pass@www.web.de:80/directory/?querystring#anker",
59
+ "https://user:pass@www.web.de:80/directory/?querystring#anker",
60
+ "http://www.web.de:80/directory/?querystring#anker",
61
+ "https://www.web.de:80/directory/?querystring#anker",
62
+ "http://www.web.de/directory/?querystring#anker",
63
+ "https://www.web.de/directory/?querystring#anker",
64
+ "http://www.web.de/directory",
65
+ "https://www.web.de/directory",
66
+ "http://de.wikipedia.org/wiki/Uniform_Resource_Identifier"
67
+ ].each do |uri|
68
+ instance.uri = uri
69
+ assert instance.valid?, uri
70
+ end
71
+
72
+ [
73
+ "ftp://ftp.is.co.za/rfc/rfc1808.txt",
74
+ "file:///C:/Dokumente%20und%20Einstellungen/Benutzer/Desktop/Uniform%20Resource%20Identifier.html",
75
+ "geo:48.33,14.122;u=22.5",
76
+ "ldap://[2001:db8::7]/c=GB?objectClass?one",
77
+ "gopher://gopher.floodgap.com",
78
+ "mailto:John.Doe@example.com",
79
+ "sip:911@pbx.mycompany.com",
80
+ "news:comp.infosystems.www.servers.unix",
81
+ "tel:+1-816-555-1212",
82
+ "telnet://192.0.2.16:80/",
83
+ "urn:oasis:names:specification:docbook:dtd:xml:4.1.2"
84
+ ].each do |uri|
85
+ instance.uri = uri
86
+ assert !instance.valid?, uri
87
+ end
88
+ end
89
+
90
+ def test_mailto
91
+ instance = ValidateMailtoUri.new
92
+ [
93
+ "mailto:test@example.net",
94
+ "mailto:test@example.net?subject=Test"
95
+ ].each do |uri|
96
+ instance.uri = uri
97
+ assert instance.valid?, uri
98
+ end
99
+
100
+ [
101
+ "ftp://ftp.is.co.za/rfc/rfc1808.txt",
102
+ "file:///C:/Dokumente%20und%20Einstellungen/Benutzer/Desktop/Uniform%20Resource%20Identifier.html",
103
+ "geo:48.33,14.122;u=22.5",
104
+ "ldap://[2001:db8::7]/c=GB?objectClass?one",
105
+ "gopher://gopher.floodgap.com",
106
+ "sip:911@pbx.mycompany.com",
107
+ "news:comp.infosystems.www.servers.unix",
108
+ "tel:+1-816-555-1212",
109
+ "telnet://192.0.2.16:80/",
110
+ "urn:oasis:names:specification:docbook:dtd:xml:4.1.2"
111
+ ].each do |uri|
112
+ instance.uri = uri
113
+ assert !instance.valid?, uri
114
+ end
115
+ end
116
+
117
+ def test_custom
118
+ instance = ValidateCustomUri.new
119
+ [
120
+ "http://example.net",
121
+ "http://example.net/test"
122
+ ].each do |uri|
123
+ instance.uri = uri
124
+ assert instance.valid?, uri
125
+ end
126
+
127
+ [
128
+ "http://test@example.net",
129
+ "http://test:test@example.net/test"
130
+ ].each do |uri|
131
+ instance.uri = uri
132
+ assert !instance.valid?, uri
133
+ end
134
+ end
135
+
136
+ end
metadata ADDED
@@ -0,0 +1,137 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails_uri_validator
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Marco Scholl
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-01-22 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: activemodel
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 3
29
+ - 0
30
+ - 0
31
+ version: 3.0.0
32
+ type: :runtime
33
+ prerelease: false
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: bundler
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ~>
41
+ - !ruby/object:Gem::Version
42
+ segments:
43
+ - 1
44
+ - 0
45
+ - 0
46
+ version: 1.0.0
47
+ type: :development
48
+ prerelease: false
49
+ version_requirements: *id002
50
+ - !ruby/object:Gem::Dependency
51
+ name: jeweler
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ~>
56
+ - !ruby/object:Gem::Version
57
+ segments:
58
+ - 1
59
+ - 5
60
+ - 2
61
+ version: 1.5.2
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: *id003
65
+ - !ruby/object:Gem::Dependency
66
+ name: activemodel
67
+ requirement: &id004 !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ segments:
73
+ - 3
74
+ - 0
75
+ - 0
76
+ version: 3.0.0
77
+ type: :runtime
78
+ prerelease: false
79
+ version_requirements: *id004
80
+ description: a class to validate uris. it builded for rails
81
+ email: develop@marco-scholl.de
82
+ executables: []
83
+
84
+ extensions: []
85
+
86
+ extra_rdoc_files:
87
+ - LICENSE.txt
88
+ - README.rdoc
89
+ files:
90
+ - .document
91
+ - Gemfile
92
+ - Gemfile.lock
93
+ - LICENSE.txt
94
+ - README.rdoc
95
+ - Rakefile
96
+ - VERSION
97
+ - init.rb
98
+ - lib/rails_uri_validator.rb
99
+ - rails_uri_validator.gemspec
100
+ - test/helper.rb
101
+ - test/test_rails_uri_validator.rb
102
+ has_rdoc: true
103
+ homepage: http://github.com/traxanos/rails_uri_validator
104
+ licenses:
105
+ - MIT
106
+ post_install_message:
107
+ rdoc_options: []
108
+
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ none: false
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ hash: 238538861103991173
117
+ segments:
118
+ - 0
119
+ version: "0"
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ segments:
126
+ - 0
127
+ version: "0"
128
+ requirements: []
129
+
130
+ rubyforge_project:
131
+ rubygems_version: 1.3.7
132
+ signing_key:
133
+ specification_version: 3
134
+ summary: an uri validator for rails
135
+ test_files:
136
+ - test/helper.rb
137
+ - test/test_rails_uri_validator.rb