validates_as_uri 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009-2010 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
+ You can install as gem:
8
+
9
+ <pre>gem "validates_as_uri"</pre>
10
+
11
+ Or as plugin:
12
+
13
+ <pre>$ rails plugin install git://github.com/sobrinho/validates_as_uri.git</pre>
14
+
15
+ h2. Usage
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-2010 Gabriel Sobrinho, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,24 @@
1
+ require 'rake/testtask'
2
+
3
+ desc 'Default: run unit tests.'
4
+ task :default => :test
5
+
6
+ desc 'Test the validates_as_uri plugin.'
7
+ Rake::TestTask.new(:test) do |t|
8
+ t.libs << 'test'
9
+ t.pattern = 'test/**/*_test.rb'
10
+ end
11
+
12
+ begin
13
+ require 'jeweler'
14
+ Jeweler::Tasks.new do |gemspec|
15
+ gemspec.name = "validates_as_uri"
16
+ gemspec.summary = "URI validation for ActiveModel"
17
+ gemspec.email = "gabriel.sobrinho@gmail.com"
18
+ gemspec.homepage = "http://github.com/sobrinho/validates_as_uri"
19
+ gemspec.authors = ["Gabriel Sobrinho"]
20
+ end
21
+ Jeweler::GemcutterTasks.new
22
+ rescue LoadError
23
+ puts "Jeweler not available. Install it with: gem install jeweler"
24
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'validates_as_uri'
@@ -0,0 +1,9 @@
1
+ class UriValidator < ActiveModel::EachValidator
2
+ def validate_each(record, attribute, value)
3
+ options[:protocols] ||= %w(http https)
4
+
5
+ if value !~ %r{^(#{Array(options[:protocols]).join('|')})://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$}
6
+ record.errors.add(attribute, options[:message])
7
+ end
8
+ end
9
+ end
@@ -0,0 +1 @@
1
+ require 'validates_as_uri/uri_validator'
@@ -0,0 +1,17 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'active_support'
4
+ require 'active_record'
5
+ require 'validates_as_uri/uri_validator'
6
+
7
+ # create a temporary database
8
+ ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => ':memory:'
9
+
10
+ silence_stream(STDOUT) do
11
+ ActiveRecord::Schema.define do
12
+ create_table :repositories do |t|
13
+ t.string :url
14
+ t.string :homepage
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,32 @@
1
+ require 'test_helper'
2
+
3
+ class ValidatesUriCpfTest < ActiveSupport::TestCase
4
+ class Repository < ActiveRecord::Base
5
+ validates :url, :uri => { :protocols => %w(git svn) }
6
+ validates :homepage, :uri => true
7
+ end
8
+
9
+ test "blank values" do
10
+ assert_equal false, Repository.new(:url => '', :homepage => '').valid?
11
+ assert_equal false, Repository.new(:url => false, :homepage => false).valid?
12
+ assert_equal false, Repository.new(:url => nil, :homepage => nil).valid?
13
+ end
14
+
15
+ test "valid protocols" do
16
+ assert Repository.new(:url => 'git://github.com/sobrinho/validates_as_uri.git',
17
+ :homepage => 'http://github.com/sobrinho/validates_as_uri').valid?
18
+
19
+ assert Repository.new(:url => 'svn://repository.com/sobrinho/validates_as_uri',
20
+ :homepage => 'http://github.com/sobrinho/validates_as_uri').valid?
21
+ end
22
+
23
+ test "invalid protocols" do
24
+ assert_equal false, Repository.new(:url => 'http://github.com/sobrinho/validates_as_uri.git',
25
+ :homepage => 'http://github.com/sobrinho/validates_as_uri').valid?
26
+ end
27
+
28
+ test "ip and port" do
29
+ assert Repository.new(:url => 'git://127.0.0.1/sobrinho/validates_as_uri.git',
30
+ :homepage => 'https://127.0.0.1:3000/sobrinho/validates_as_uri').valid?
31
+ end
32
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: validates_as_uri
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
+ - Gabriel Sobrinho
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-02-28 00:00:00 -03:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description:
22
+ email: gabriel.sobrinho@gmail.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - README.textile
29
+ files:
30
+ - MIT-LICENSE
31
+ - README.textile
32
+ - Rakefile
33
+ - VERSION
34
+ - init.rb
35
+ - lib/validates_as_uri.rb
36
+ - lib/validates_as_uri/uri_validator.rb
37
+ - test/test_helper.rb
38
+ - test/validates_as_uri_test.rb
39
+ has_rdoc: true
40
+ homepage: http://github.com/sobrinho/validates_as_uri
41
+ licenses: []
42
+
43
+ post_install_message:
44
+ rdoc_options:
45
+ - --charset=UTF-8
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ segments:
53
+ - 0
54
+ version: "0"
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ segments:
60
+ - 0
61
+ version: "0"
62
+ requirements: []
63
+
64
+ rubyforge_project:
65
+ rubygems_version: 1.3.6
66
+ signing_key:
67
+ specification_version: 3
68
+ summary: URI validation for ActiveModel
69
+ test_files:
70
+ - test/test_helper.rb
71
+ - test/validates_as_uri_test.rb