activemodel-url_validator 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ee022d1df731078c7581ee23a470e85e3f0c8ac4
4
+ data.tar.gz: 2f222fe4833bd0ad91bb699751a171bc0bfa6256
5
+ SHA512:
6
+ metadata.gz: 97e028792ea3f33e250ce41b8b1692237250f6210c1faa326bdfed16f8125122d795eaa02dc9cf68444418869a7ccda1c74e52f6c5112f347fa5387c1f905256
7
+ data.tar.gz: cc1138bd0d63c4e38e5330f6991376408045914abd76f535c23e091eaa936033f3e589e42526f4dba29e775cff3713a12d83d122b9a2109e30138f22b7e4af9a
data/.gitignore ADDED
@@ -0,0 +1,34 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/tmp/
9
+ /test/version_tmp/
10
+ /tmp/
11
+
12
+ ## Specific to RubyMotion:
13
+ .dat*
14
+ .repl_history
15
+ build/
16
+
17
+ ## Documentation cache and generated files:
18
+ /.yardoc/
19
+ /_yardoc/
20
+ /doc/
21
+ /rdoc/
22
+
23
+ ## Environment normalisation:
24
+ /.bundle/
25
+ /lib/bundler/man/
26
+
27
+ # for a library or gem, you might want to ignore these files since the code is
28
+ # intended to run in multiple environments; otherwise, check them in:
29
+ Gemfile.lock
30
+ .ruby-version
31
+ .ruby-gemset
32
+
33
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
34
+ .rvmrc
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in activemodel-url_validator.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,42 @@
1
+ # activemodel-url_validator
2
+
3
+ [![Build Status](https://travis-ci.org/increments/activemodel-url_validator.svg?branch=master)](https://travis-ci.org/increments/activemodel-url_validator)
4
+
5
+ ## Usage
6
+
7
+ Add to your Gemfile:
8
+
9
+ ```rb
10
+ gem 'activemodel-url_validator'
11
+ ```
12
+
13
+ Run:
14
+
15
+ ```
16
+ bundle install
17
+ ```
18
+
19
+ Then add the following to your model:
20
+
21
+ ```rb
22
+ validates :my_url_attribute, url: true
23
+ ```
24
+
25
+ ### Custom options
26
+
27
+ Name | Value | Default | Desc.
28
+ ----|----|----|----
29
+ `scheme` | Array of String | nil | Specify allowed scheme types.
30
+ `allow_no_scheme` | Boolean | false | Whether scheme less URI is allowed.
31
+
32
+ ```rb
33
+ validates :my_url_attribute, url: { scheme: ['https'] }
34
+ ```
35
+
36
+ ## Validation outside a model
37
+
38
+ If you need to validate a url outside a model, you can do that:
39
+
40
+ ```rb
41
+ UrlValidator.valid?(string, options)
42
+ ```
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new('spec')
5
+ task default: :spec
@@ -0,0 +1,22 @@
1
+ file = File.open(File.expand_path('../lib/url_validator/version.rb', __FILE__))
2
+ version = file.read.scan(/\d+\.\d+\.\d+/).first
3
+ file.close
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'activemodel-url_validator'
7
+ spec.version = version
8
+ spec.authors = ['Yuku Takahashi']
9
+ spec.email = ['yuku@qiita.com']
10
+ spec.summary = 'A url validator for Rails 3 and 4.'
11
+ spec.homepage = 'https://github.com/increments/activemodel-url_validator'
12
+ spec.license = 'MIT'
13
+
14
+ spec.files = `git ls-files -z`.split("\x0")
15
+ spec.require_paths = ['lib']
16
+
17
+ spec.add_dependency 'activemodel'
18
+
19
+ spec.add_development_dependency 'bundler', '~> 1.7'
20
+ spec.add_development_dependency 'rake', '~> 10.0'
21
+ spec.add_development_dependency 'rspec'
22
+ end
@@ -0,0 +1,2 @@
1
+ require 'url_validator/version'
2
+ require 'url_validator/engine'
@@ -0,0 +1,25 @@
1
+ class UrlValidator < ActiveModel::EachValidator
2
+ class << self
3
+ def valid?(value, options = {})
4
+ options = default_options.merge(options)
5
+ uri = URI.parse(value)
6
+ if uri.scheme.nil?
7
+ !!options[:allow_no_scheme]
8
+ else
9
+ options[:scheme] ? options[:scheme].include?(uri.scheme) : true
10
+ end
11
+ rescue URI::Error
12
+ false
13
+ end
14
+
15
+ def default_options
16
+ { scheme: nil, allow_no_scheme: false }
17
+ end
18
+ end
19
+
20
+ def validate_each(record, attribute, value)
21
+ unless self.class.valid?(value, options)
22
+ record.errors.add(attribute, options[:message] || :invalid_url)
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,6 @@
1
+ require 'rails'
2
+
3
+ class UrlValidator
4
+ class Engine < ::Rails::Engine
5
+ end
6
+ end
@@ -0,0 +1,3 @@
1
+ class UrlValidator
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,23 @@
1
+ require 'active_model'
2
+ require 'url_validator'
3
+
4
+ RSpec.configure do |config|
5
+ config.color = true
6
+ config.run_all_when_everything_filtered = true
7
+ end
8
+
9
+ class TestModel
10
+ include ActiveModel::Validations
11
+
12
+ def self.name
13
+ 'TestModel'
14
+ end
15
+
16
+ def initialize(attributes = {})
17
+ @attributes = attributes
18
+ end
19
+
20
+ def read_attribute_for_validation(key)
21
+ @attributes[key]
22
+ end
23
+ end
@@ -0,0 +1,153 @@
1
+ require 'spec_helper'
2
+
3
+ # Shared examples for value.
4
+
5
+ shared_examples_for 'invalid value is given', given: :invalid do
6
+ let(:value) do
7
+ 'This is invalid URI'
8
+ end
9
+ end
10
+
11
+ shared_examples_for 'scheme less value is given', given: :scheme_less do
12
+ let(:value) do
13
+ '//example.com/scheme/less'
14
+ end
15
+ end
16
+
17
+ [:http, :https].each do |scheme|
18
+ shared_examples_for "#{scheme} URI is given", given: scheme do
19
+ let(:value) do
20
+ "#{scheme}://example.com/#{scheme}/scheme"
21
+ end
22
+ end
23
+ end
24
+
25
+ # Shared examples for options
26
+
27
+ shared_examples_for 'allow_no_scheme is true', allow_no_scheme: true do
28
+ let(:allow_no_scheme) do
29
+ true
30
+ end
31
+ end
32
+
33
+ shared_examples_for 'scheme includes http', scheme: :http do
34
+ let(:scheme) do
35
+ ['http']
36
+ end
37
+ end
38
+
39
+ shared_examples_for 'scheme is an empty array', scheme: :empty do
40
+ let(:scheme) do
41
+ []
42
+ end
43
+ end
44
+
45
+ describe UrlValidator do
46
+ describe '.valid?' do
47
+ subject do
48
+ described_class.valid?(value, options)
49
+ end
50
+
51
+ let(:options) do
52
+ options = {}
53
+ described_class.default_options.keys.each do |option_name|
54
+ options[option_name] = send(option_name) if respond_to? option_name
55
+ end
56
+ options
57
+ end
58
+
59
+ context 'when invalid value is given', given: :invalid do
60
+ it { should be_falsey }
61
+ end
62
+
63
+ context 'when a scheme less URI is given', given: :scheme_less do
64
+ it { should be_falsey }
65
+
66
+ context 'and scheme less URI is allowed', allow_no_scheme: true do
67
+ it { should be_truthy }
68
+
69
+ context 'and scheme option is given', scheme: :http do
70
+ it { should be_truthy }
71
+ end
72
+ end
73
+ end
74
+
75
+ context 'when a HTTP URI is given', given: :http do
76
+ it { should be_truthy }
77
+
78
+ context 'and http scheme is allowed', scheme: :http do
79
+ it { should be_truthy }
80
+ end
81
+
82
+ context 'and http scheme is not allowed', scheme: :empty do
83
+ it { should be_falsey }
84
+ end
85
+ end
86
+
87
+ context 'when a HTTPS URI is given', given: :https do
88
+ it { should be_truthy }
89
+
90
+ context "and http scheme is allowed but https isn't", scheme: :http do
91
+ it { should be_falsey }
92
+ end
93
+ end
94
+ end
95
+
96
+ describe 'validation' do
97
+ subject do
98
+ model_class.new(attr: value)
99
+ end
100
+
101
+ context 'when custom option is not specified' do
102
+ let(:model_class) do
103
+ Class.new(TestModel) do
104
+ validates :attr, url: true
105
+ end
106
+ end
107
+
108
+ context 'and a valid scheme less value is given', given: :scheme_less do
109
+ it { should be_invalid }
110
+ end
111
+
112
+ context 'and a valid http value is given', given: :http do
113
+ it { should be_valid }
114
+ end
115
+
116
+ context 'and an invalid value is given', given: :invalid do
117
+ it { should be_invalid }
118
+ end
119
+ end
120
+
121
+ context 'when allow_no_scheme is true' do
122
+ let(:model_class) do
123
+ Class.new(TestModel) do
124
+ validates :attr, url: { allow_no_scheme: true }
125
+ end
126
+ end
127
+
128
+ context 'and a valid scheme less value is given', given: :scheme_less do
129
+ it { should be_valid }
130
+ end
131
+ end
132
+
133
+ context 'when http scheme is allowed', scheme: :http do
134
+ let(:model_class) do
135
+ Class.new(TestModel) do
136
+ validates :attr, url: { scheme: ['http'] }
137
+ end
138
+ end
139
+
140
+ context 'and a valid scheme less value is given', given: :scheme_less do
141
+ it { should be_invalid }
142
+ end
143
+
144
+ context 'and a valid http value is given', given: :http do
145
+ it { should be_valid }
146
+ end
147
+
148
+ context 'and a valid https value is given', given: :https do
149
+ it { should be_invalid }
150
+ end
151
+ end
152
+ end
153
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activemodel-url_validator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Yuku Takahashi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activemodel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description:
70
+ email:
71
+ - yuku@qiita.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".travis.yml"
78
+ - Gemfile
79
+ - README.md
80
+ - Rakefile
81
+ - activemodel-url_validator.gemspec
82
+ - lib/activemodel-url_validator.rb
83
+ - lib/url_validator.rb
84
+ - lib/url_validator/engine.rb
85
+ - lib/url_validator/version.rb
86
+ - spec/spec_helper.rb
87
+ - spec/url_validator_spec.rb
88
+ homepage: https://github.com/increments/activemodel-url_validator
89
+ licenses:
90
+ - MIT
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.2.2
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: A url validator for Rails 3 and 4.
112
+ test_files: []