validate_url 1.0.2 → 1.0.13

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 3a9370dd3549d270bff452c951a9de3ae4d0288b
4
- data.tar.gz: 342adc16d0211b47d822d590f0395aa53ea74b8a
2
+ SHA256:
3
+ metadata.gz: 414a69dd5d0048f3563cafe2136e4b4c00731d4492afa77b4c9248bcd91f24d2
4
+ data.tar.gz: b92e07f5bdb42ff396395fd1adf40473ff08a4781397357cd752dbbbcb198523
5
5
  SHA512:
6
- metadata.gz: ce037a673542945b111562d32cb1633dfcc955fa5a256ad16823b2094da17d8715fa28b9a4bf252ceb0809f52d88e797c3534a7f0f88d5a0c30e3024b8da3538
7
- data.tar.gz: 702b42045a2f850bcc2f5595299177ce97171caa05ca0a4732d78f7ec5b9c81c83b84c700e0a2605c870ff175bbf60232871e04b3b3c7db2ec6735089fd7cebc
6
+ metadata.gz: e3bbe821276bf8f2c82e227a2b0c9bcb9aa76782994da820977a992e7bb0d9116a623d421e1850713cd8dfed91b22620c2d9bcd6d30770e1d198bf8486a13992
7
+ data.tar.gz: 07bf30ca4b67f8701baacd6505a3c9f64d792267cca3f672832b927e5f1df331ba32881faee2d755aa01abd92c7bbefde91795df3fd5da0246a8b82ed94fbd82
data/README.md CHANGED
@@ -1,14 +1,14 @@
1
1
  # Validates URL
2
2
 
3
- This gem adds the capability of validating URLs to ActiveRecord and ActiveModel (Rails 3).
3
+ This gem adds the capability of validating URLs to ActiveRecord and ActiveModel.
4
4
 
5
5
  ## Installation
6
-
7
- ```
6
+
7
+ ```
8
8
  # add this to your Gemfile
9
9
  gem "validate_url"
10
10
 
11
- # and run
11
+ # or run
12
12
  sudo gem install validate_url
13
13
  ```
14
14
 
@@ -16,19 +16,25 @@ sudo gem install validate_url
16
16
 
17
17
  ### With ActiveRecord
18
18
 
19
- ```ruby
19
+ ```ruby
20
20
  class Pony < ActiveRecord::Base
21
21
  # standard validation
22
- validates :homepage, :url => true
22
+ validates :homepage, url: true
23
23
 
24
24
  # with allow_nil
25
- validates :homepage, :url => {:allow_nil => true}
25
+ validates :homepage, url: { allow_nil: true }
26
26
 
27
27
  # with allow_blank
28
- validates :homepage, :url => {:allow_blank => true}
28
+ validates :homepage, url: { allow_blank: true }
29
29
 
30
30
  # without local hostnames
31
- validates :homepage, :url => {:no_local => true}
31
+ validates :homepage, url: { no_local: true }
32
+
33
+ # with custom schemes
34
+ validates :homepage, url: { schemes: ['https'] }
35
+
36
+ # with public suffix database https://publicsuffix.org/
37
+ validates :homepage, url: { public_suffix: true }
32
38
  end
33
39
  ```
34
40
 
@@ -41,14 +47,30 @@ class Unicorn
41
47
  attr_accessor :homepage
42
48
 
43
49
  # with legacy syntax (the syntax above works also)
44
- validates_url :homepage, :allow_blank => true
50
+ validates_url :homepage, allow_blank: true
51
+ end
52
+ ```
53
+
54
+ ### With RSpec
55
+
56
+ Require the matcher in `spec_helper.rb` or `rails_helper.rb`:
57
+
58
+ ```ruby
59
+ require 'validate_url/rspec_matcher'
60
+ ```
61
+
62
+ In your spec:
63
+
64
+ ```ruby
65
+ describe Unicorn
66
+ it { is_expected.to validate_url_of(:homepage) }
45
67
  end
46
68
  ```
47
69
 
48
70
  ### I18n
49
71
 
50
- The default error message `is not valid url`.
51
- You can pass the `:message => "my custom error"` option to your validation to define your own, custom message.
72
+ The default error message `is not a valid URL`.
73
+ You can pass the `message: "my custom error"` option to your validation to define your own, custom message.
52
74
 
53
75
 
54
76
  ## Contributing
@@ -63,4 +85,16 @@ Validates URL is created and maintained by [PerfectLine](http://www.perfectline.
63
85
  ## License
64
86
 
65
87
  Validates URL is Copyright © 2010-2014 [PerfectLine](http://www.perfectline.co), LLC. It is free software, and may be
66
- redistributed under the terms specified in the LICENSE file.
88
+ redistributed under the terms specified in the LICENSE file.
89
+
90
+ ## How to push new version
91
+
92
+ ```
93
+ rake version:bump:patch
94
+ rake gemspec
95
+ ```
96
+ Fix validate_url.gemspec to remove unneeded wrong strings
97
+ ```
98
+ gem build validate_url.gemspec
99
+ gem push validate_url-1.0.8.gem
100
+ ```
data/lib/locale/ar.yml ADDED
@@ -0,0 +1,4 @@
1
+ ar:
2
+ errors:
3
+ messages:
4
+ url: عنوان الموقع غير صالح
data/lib/locale/es.yml ADDED
@@ -0,0 +1,4 @@
1
+ es:
2
+ errors:
3
+ messages:
4
+ url: no es una URL válida
data/lib/locale/fr.yml ADDED
@@ -0,0 +1,4 @@
1
+ fr:
2
+ errors:
3
+ messages:
4
+ url: "n'est pas une URL valide"
data/lib/locale/ja.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ja:
2
2
  errors:
3
3
  messages:
4
- url: は不正なURLです。
4
+ url: は不正なURLです
data/lib/locale/km.yml ADDED
@@ -0,0 +1,4 @@
1
+ km:
2
+ errors:
3
+ messages:
4
+ url: "មិនត្រឹមត្រូវ"
data/lib/locale/pl.yml ADDED
@@ -0,0 +1,4 @@
1
+ pl:
2
+ errors:
3
+ messages:
4
+ url: nie jest poprawnym adresem URL.
data/lib/locale/ro.yml ADDED
@@ -0,0 +1,4 @@
1
+ ro:
2
+ errors:
3
+ messages:
4
+ url: nu este un URL valid
data/lib/locale/ru.yml ADDED
@@ -0,0 +1,4 @@
1
+ ru:
2
+ errors:
3
+ messages:
4
+ url: некорректный URL
data/lib/locale/vi.yml ADDED
@@ -0,0 +1,4 @@
1
+ vi:
2
+ errors:
3
+ messages:
4
+ url: không phải là một địa chỉ liên kết hợp lệ
@@ -0,0 +1,4 @@
1
+ zh-CN:
2
+ errors:
3
+ messages:
4
+ url: '网址格式不正确'
@@ -0,0 +1,4 @@
1
+ zh-TW:
2
+ errors:
3
+ messages:
4
+ url: '網址格式不正確'
@@ -0,0 +1,12 @@
1
+ RSpec::Matchers.define :validate_url_of do |attribute|
2
+ match do
3
+ actual = subject.is_a?(Class) ? subject.new : subject
4
+ actual.send(:"#{attribute}=", "htp://invalidurl")
5
+ expect(actual).to be_invalid
6
+ @expected_message ||= I18n.t("errors.messages.url")
7
+ expect(actual.errors.messages[attribute.to_sym]).to include(@expected_message)
8
+ end
9
+ chain :with_message do |message|
10
+ @expected_message = message
11
+ end
12
+ end
data/lib/validate_url.rb CHANGED
@@ -1,18 +1,18 @@
1
- require 'addressable/uri'
2
1
  require 'active_model'
3
2
  require 'active_support/i18n'
4
- I18n.load_path << File.dirname(__FILE__) + '/locale/en.yml'
5
- I18n.load_path << File.dirname(__FILE__) + '/locale/tr.yml'
6
- I18n.load_path << File.dirname(__FILE__) + '/locale/ja.yml'
3
+ require 'public_suffix'
4
+ I18n.load_path += Dir[File.dirname(__FILE__) + "/locale/*.yml"]
7
5
 
8
6
  module ActiveModel
9
7
  module Validations
10
8
  class UrlValidator < ActiveModel::EachValidator
9
+ RESERVED_OPTIONS = [:schemes, :no_local]
11
10
 
12
11
  def initialize(options)
13
- options.reverse_merge!(:schemes => %w(http https))
14
- options.reverse_merge!(:message => :url)
15
- options.reverse_merge!(:no_local => false)
12
+ options.reverse_merge!(schemes: %w(http https))
13
+ options.reverse_merge!(message: :url)
14
+ options.reverse_merge!(no_local: false)
15
+ options.reverse_merge!(public_suffix: false)
16
16
 
17
17
  super(options)
18
18
  end
@@ -20,14 +20,30 @@ module ActiveModel
20
20
  def validate_each(record, attribute, value)
21
21
  schemes = [*options.fetch(:schemes)].map(&:to_s)
22
22
  begin
23
- uri = Addressable::URI.parse(value)
24
- unless uri && uri.host && schemes.include?(uri.scheme) && (!options.fetch(:no_local) || uri.host.include?('.'))
25
- record.errors.add(attribute, options.fetch(:message), :value => value)
23
+ uri = URI.parse(value)
24
+ host = uri && uri.host
25
+ scheme = uri && uri.scheme
26
+
27
+ valid_raw_url = scheme && value =~ /\A#{URI::regexp([scheme])}\z/
28
+ valid_scheme = host && scheme && schemes.include?(scheme)
29
+ valid_no_local = !options.fetch(:no_local) || (host && host.include?('.'))
30
+ valid_suffix = !options.fetch(:public_suffix) || (host && PublicSuffix.valid?(host, :default_rule => nil))
31
+
32
+ unless valid_raw_url && valid_scheme && valid_no_local && valid_suffix
33
+ record.errors.add(attribute, options.fetch(:message), value: value)
26
34
  end
27
- rescue Addressable::URI::InvalidURIError
28
- record.errors.add(attribute, options.fetch(:message), :value => value)
35
+ rescue URI::InvalidURIError
36
+ record.errors.add(attribute, :url, **filtered_options(value))
29
37
  end
30
38
  end
39
+
40
+ protected
41
+
42
+ def filtered_options(value)
43
+ filtered = options.except(*RESERVED_OPTIONS)
44
+ filtered[:value] = value
45
+ filtered
46
+ end
31
47
  end
32
48
 
33
49
  module ClassMethods
@@ -36,8 +52,8 @@ module ActiveModel
36
52
  # class Unicorn
37
53
  # include ActiveModel::Validations
38
54
  # attr_accessor :homepage, :ftpsite
39
- # validates_url :homepage, :allow_blank => true
40
- # validates_url :ftpsite, :schemes => ['ftp']
55
+ # validates_url :homepage, allow_blank: true
56
+ # validates_url :ftpsite, schemes: ['ftp']
41
57
  # end
42
58
  # Configuration options:
43
59
  # * <tt>:message</tt> - A custom error message (default is: "is not a valid URL").
metadata CHANGED
@@ -1,39 +1,53 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: validate_url
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tanel Suurhans
8
8
  - Tarmo Lehtpuu
9
9
  - Vladimir Krylov
10
- autorequire:
10
+ autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2015-07-21 00:00:00.000000000 Z
13
+ date: 2020-09-23 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
- name: activemodel
16
+ name: jeweler
17
17
  requirement: !ruby/object:Gem::Requirement
18
18
  requirements:
19
19
  - - ">="
20
20
  - !ruby/object:Gem::Version
21
- version: 3.0.0
22
- type: :runtime
21
+ version: '0'
22
+ type: :development
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
25
25
  requirements:
26
26
  - - ">="
27
27
  - !ruby/object:Gem::Version
28
- version: 3.0.0
28
+ version: '0'
29
29
  - !ruby/object:Gem::Dependency
30
- name: addressable
30
+ name: sqlite3
31
31
  requirement: !ruby/object:Gem::Requirement
32
32
  requirements:
33
33
  - - ">="
34
34
  - !ruby/object:Gem::Version
35
35
  version: '0'
36
- type: :runtime
36
+ type: :development
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ - !ruby/object:Gem::Dependency
44
+ name: activerecord
45
+ requirement: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ type: :development
37
51
  prerelease: false
38
52
  version_requirements: !ruby/object:Gem::Requirement
39
53
  requirements:
@@ -68,6 +82,34 @@ dependencies:
68
82
  - - ">="
69
83
  - !ruby/object:Gem::Version
70
84
  version: 1.1.2
85
+ - !ruby/object:Gem::Dependency
86
+ name: activemodel
87
+ requirement: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: 3.0.0
92
+ type: :runtime
93
+ prerelease: false
94
+ version_requirements: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: 3.0.0
99
+ - !ruby/object:Gem::Dependency
100
+ name: public_suffix
101
+ requirement: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ type: :runtime
107
+ prerelease: false
108
+ version_requirements: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
71
113
  description: Library for validating urls in Rails.
72
114
  email:
73
115
  - tanel.suurhans@perfectline.co
@@ -83,17 +125,28 @@ files:
83
125
  - README.md
84
126
  - init.rb
85
127
  - install.rb
128
+ - lib/locale/ar.yml
86
129
  - lib/locale/de.yml
87
130
  - lib/locale/en.yml
131
+ - lib/locale/es.yml
132
+ - lib/locale/fr.yml
88
133
  - lib/locale/it.yml
89
134
  - lib/locale/ja.yml
135
+ - lib/locale/km.yml
136
+ - lib/locale/pl.yml
90
137
  - lib/locale/pt-BR.yml
138
+ - lib/locale/ro.yml
139
+ - lib/locale/ru.yml
91
140
  - lib/locale/tr.yml
141
+ - lib/locale/vi.yml
142
+ - lib/locale/zh-CN.yml
143
+ - lib/locale/zh-TW.yml
92
144
  - lib/validate_url.rb
145
+ - lib/validate_url/rspec_matcher.rb
93
146
  homepage: http://github.com/perfectline/validates_url/tree/master
94
147
  licenses: []
95
148
  metadata: {}
96
- post_install_message:
149
+ post_install_message:
97
150
  rdoc_options: []
98
151
  require_paths:
99
152
  - lib
@@ -108,9 +161,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
108
161
  - !ruby/object:Gem::Version
109
162
  version: '0'
110
163
  requirements: []
111
- rubyforge_project:
112
- rubygems_version: 2.4.5
113
- signing_key:
164
+ rubygems_version: 3.0.8
165
+ signing_key:
114
166
  specification_version: 4
115
167
  summary: Library for validating urls in Rails.
116
168
  test_files: []