validates-format-of-uri 0.0.1
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/.gitignore +1 -0
- data/.rvmrc +1 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +26 -0
- data/LICENSE.md +25 -0
- data/README.md +19 -0
- data/Rakefile +20 -0
- data/lib/mirroring_house/validations/validates_format_of_uri.rb +70 -0
- data/lib/mirroring_house/validations/validates_format_of_uri/version.rb +22 -0
- data/rails/init.rb +1 -0
- data/test/cases/helper.rb +9 -0
- data/test/cases/validations/validates_format_of_uri_test.rb +64 -0
- data/test/config.rb +3 -0
- data/test/models/website.rb +12 -0
- data/validates-format-of-uri.gemspec +26 -0
- metadata +73 -0
data/.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
pkg
|
data/.rvmrc
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
rvm use ruby-1.9.2-p290@mirroring_house_validates_format_of_uri --create
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
validates-format-of-uri (0.0.1)
|
|
5
|
+
activemodel (~> 3.1.0)
|
|
6
|
+
|
|
7
|
+
GEM
|
|
8
|
+
remote: http://rubygems.org/
|
|
9
|
+
specs:
|
|
10
|
+
activemodel (3.1.0)
|
|
11
|
+
activesupport (= 3.1.0)
|
|
12
|
+
bcrypt-ruby (~> 3.0.0)
|
|
13
|
+
builder (~> 3.0.0)
|
|
14
|
+
i18n (~> 0.6)
|
|
15
|
+
activesupport (3.1.0)
|
|
16
|
+
multi_json (~> 1.0)
|
|
17
|
+
bcrypt-ruby (3.0.0)
|
|
18
|
+
builder (3.0.0)
|
|
19
|
+
i18n (0.6.0)
|
|
20
|
+
multi_json (1.0.3)
|
|
21
|
+
|
|
22
|
+
PLATFORMS
|
|
23
|
+
ruby
|
|
24
|
+
|
|
25
|
+
DEPENDENCIES
|
|
26
|
+
validates-format-of-uri!
|
data/LICENSE.md
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
Copyright 2011 3Crowd Technologies, Inc. All rights reserved.
|
|
2
|
+
|
|
3
|
+
Redistribution and use in source and binary forms, with or without modification, are
|
|
4
|
+
permitted provided that the following conditions are met:
|
|
5
|
+
|
|
6
|
+
1. Redistributions of source code must retain the above copyright notice, this list of
|
|
7
|
+
conditions and the following disclaimer.
|
|
8
|
+
|
|
9
|
+
2. Redistributions in binary form must reproduce the above copyright notice, this list
|
|
10
|
+
of conditions and the following disclaimer in the documentation and/or other materials
|
|
11
|
+
provided with the distribution.
|
|
12
|
+
|
|
13
|
+
THIS SOFTWARE IS PROVIDED BY 3Crowd Technologies, Inc. ''AS IS'' AND ANY EXPRESS OR IMPLIED
|
|
14
|
+
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 3Crowd Technologies, Inc. OR
|
|
16
|
+
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
17
|
+
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
18
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
|
19
|
+
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
20
|
+
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
|
21
|
+
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
22
|
+
|
|
23
|
+
The views and conclusions contained in the software and documentation are those of the
|
|
24
|
+
authors and should not be interpreted as representing official policies, either expressed
|
|
25
|
+
or implied, of 3Crowd Technologies, Inc..
|
data/README.md
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Validates format of URI
|
|
2
|
+
=======================
|
|
3
|
+
An ActiveModel field validator using the URI parsing library included with ruby
|
|
4
|
+
to validate that a given field matches the format of a URI as given by RFC 2396.
|
|
5
|
+
See URI#parse for more information.
|
|
6
|
+
|
|
7
|
+
Installation
|
|
8
|
+
------------
|
|
9
|
+
Add the following to your Gemfile:
|
|
10
|
+
gem 'validates-format-of-uri', '~> 0.0.1', :require => 'mirroring_house/validations/validates_format_of_uri'
|
|
11
|
+
|
|
12
|
+
Restrictions
|
|
13
|
+
------------
|
|
14
|
+
* Does not validate existance of domain
|
|
15
|
+
* Does not validate that the domain is actually registered
|
|
16
|
+
|
|
17
|
+
Credits
|
|
18
|
+
-------
|
|
19
|
+
Many thanks to the inspiration provided by https://gist.github.com/102138
|
data/Rakefile
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
dir = File.dirname(__FILE__)
|
|
2
|
+
|
|
3
|
+
require 'rake/testtask'
|
|
4
|
+
|
|
5
|
+
Rake::TestTask.new do |t|
|
|
6
|
+
t.libs << 'test'
|
|
7
|
+
t.test_files = Dir.glob("#{dir}/test/cases/**/*_test.rb").sort
|
|
8
|
+
t.warning = true
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
task :default => [:clobber, :test, :package]
|
|
12
|
+
|
|
13
|
+
require 'rake/packagetask'
|
|
14
|
+
require 'rubygems/package_task'
|
|
15
|
+
|
|
16
|
+
spec = eval(File.read("#{dir}/validates-format-of-uri.gemspec"))
|
|
17
|
+
|
|
18
|
+
Gem::PackageTask.new(spec) do |p|
|
|
19
|
+
p.gem_spec = spec
|
|
20
|
+
end
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
require 'uri'
|
|
2
|
+
require 'active_model'
|
|
3
|
+
|
|
4
|
+
module MirroringHouse
|
|
5
|
+
module Validations
|
|
6
|
+
|
|
7
|
+
module ValidatesFormatOfURI
|
|
8
|
+
extend ActiveSupport::Concern
|
|
9
|
+
|
|
10
|
+
module InstanceMethods
|
|
11
|
+
|
|
12
|
+
class URIFormatValidator < ActiveModel::EachValidator
|
|
13
|
+
|
|
14
|
+
MESSAGES = { :scheme => :invalid_scheme, :host_blank => :host_cannot_be_blank, :general_error => :unable_to_parse }.freeze
|
|
15
|
+
SCHEMES = [ :http, :https ].freeze
|
|
16
|
+
ALLOW_BLANK_HOST = false.freeze
|
|
17
|
+
|
|
18
|
+
RESERVED_OPTIONS = []
|
|
19
|
+
|
|
20
|
+
def initialize(options)
|
|
21
|
+
raise ArgumentError, ":schemes must be an Array of Symbols describing allowed schemes" unless options[:schemes].nil? || (options[:schemes].is_a?(Array) && options[:schemes].all?{|a| a.is_a?(Symbol)})
|
|
22
|
+
options[:schemes] ||= SCHEMES
|
|
23
|
+
options[:allow_blank_host] ||= ALLOW_BLANK_HOST
|
|
24
|
+
super
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def validate_each(record, attribute, value)
|
|
28
|
+
|
|
29
|
+
errors_options = options.except(*RESERVED_OPTIONS)
|
|
30
|
+
|
|
31
|
+
begin
|
|
32
|
+
|
|
33
|
+
uri = URI.parse(value)
|
|
34
|
+
|
|
35
|
+
record.errors.add(attribute, MESSAGES[:host_blank], errors_options) if uri.host.blank? && !options[:allow_blank_host]
|
|
36
|
+
record.errors.add(attribute, MESSAGES[:scheme_invalid], errors_options) unless options[:schemes].map{|scheme| scheme.to_s}.include?(uri.scheme)
|
|
37
|
+
|
|
38
|
+
rescue URI::InvalidURIError => e
|
|
39
|
+
|
|
40
|
+
record.errors.add(attribute, MESSAGES[:general_error], errors_options)
|
|
41
|
+
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
module ClassMethods
|
|
50
|
+
|
|
51
|
+
# Validates that the specified attribute parses correctly as a URI, and matches the restrictions specified.
|
|
52
|
+
#
|
|
53
|
+
# class Person < ActiveRecord::Base
|
|
54
|
+
# validates_format_of_uri :website
|
|
55
|
+
# end
|
|
56
|
+
#
|
|
57
|
+
# Configuration options:
|
|
58
|
+
# * <tt>:schemes</tt> - An Array of Symbols describing allowed URI schemes (e.g. [ :http, :https ], to allow https:// and http:// URIs)
|
|
59
|
+
#
|
|
60
|
+
def validates_format_of_uri(*attr_names)
|
|
61
|
+
validates_with InstanceMethods::URIFormatValidator, _merge_attributes(attr_names)
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
ActiveModel::Validations.send(:include, MirroringHouse::Validations::ValidatesFormatOfURI)
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
require 'uri/http'
|
|
2
|
+
|
|
3
|
+
module MirroringHouse
|
|
4
|
+
module Validations
|
|
5
|
+
|
|
6
|
+
module ValidatesFormatOfURI
|
|
7
|
+
|
|
8
|
+
module Version
|
|
9
|
+
MAJOR = '0'
|
|
10
|
+
MINOR = '0'
|
|
11
|
+
PATCH = '1'
|
|
12
|
+
|
|
13
|
+
def self.to_standard_version_s
|
|
14
|
+
[MAJOR, MINOR, PATCH].join('.')
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
end
|
|
22
|
+
end
|
data/rails/init.rb
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
require 'mirroring_house/validations/validates_format_of_uri'
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
require 'cases/helper'
|
|
2
|
+
|
|
3
|
+
require 'models/website'
|
|
4
|
+
|
|
5
|
+
class URIFormatValidationTest < ActiveModel::TestCase
|
|
6
|
+
|
|
7
|
+
def teardown
|
|
8
|
+
Website.reset_callbacks(:validate)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def test_validation_of_uri_outside_of_given_scheme_set_fails_when_scheme_not_in_given_schemes
|
|
12
|
+
Website.validates_format_of_uri :uri, :schemes => [ :http ]
|
|
13
|
+
site = Website.new(:uri => "https://example.com")
|
|
14
|
+
assert site.invalid?, "https scheme was not invalid when only http was specified as the only valid scheme. Site validations: #{site.errors.inspect} "
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def test_validation_of_uri_within_given_scheme_set_succeeds_when_scheme_in_given_schemes
|
|
18
|
+
Website.validates_format_of_uri :uri, :schemes => [ :http ]
|
|
19
|
+
site = Website.new(:uri => "http://example.com")
|
|
20
|
+
assert site.valid?, "http was not a valid scheme when http was specified as a valid scheme. Site validations: #{site.errors.inspect}"
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def test_validation_of_uri_succeeds_when_scheme_is_in_default_scheme_set
|
|
24
|
+
uri = URI.parse("http://example.com")
|
|
25
|
+
Website.validates_format_of_uri :uri
|
|
26
|
+
site = Website.new(:uri => uri.to_s)
|
|
27
|
+
assert MirroringHouse::Validations::ValidatesFormatOfURI::InstanceMethods::URIFormatValidator::SCHEMES.include?(uri.scheme.to_sym), "Test URI scheme was not in validator default schemes"
|
|
28
|
+
assert site.valid?, "http was not validated as a default valid scheme. Site validations: #{site.errors.inspect}"
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def test_validation_of_uri_fails_when_scheme_is_not_in_default_scheme_set
|
|
32
|
+
uri = URI.parse("telnet://example.com")
|
|
33
|
+
Website.validates_format_of_uri :uri
|
|
34
|
+
site = Website.new(:uri => uri.to_s)
|
|
35
|
+
refute MirroringHouse::Validations::ValidatesFormatOfURI::InstanceMethods::URIFormatValidator::SCHEMES.include?(uri.scheme.to_sym), "Test URI scheme was included in validator default schemes"
|
|
36
|
+
refute site.valid?, "telnet was not invalidated as a default invalid scheme. Site validations: #{site.errors.inspect}"
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def test_validation_of_uri_fails_when_host_is_blank_if_allow_blank_host_is_not_set
|
|
40
|
+
uri = URI.parse("http:///")
|
|
41
|
+
Website.validates_format_of_uri :uri, :allow_blank_host => false
|
|
42
|
+
site = Website.new(:uri => uri.to_s)
|
|
43
|
+
assert MirroringHouse::Validations::ValidatesFormatOfURI::InstanceMethods::URIFormatValidator::SCHEMES.include?(uri.scheme.to_sym), "Test URI scheme was not in validator default schemes"
|
|
44
|
+
refute site.valid?, "blank host was not flagged as invalid"
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def test_validation_of_uri_succeeds_when_host_is_blank_if_allow_blank_host_is_set
|
|
48
|
+
uri = URI.parse("http:///")
|
|
49
|
+
Website.validates_format_of_uri :uri, :allow_blank_host => true
|
|
50
|
+
site = Website.new(:uri => uri.to_s)
|
|
51
|
+
assert MirroringHouse::Validations::ValidatesFormatOfURI::InstanceMethods::URIFormatValidator::SCHEMES.include?(uri.scheme.to_sym), "Test URI scheme was not in validator default schemes"
|
|
52
|
+
assert site.valid?, "blank host was not accepted even though allow_blank_host option was true"
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def test_validation_of_uri_fails_when_host_is_blank_if_allow_blank_host_is_unspecified
|
|
56
|
+
uri = URI.parse("http:///")
|
|
57
|
+
Website.validates_format_of_uri :uri
|
|
58
|
+
site = Website.new(:uri => uri.to_s)
|
|
59
|
+
assert MirroringHouse::Validations::ValidatesFormatOfURI::InstanceMethods::URIFormatValidator::SCHEMES.include?(uri.scheme.to_sym), "Test URI scheme was not in validator default schemes"
|
|
60
|
+
refute site.valid?, "blank host was not flagged as invalid"
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
end
|
data/test/config.rb
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
|
2
|
+
require File.expand_path('./lib/mirroring_house/validations/validates_format_of_uri/version.rb')
|
|
3
|
+
|
|
4
|
+
Gem::Specification.new do |gem|
|
|
5
|
+
|
|
6
|
+
gem.name = 'validates-format-of-uri'
|
|
7
|
+
gem.version = MirroringHouse::Validations::ValidatesFormatOfURI::Version.to_standard_version_s
|
|
8
|
+
|
|
9
|
+
gem.authors = ["Justin Lynn", "3Crowd Technologies, Inc. (Sponsor)"]
|
|
10
|
+
gem.email = ["eng@3crowd.com"]
|
|
11
|
+
|
|
12
|
+
gem.summary = %q{An ActiveModel wrapper for URI#parse}
|
|
13
|
+
gem.description = %q{An ActiveModel validator utilizing the built in URI parse methods to provide a rich semantic API for URI validation}
|
|
14
|
+
|
|
15
|
+
gem.homepage = 'https://github.com/mirroring-house/validates_format_of_uri'
|
|
16
|
+
|
|
17
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map{|f| File.basename f }
|
|
18
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
|
19
|
+
gem.files = `git ls-files`.split("\n")
|
|
20
|
+
|
|
21
|
+
gem.require_paths = ['lib']
|
|
22
|
+
|
|
23
|
+
gem.required_rubygems_version = Gem::Requirement.new('>= 1.3.6')
|
|
24
|
+
|
|
25
|
+
gem.add_dependency 'activemodel', '~> 3.1.0'
|
|
26
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: validates-format-of-uri
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
prerelease:
|
|
6
|
+
platform: ruby
|
|
7
|
+
authors:
|
|
8
|
+
- Justin Lynn
|
|
9
|
+
- 3Crowd Technologies, Inc. (Sponsor)
|
|
10
|
+
autorequire:
|
|
11
|
+
bindir: bin
|
|
12
|
+
cert_chain: []
|
|
13
|
+
date: 2011-09-12 00:00:00.000000000Z
|
|
14
|
+
dependencies:
|
|
15
|
+
- !ruby/object:Gem::Dependency
|
|
16
|
+
name: activemodel
|
|
17
|
+
requirement: &8091000 !ruby/object:Gem::Requirement
|
|
18
|
+
none: false
|
|
19
|
+
requirements:
|
|
20
|
+
- - ~>
|
|
21
|
+
- !ruby/object:Gem::Version
|
|
22
|
+
version: 3.1.0
|
|
23
|
+
type: :runtime
|
|
24
|
+
prerelease: false
|
|
25
|
+
version_requirements: *8091000
|
|
26
|
+
description: An ActiveModel validator utilizing the built in URI parse methods to
|
|
27
|
+
provide a rich semantic API for URI validation
|
|
28
|
+
email:
|
|
29
|
+
- eng@3crowd.com
|
|
30
|
+
executables: []
|
|
31
|
+
extensions: []
|
|
32
|
+
extra_rdoc_files: []
|
|
33
|
+
files:
|
|
34
|
+
- .gitignore
|
|
35
|
+
- .rvmrc
|
|
36
|
+
- Gemfile
|
|
37
|
+
- Gemfile.lock
|
|
38
|
+
- LICENSE.md
|
|
39
|
+
- README.md
|
|
40
|
+
- Rakefile
|
|
41
|
+
- lib/mirroring_house/validations/validates_format_of_uri.rb
|
|
42
|
+
- lib/mirroring_house/validations/validates_format_of_uri/version.rb
|
|
43
|
+
- rails/init.rb
|
|
44
|
+
- test/cases/helper.rb
|
|
45
|
+
- test/cases/validations/validates_format_of_uri_test.rb
|
|
46
|
+
- test/config.rb
|
|
47
|
+
- test/models/website.rb
|
|
48
|
+
- validates-format-of-uri.gemspec
|
|
49
|
+
homepage: https://github.com/mirroring-house/validates_format_of_uri
|
|
50
|
+
licenses: []
|
|
51
|
+
post_install_message:
|
|
52
|
+
rdoc_options: []
|
|
53
|
+
require_paths:
|
|
54
|
+
- lib
|
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
56
|
+
none: false
|
|
57
|
+
requirements:
|
|
58
|
+
- - ! '>='
|
|
59
|
+
- !ruby/object:Gem::Version
|
|
60
|
+
version: '0'
|
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
62
|
+
none: false
|
|
63
|
+
requirements:
|
|
64
|
+
- - ! '>='
|
|
65
|
+
- !ruby/object:Gem::Version
|
|
66
|
+
version: 1.3.6
|
|
67
|
+
requirements: []
|
|
68
|
+
rubyforge_project:
|
|
69
|
+
rubygems_version: 1.8.6
|
|
70
|
+
signing_key:
|
|
71
|
+
specification_version: 3
|
|
72
|
+
summary: An ActiveModel wrapper for URI#parse
|
|
73
|
+
test_files: []
|