uri_format_validator 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.codeclimate.yml +26 -0
- data/.editorconfig +15 -0
- data/.gitignore +14 -0
- data/.hound.yml +3 -0
- data/.rubocop.yml +1076 -0
- data/.travis.yml +45 -0
- data/CHANGELOG.adoc +9 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.adoc +138 -0
- data/ROADMAP.adoc +151 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/rake +18 -0
- data/bin/rspec +18 -0
- data/bin/rubocop +18 -0
- data/bin/setup +8 -0
- data/examples/basic.rb +5 -0
- data/gemfiles/common.gemfile +7 -0
- data/gemfiles/rails-4.0.gemfile +3 -0
- data/gemfiles/rails-4.1.gemfile +3 -0
- data/gemfiles/rails-4.2.gemfile +3 -0
- data/gemfiles/rails-5.0.gemfile +3 -0
- data/gemfiles/rails-5.1.gemfile +3 -0
- data/gemfiles/rails-head.gemfile +3 -0
- data/lib/uri_format_validator/locale/en.yml +4 -0
- data/lib/uri_format_validator/locale/pl.yml +4 -0
- data/lib/uri_format_validator/localization.rb +10 -0
- data/lib/uri_format_validator/reacher.rb +37 -0
- data/lib/uri_format_validator/validators/helper_methods.rb +19 -0
- data/lib/uri_format_validator/validators/uri_format_validator.rb +166 -0
- data/lib/uri_format_validator/validators.rb +16 -0
- data/lib/uri_format_validator/version.rb +6 -0
- data/lib/uri_format_validator.rb +14 -0
- data/uri_format_validator.gemspec +36 -0
- metadata +198 -0
data/.travis.yml
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
sudo: false
|
2
|
+
dist: trusty
|
3
|
+
language: ruby
|
4
|
+
|
5
|
+
before_install:
|
6
|
+
- gem install bundler -v 1.15.4
|
7
|
+
|
8
|
+
script:
|
9
|
+
- bundle exec rspec
|
10
|
+
|
11
|
+
rvm:
|
12
|
+
- "2.4"
|
13
|
+
- "2.3"
|
14
|
+
- "2.2.2"
|
15
|
+
- "ruby-head"
|
16
|
+
- "rbx-3"
|
17
|
+
- "jruby"
|
18
|
+
- "jruby-head"
|
19
|
+
|
20
|
+
gemfile:
|
21
|
+
- gemfiles/rails-5.1.gemfile
|
22
|
+
|
23
|
+
matrix:
|
24
|
+
include:
|
25
|
+
- rvm: "2.4"
|
26
|
+
gemfile: gemfiles/rails-head.gemfile
|
27
|
+
|
28
|
+
- rvm: "2.4"
|
29
|
+
gemfile: gemfiles/rails-5.0.gemfile
|
30
|
+
|
31
|
+
- rvm: "2.4"
|
32
|
+
gemfile: gemfiles/rails-4.2.gemfile
|
33
|
+
|
34
|
+
# Test Rails 4.0–4.1 against Ruby 2.3
|
35
|
+
- rvm: "2.3"
|
36
|
+
gemfile: gemfiles/rails-4.1.gemfile
|
37
|
+
|
38
|
+
- rvm: "2.3"
|
39
|
+
gemfile: gemfiles/rails-4.0.gemfile
|
40
|
+
|
41
|
+
allow_failures:
|
42
|
+
- rvm: "ruby-head"
|
43
|
+
- rvm: "jruby-head"
|
44
|
+
- rvm: "jruby"
|
45
|
+
- gemfile: gemfiles/rails-head.gemfile
|
data/CHANGELOG.adoc
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at jeffrey.lau@ribose.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Ribose Inc.
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.adoc
ADDED
@@ -0,0 +1,138 @@
|
|
1
|
+
= UriFormatValidator
|
2
|
+
|
3
|
+
image:https://img.shields.io/gem/v/uri_format_validator.svg[
|
4
|
+
Gem Version, link="https://rubygems.org/gems/uri_format_validator"]
|
5
|
+
image:https://img.shields.io/travis/riboseinc/uri_format_validator/master.svg[
|
6
|
+
Build Status, link="https://travis-ci.org/riboseinc/uri_format_validator"]
|
7
|
+
image:https://img.shields.io/codeclimate/github/riboseinc/uri_format_validator.svg[
|
8
|
+
"Code Climate", link="https://codeclimate.com/github/riboseinc/uri_format_validator"]
|
9
|
+
image:https://img.shields.io/codecov/c/github/riboseinc/uri_format_validator.svg[
|
10
|
+
"Test Coverage", link="https://codecov.io/gh/riboseinc/uri_format_validator"]
|
11
|
+
|
12
|
+
== Introduction
|
13
|
+
|
14
|
+
This gem provides URI field validator for Rails' Active Model.
|
15
|
+
|
16
|
+
UriFormatValidator requires Active Model 4+. It is tested against MRI 2.2.2+,
|
17
|
+
JRuby, and Rubinius in their recent versions.
|
18
|
+
|
19
|
+
== Getting started
|
20
|
+
|
21
|
+
Add this line to your application's Gemfile:
|
22
|
+
|
23
|
+
[source,ruby]
|
24
|
+
----
|
25
|
+
gem 'uri_format_validator'
|
26
|
+
----
|
27
|
+
|
28
|
+
Then install by executing `bundle install`. You may now define validations
|
29
|
+
in your models, like that:
|
30
|
+
|
31
|
+
[source,ruby]
|
32
|
+
----
|
33
|
+
class User < ActiveRecord::Base
|
34
|
+
validates :home_site_url, uri_format: true
|
35
|
+
end
|
36
|
+
----
|
37
|
+
|
38
|
+
For your convenience, an old-fashioned validation helper is also provided:
|
39
|
+
|
40
|
+
[source,ruby]
|
41
|
+
----
|
42
|
+
class User < ActiveRecord::Base
|
43
|
+
validates_uri_format_of :home_site_url
|
44
|
+
end
|
45
|
+
----
|
46
|
+
|
47
|
+
== Validation options
|
48
|
+
|
49
|
+
=== URI component constraints
|
50
|
+
|
51
|
+
You can specify constraints on URI components: scheme, authority, path, query,
|
52
|
+
and fragment.
|
53
|
+
|
54
|
+
[source,ruby]
|
55
|
+
----
|
56
|
+
validates :home_site_url, uri_format: { scheme: /https?/ }
|
57
|
+
validates :home_site_url, uri_format: { scheme: %w[ssh telnet] }
|
58
|
+
|
59
|
+
validates :home_site_url, uri_format: { authority: /\.example\./ }
|
60
|
+
validates :home_site_url, uri_format: { authority: %w[example.com example.test] }
|
61
|
+
|
62
|
+
# require path component (`/` is not enough)
|
63
|
+
validates :home_site_url, uri_format: { path: true }
|
64
|
+
# disallow presence of path component
|
65
|
+
validates :home_site_url, uri_format: { path: false }
|
66
|
+
# match path against regular expression
|
67
|
+
validates :home_site_url, uri_format: { path: /regexp/ }
|
68
|
+
|
69
|
+
# require query component
|
70
|
+
validates :home_site_url, uri_format: { query: true }
|
71
|
+
# disallow presence of query component
|
72
|
+
validates :home_site_url, uri_format: { query: false }
|
73
|
+
|
74
|
+
# require fragment component
|
75
|
+
validates :home_site_url, uri_format: { fragment: true }
|
76
|
+
# disallow presence of fragment component
|
77
|
+
validates :home_site_url, uri_format: { fragment: false }
|
78
|
+
----
|
79
|
+
|
80
|
+
When `scheme` option is unspecified, only http and https are allowed.
|
81
|
+
|
82
|
+
=== Relative URIs
|
83
|
+
|
84
|
+
By default, only absolute URIs are valid. Specify `authority: false` option
|
85
|
+
to allow relative and disallow absolute URLs:
|
86
|
+
|
87
|
+
[source,ruby]
|
88
|
+
----
|
89
|
+
validates :home_site_url, uri_format: { authority: false }
|
90
|
+
----
|
91
|
+
|
92
|
+
=== Reachable URLs
|
93
|
+
|
94
|
+
With `retrievable` options, you can ensure that URL indeed points to some
|
95
|
+
resource. A HTTP HEAD request will be performed to that URL, and it must result
|
96
|
+
with 2xx status code. Otherwise, given value is considered invalid.
|
97
|
+
|
98
|
+
[source,ruby]
|
99
|
+
----
|
100
|
+
validates :home_site_url, uri_format: { retrievable: true }
|
101
|
+
----
|
102
|
+
|
103
|
+
=== Error messages
|
104
|
+
|
105
|
+
You can also override the default error message:
|
106
|
+
|
107
|
+
[source,ruby]
|
108
|
+
----
|
109
|
+
validates :my_url_attribute, uri_format: true, message: 'is not a valid URL'
|
110
|
+
----
|
111
|
+
|
112
|
+
Alternatively, you can provide your own localization string for
|
113
|
+
`errors.messages.invalid_uri` key.
|
114
|
+
|
115
|
+
== Development
|
116
|
+
|
117
|
+
After checking out the repo, run `bin/setup` to install dependencies.
|
118
|
+
Then, run `rake spec` to run the tests. You can also run `bin/console`
|
119
|
+
for an interactive prompt that will allow you to experiment.
|
120
|
+
|
121
|
+
To install this gem onto your local machine, run
|
122
|
+
`bundle exec rake install`. To release a new version, update the version
|
123
|
+
number in `version.rb`, and then run `bundle exec rake release`, which
|
124
|
+
will create a git tag for the version, push git commits and tags, and
|
125
|
+
push the `.gem` file to https://rubygems.org[rubygems.org].
|
126
|
+
|
127
|
+
== Contributing
|
128
|
+
|
129
|
+
Bug reports and pull requests are welcome on GitHub at
|
130
|
+
https://github.com/riboseinc/uri_format_validator. This project is intended to
|
131
|
+
be a safe, welcoming space for collaboration, and contributors are
|
132
|
+
expected to adhere to the http://contributor-covenant.org[Contributor
|
133
|
+
Covenant] code of conduct.
|
134
|
+
|
135
|
+
== License
|
136
|
+
|
137
|
+
The gem is available as open source under the terms of the
|
138
|
+
http://opensource.org/licenses/MIT[MIT License].
|
data/ROADMAP.adoc
ADDED
@@ -0,0 +1,151 @@
|
|
1
|
+
= Roadmap
|
2
|
+
|
3
|
+
== Basic options
|
4
|
+
|
5
|
+
=== Examples
|
6
|
+
|
7
|
+
[source,ruby]
|
8
|
+
----
|
9
|
+
# default case
|
10
|
+
validates :my_url, url: true
|
11
|
+
|
12
|
+
# allow nil
|
13
|
+
validates :my_url, url: {allow_nil: true}
|
14
|
+
|
15
|
+
# allow blank
|
16
|
+
validates :my_url, url: {allow_blank: true}
|
17
|
+
----
|
18
|
+
|
19
|
+
== URL
|
20
|
+
|
21
|
+
We'd like uri_format_validator to support
|
22
|
+
https://tools.ietf.org/html/rfc3986#appendix-B[RFC 3986],
|
23
|
+
with the option to impose a constraint on the URI scheme, e.g. to only `http` &
|
24
|
+
`https`.
|
25
|
+
|
26
|
+
Next step could introduce options to impose constraints on these as well:
|
27
|
+
|
28
|
+
authority = $4
|
29
|
+
path = $5
|
30
|
+
query = $7
|
31
|
+
fragment = $9
|
32
|
+
|
33
|
+
NOTE: _Copied from https://tools.ietf.org/html/rfc3986#appendix-B[RFC 3986]_
|
34
|
+
|
35
|
+
=== Constraints
|
36
|
+
|
37
|
+
Constraints for the above fields (`authority`, `path`, `query`, `fragment`)
|
38
|
+
could be either presence or a match.
|
39
|
+
|
40
|
+
=== Authority
|
41
|
+
|
42
|
+
Within the `authority` portion, the following could be made optional,
|
43
|
+
prohibited, etc.:
|
44
|
+
|
45
|
+
* `username`
|
46
|
+
* `password`
|
47
|
+
* `port`
|
48
|
+
|
49
|
+
==== Examples
|
50
|
+
|
51
|
+
[source,ruby]
|
52
|
+
----
|
53
|
+
# require absence of fragment
|
54
|
+
validates :my_url, url: {fragment: false}
|
55
|
+
|
56
|
+
# require presence of path
|
57
|
+
validates :my_url, url: {path: true}
|
58
|
+
|
59
|
+
# require authority format to match a regex
|
60
|
+
validates :my_url, url: {authority: %r{(?:.+\.)*example.com}}
|
61
|
+
|
62
|
+
# require absense of authority (?!)
|
63
|
+
validates :my_url, url: {authority: false}
|
64
|
+
|
65
|
+
# require presense of authority (?!)
|
66
|
+
validates :my_url, url: {authority: true}
|
67
|
+
|
68
|
+
# require authority to match against a specific array
|
69
|
+
validates :my_url, url: {authority: %w[https://google.co.uk https://google.com]}
|
70
|
+
----
|
71
|
+
|
72
|
+
=== Schemes
|
73
|
+
|
74
|
+
To add an option to validate according to the list at:
|
75
|
+
https://www.iana.org/assignments/uri-schemes/uri-schemes.xhtml
|
76
|
+
|
77
|
+
==== Examples
|
78
|
+
|
79
|
+
[source,ruby]
|
80
|
+
----
|
81
|
+
# match against the IANA list above
|
82
|
+
validates :my_url, url: {scheme: :all}
|
83
|
+
|
84
|
+
# match against a specific array
|
85
|
+
|
86
|
+
# todo
|
87
|
+
# replace with "in" and symbols
|
88
|
+
# validates :my_url, url: {scheme: {in: [:ftp, :http, :https]}}
|
89
|
+
|
90
|
+
validates :my_url, url: {scheme: %w[ftp http https]}
|
91
|
+
|
92
|
+
# match against a regex
|
93
|
+
|
94
|
+
# todo
|
95
|
+
# not needed; remove?
|
96
|
+
validates :my_url, url: {scheme: /myscheme[0-9]/}
|
97
|
+
----
|
98
|
+
|
99
|
+
== Rails
|
100
|
+
|
101
|
+
To support Rails 4 and 5.
|
102
|
+
|
103
|
+
== Reachability
|
104
|
+
|
105
|
+
To add an option to test for the given URL to see if it's reachable, or if the
|
106
|
+
domain is resolvable.
|
107
|
+
|
108
|
+
=== Examples
|
109
|
+
|
110
|
+
[source,ruby]
|
111
|
+
----
|
112
|
+
# domain name resolvable
|
113
|
+
validates :my_url, url: {resolvable: true}
|
114
|
+
|
115
|
+
# host is reachable
|
116
|
+
validates :my_url, url: {reachable: true}
|
117
|
+
|
118
|
+
# full URL document returns 2xx HTTP status code
|
119
|
+
validates :my_url, url: {retrievable: true}
|
120
|
+
----
|
121
|
+
|
122
|
+
|
123
|
+
== Reserved
|
124
|
+
|
125
|
+
* `.test`
|
126
|
+
* `.example`
|
127
|
+
* `.invalid`
|
128
|
+
* `.localhost`
|
129
|
+
|
130
|
+
http://www.faqs.org/rfcs/rfc2606.html
|
131
|
+
|
132
|
+
=== Examples
|
133
|
+
|
134
|
+
[source,ruby]
|
135
|
+
----
|
136
|
+
# allow reserved authorities to be entered
|
137
|
+
# default behaviour
|
138
|
+
validates :my_url, url: {allow_reserved: true}
|
139
|
+
|
140
|
+
# disallow reserved authorities to be entered
|
141
|
+
validates :my_url, url: {allow_reserved: false}
|
142
|
+
----
|
143
|
+
|
144
|
+
== I18n
|
145
|
+
|
146
|
+
To internationalize the error message strings to `de`, `en-UK`, `en-US`, `es`,
|
147
|
+
`fr`, `ja`, `ko`, `nl`, `pt-BR`, `ru`, `zh-CN`, `zh-TW`, etc.
|
148
|
+
|
149
|
+
== (DONE) Travis CI
|
150
|
+
|
151
|
+
Add Travis CI badge to `README`.
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "uri_format_validator"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
require "pry"
|
11
|
+
Pry.start
|
12
|
+
|
13
|
+
# require "irb"
|
14
|
+
# IRB.start(__FILE__)
|
data/bin/rake
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
#
|
5
|
+
# This file was generated by Bundler.
|
6
|
+
#
|
7
|
+
# The application 'rake' is installed as part of a gem, and
|
8
|
+
# this file is here to facilitate running it.
|
9
|
+
#
|
10
|
+
|
11
|
+
require "pathname"
|
12
|
+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
|
13
|
+
Pathname.new(__FILE__).realpath)
|
14
|
+
|
15
|
+
require "rubygems"
|
16
|
+
require "bundler/setup"
|
17
|
+
|
18
|
+
load Gem.bin_path("rake", "rake")
|
data/bin/rspec
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
#
|
5
|
+
# This file was generated by Bundler.
|
6
|
+
#
|
7
|
+
# The application 'rspec' is installed as part of a gem, and
|
8
|
+
# this file is here to facilitate running it.
|
9
|
+
#
|
10
|
+
|
11
|
+
require "pathname"
|
12
|
+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
|
13
|
+
Pathname.new(__FILE__).realpath)
|
14
|
+
|
15
|
+
require "rubygems"
|
16
|
+
require "bundler/setup"
|
17
|
+
|
18
|
+
load Gem.bin_path("rspec-core", "rspec")
|
data/bin/rubocop
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
#
|
5
|
+
# This file was generated by Bundler.
|
6
|
+
#
|
7
|
+
# The application 'rubocop' is installed as part of a gem, and
|
8
|
+
# this file is here to facilitate running it.
|
9
|
+
#
|
10
|
+
|
11
|
+
require "pathname"
|
12
|
+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
|
13
|
+
Pathname.new(__FILE__).realpath)
|
14
|
+
|
15
|
+
require "rubygems"
|
16
|
+
require "bundler/setup"
|
17
|
+
|
18
|
+
load Gem.bin_path("rubocop", "rubocop")
|
data/bin/setup
ADDED
data/examples/basic.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# (c) Copyright 2017 Ribose Inc.
|
2
|
+
#
|
3
|
+
|
4
|
+
require "net/http"
|
5
|
+
|
6
|
+
module UriFormatValidator
|
7
|
+
# Reacher is a minimalist net client which purpose is to determine whether
|
8
|
+
# given URL is resolvable, host is reachable, and content is retrievable.
|
9
|
+
class Reacher
|
10
|
+
attr_reader :url
|
11
|
+
|
12
|
+
def initialize(url)
|
13
|
+
@url = url
|
14
|
+
end
|
15
|
+
|
16
|
+
# Tests whether given +url+ is retrievable, that is making a HEAD request
|
17
|
+
# results with 2xx status code.
|
18
|
+
def retrievable?
|
19
|
+
head_response.is_a?(Net::HTTPSuccess)
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def head_response
|
25
|
+
Net::HTTP.start(url.hostname, url.port, use_ssl: use_ssl?) do |http|
|
26
|
+
http.request_head(url)
|
27
|
+
end
|
28
|
+
rescue
|
29
|
+
# FIXME The rescue-all clause should be replaced with sth specific
|
30
|
+
nil
|
31
|
+
end
|
32
|
+
|
33
|
+
def use_ssl?
|
34
|
+
url.scheme == "https"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# (c) Copyright 2017 Ribose Inc.
|
2
|
+
#
|
3
|
+
|
4
|
+
require "active_model"
|
5
|
+
|
6
|
+
module UriFormatValidator
|
7
|
+
module Validators
|
8
|
+
module HelperMethods
|
9
|
+
# Encapsulates the pattern of wanting to validate an URL.
|
10
|
+
#
|
11
|
+
# class Post < ActiveRecord::Base
|
12
|
+
# validates_uri_format_of :permalink
|
13
|
+
# end
|
14
|
+
def validates_uri_format_of(*attr_names)
|
15
|
+
validates_with UriFormatValidator, _merge_attributes(attr_names)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|