validates_uri_format_of 0.1.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.
Files changed (5) hide show
  1. data/LICENSE +20 -0
  2. data/README.md +29 -0
  3. data/init.rb +1 -0
  4. data/lib/validates_uri_format_of.rb +143 -0
  5. metadata +80 -0
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Digineo GmbH
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.md ADDED
@@ -0,0 +1,29 @@
1
+ # validates\_uri\_format\_of
2
+
3
+ Rails plugin that provides a `validates_uri_format_of` method to `ActiveRecord` models.
4
+ You can require or disallow certain components of a URI.
5
+
6
+ ## Installation
7
+
8
+ Just add the following line to your environment.rb:
9
+
10
+ config.gem 'validates_uri_format_of'
11
+
12
+ ## Usage
13
+
14
+ After installing the plugin, it's used like
15
+
16
+ class User < ActiveRecord::Base
17
+ validates_uri_format_of :url,
18
+ :allow_nil => true,
19
+ :require_fqdn => true
20
+ end
21
+
22
+
23
+ ## Features
24
+
25
+ Take a look at the lib/ and test/ directories.
26
+
27
+ ## Credits and license
28
+
29
+ By [Digineo GmbH](http://www.digineo.de/) under the MIT license.
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'lib/validates_uri_format_of'
@@ -0,0 +1,143 @@
1
+ module ValidatesUriFormatOf
2
+ REGEXP_FQDN = /^(xn--)?[^\W_]+([-.][^\W_]+)*\.[a-z]{2,6}\.?$/i
3
+
4
+ COMPONENTS = {
5
+ # Komponenten, bei denen angegeben werden kann, ob etwas explizit
6
+ # angegeben werden muss oder nicht angegeben werden darf oder
7
+ # ob dies egal ist.
8
+ :with => {
9
+
10
+ # Schlüssel sind unsere eigenen Bezeichnungen,
11
+ # Werte die Bezeichnungen von URI, wobei URI nicht
12
+ # direkt befragt wird, wenn der Wert nil ist.
13
+ :query => :query,
14
+ :user => :user,
15
+ :password => :password,
16
+ :auth => :userinfo, #####
17
+ :fragment => :fragment,
18
+ :port => nil
19
+ },
20
+ :require => {
21
+ :scheme => :scheme,
22
+ :host => :host,
23
+ :path => :path
24
+ }
25
+ }
26
+
27
+ DEFAULT_CONFIGURATION = {
28
+ :schemes => ['http','https'],
29
+
30
+ :require_scheme => true,
31
+ :require_host => true,
32
+ :require_path => true,
33
+
34
+ :require_fqdn => nil,
35
+
36
+ :with_port => nil, # URI gibt immer einen Port zurück, auch, wenn dieser nicht explizit angegeben wurde.
37
+ :with_query => nil,
38
+ :with_user => nil,
39
+ :with_password => nil,
40
+ :with_fragment => nil, # Anchor
41
+
42
+ :with_auth => nil,
43
+ :on => :save
44
+ }
45
+
46
+
47
+ def validates_uri_format_of(*attr_names)
48
+ configuration = {
49
+ :on => :save
50
+ }.merge(DEFAULT_CONFIGURATION)
51
+
52
+ configuration.update(attr_names.extract_options!)
53
+ #validates_each(attr_names, configuration) do |record, attr_name, value|
54
+ validates_each(attr_names) do |record, attr_name, value|
55
+ begin
56
+ raise(URI::InvalidURIError, "cannot be a #{value.class}") unless [NilClass, String].include?(value.class)
57
+
58
+ for meth in [:nil, :empty]
59
+ if value.send("#{meth}?")
60
+ if configuration[:"allow_#{meth}"]
61
+ next
62
+ else
63
+ raise URI::InvalidURIError, "cannot be #{meth}"
64
+ end
65
+ end
66
+ end
67
+
68
+ uri = URI.parse(value)
69
+ # check required fields
70
+ for key in COMPONENTS[:require].keys
71
+ if configuration[:"require_#{key}"] && uri.send(COMPONENTS[:require][key]).to_s.empty?
72
+ raise URI::InvalidURIError, "#{key} missing"
73
+ end
74
+ end
75
+
76
+ if configuration[:require_fqdn] && uri.host !~ REGEXP_FQDN
77
+ raise URI::InvalidURIError, "host must be FQDN"
78
+ end
79
+
80
+
81
+ # URI.parse(value).port gibt immer einen Port zurück, auch, wenn
82
+ # dieser nicht explizit angegeben wurde.
83
+ #
84
+ # URI.split(value) gibt ein Array wie folgt zurück:
85
+ # * Scheme
86
+ # * Userinfo
87
+ # * Host
88
+ # * Port
89
+ # * Registry
90
+ # * Path
91
+ # * Opaque
92
+ # * Query
93
+ # * Fragment
94
+ with_port = configuration[:with_port]
95
+ port_empty = URI.split(value)[3].to_s.empty?
96
+
97
+ if port_empty && with_port
98
+ raise URI::InvalidURIError, "port required"
99
+ elsif !port_empty && (with_port==false)
100
+ raise URI::InvalidURIError, "port not allowed"
101
+ end
102
+
103
+
104
+ # check scheme
105
+ raise URI::InvalidURIError, "invalid scheme" unless configuration[:schemes].include?(uri.scheme.to_s.downcase)
106
+
107
+ # check fields
108
+ for key in COMPONENTS[:with].keys
109
+ should = configuration[:"with_#{key}"]
110
+ next if should.nil?
111
+
112
+ next unless COMPONENTS[:with][key] # Wenn nil, dann findet die Überprüfung woanders statt
113
+ value = uri.send(COMPONENTS[:with][key])
114
+ empty = value.to_s.empty?
115
+
116
+ if empty && should
117
+ raise URI::InvalidURIError, "#{key} required"
118
+ elsif !empty && (should==false)
119
+ raise URI::InvalidURIError, "#{key} not allowed"
120
+ end
121
+ end
122
+
123
+ if uri.to_s =~ /\#$/
124
+ case configuration[:with_fragment]
125
+ when true
126
+ raise URI::InvalidURIError, "fragment required"
127
+ when false
128
+ raise URI::InvalidURIError, "fragment not allowed"
129
+ end
130
+ end
131
+
132
+ if (uri.to_s =~ /\?$/) && (configuration[:with_query] == false)
133
+ raise URI::InvalidURIError, "fragment not allowed"
134
+ end
135
+ rescue URI::InvalidURIError => e
136
+ record.errors.add(attr_name, :invalid, :default => (configuration[:message]||e.message), :value => value)
137
+ end
138
+ end
139
+ end
140
+
141
+ end
142
+
143
+ ActiveRecord::Base.extend(ValidatesUriFormatOf)
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: validates_uri_format_of
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 1
9
+ version: 0.1.1
10
+ platform: ruby
11
+ authors:
12
+ - Julian Kornberger
13
+ - Averell
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-03-18 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: activerecord
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 2
30
+ - 3
31
+ version: "2.3"
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ description: Rails plugin that provides a validates_uri_format_of method to ActiveRecord models. URLs are validated by several options.
35
+ email: github@digineo.de
36
+ executables: []
37
+
38
+ extensions: []
39
+
40
+ extra_rdoc_files:
41
+ - README.md
42
+ - LICENSE
43
+ files:
44
+ - init.rb
45
+ - LICENSE
46
+ - README.md
47
+ - lib/validates_uri_format_of.rb
48
+ has_rdoc: true
49
+ homepage: http://github.com/digineo/validates_uri_format_of
50
+ licenses: []
51
+
52
+ post_install_message:
53
+ rdoc_options:
54
+ - --inline-source
55
+ - --charset=UTF-8
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ segments:
63
+ - 0
64
+ version: "0"
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ segments:
70
+ - 0
71
+ version: "0"
72
+ requirements:
73
+ - :"rmagick libqt4-ruby libqt4-webkit"
74
+ rubyforge_project:
75
+ rubygems_version: 1.3.6
76
+ signing_key:
77
+ specification_version: 3
78
+ summary: URL Validation for ActiveRecord models.
79
+ test_files: []
80
+