url_regexp 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/.gitignore +17 -0
- data/.travis.yml +6 -0
- data/Gemfile +6 -0
- data/LICENSE +20 -0
- data/README.md +59 -0
- data/Rakefile +6 -0
- data/lib/url_regexp.rb +5 -0
- data/lib/url_regexp/host.rb +21 -0
- data/lib/url_regexp/node.rb +20 -0
- data/lib/url_regexp/path.rb +51 -0
- data/lib/url_regexp/path_set.rb +47 -0
- data/lib/url_regexp/query.rb +24 -0
- data/lib/url_regexp/root.rb +26 -0
- data/lib/url_regexp/scheme.rb +27 -0
- data/lib/url_regexp/version.rb +3 -0
- data/url_regexp.gemspec +23 -0
- metadata +103 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0597b803c2c877dae434e22bd8c0e732bce5a206
|
4
|
+
data.tar.gz: c22828be492484b11449a3b432c265a0c8185b16
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: acd92da4d537af3110eeb6c7b120f3fc0965c76beb4ec5334392c5bd25b2105087d07123be083933d8447f0103f7f140ab5a3c9a30490f8758edd2d62433d0fe
|
7
|
+
data.tar.gz: cecdd701dd22adb5bfece787b5ad41aa64f2062701c3a71988d66d878104172a38e5b82f6da8e5f0d17b3c65c5306b266193f405efa4dcd178662df0e15d6cf3
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Daisuke Taniwaki
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
7
|
+
the Software without restriction, including without limitation the rights to
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
10
|
+
subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
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, FITNESS
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
# url_regexp
|
2
|
+
|
3
|
+
[![Gem Version][gem-image]][gem-link]
|
4
|
+
[![Dependency Status][deps-image]][deps-link]
|
5
|
+
[![Build Status][build-image]][build-link]
|
6
|
+
[![Coverage Status][cov-image]][cov-link]
|
7
|
+
[![Code Climate][gpa-image]][gpa-link]
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add the url_regexp gem to your Gemfile.
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem "url_regexp"
|
15
|
+
```
|
16
|
+
|
17
|
+
And run `bundle install`.
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
e.g.
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
root = UrlRegexp::Root.new
|
25
|
+
root.append('http://www.example.com/foo/bar')
|
26
|
+
root.to_regexp
|
27
|
+
# => /^http:\/\/www\.example\.com\/foo\/bar(\?.*)?(#|$)/
|
28
|
+
root.append('http://www.example.com/foo/bar/wow')
|
29
|
+
root.to_regexp
|
30
|
+
# => /^http:\/\/www\.example\.com\/foo\/bar(\/wow)?(\?.*)?(#|$)/
|
31
|
+
```
|
32
|
+
|
33
|
+
You can set the options globally and locally. Locally set option overwrites the one globally set. Just add any settings necessary for your mailers from the list below.
|
34
|
+
|
35
|
+
## Contributing
|
36
|
+
|
37
|
+
1. Fork it
|
38
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
39
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
40
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
41
|
+
5. Create new [Pull Request](../../pull/new/master)
|
42
|
+
|
43
|
+
## Copyright
|
44
|
+
|
45
|
+
Copyright (c) 2016 Daisuke Taniwaki. See [LICENSE](LICENSE) for details.
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
[gem-image]: https://badge.fury.io/rb/url_regexp.svg
|
50
|
+
[gem-link]: http://badge.fury.io/rb/url_regexp
|
51
|
+
[build-image]: https://secure.travis-ci.org/dtaniwaki/url_regexp.png?branch=master
|
52
|
+
[build-link]: http://travis-ci.org/dtaniwaki/url_regexp?branch=master
|
53
|
+
[deps-image]: https://gemnasium.com/dtaniwaki/url_regexp.svg?branch=master
|
54
|
+
[deps-link]: https://gemnasium.com/dtaniwaki/url_regexp?branch=master
|
55
|
+
[cov-image]: https://coveralls.io/repos/dtaniwaki/url_regexp/badge.png?branch=master
|
56
|
+
[cov-link]: https://coveralls.io/r/dtaniwaki/url_regexp?branch=master
|
57
|
+
[gpa-image]: https://codeclimate.com/github/dtaniwaki/url_regexp.png?branch=master
|
58
|
+
[gpa-link]: https://codeclimate.com/github/dtaniwaki/url_regexp?branch=master
|
59
|
+
|
data/Rakefile
ADDED
data/lib/url_regexp.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
module UrlRegexp
|
2
|
+
class Host < Node
|
3
|
+
def initialize
|
4
|
+
@hosts = Set.new
|
5
|
+
end
|
6
|
+
|
7
|
+
def append(host)
|
8
|
+
@hosts << host
|
9
|
+
end
|
10
|
+
|
11
|
+
def to_regexp_s
|
12
|
+
hosts = @hosts.map { |h| Regexp.quote(h.to_s) }
|
13
|
+
if 1 < hosts.size
|
14
|
+
hosts = "(#{hosts.join('|')})"
|
15
|
+
else
|
16
|
+
hosts = hosts[0]
|
17
|
+
end
|
18
|
+
hosts
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module UrlRegexp
|
2
|
+
class Node
|
3
|
+
def eql?(other)
|
4
|
+
hash == other.hash
|
5
|
+
end
|
6
|
+
|
7
|
+
def append(node)
|
8
|
+
raise NotImplementedError
|
9
|
+
end
|
10
|
+
|
11
|
+
def to_regexp_s
|
12
|
+
raise NotImplementedError
|
13
|
+
end
|
14
|
+
|
15
|
+
def to_regexp
|
16
|
+
rs = to_regexp_s
|
17
|
+
Regexp.new(rs)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require_relative './node'
|
2
|
+
require_relative './path_set'
|
3
|
+
|
4
|
+
module UrlRegexp
|
5
|
+
class Path < Node
|
6
|
+
attr_reader :label, :paths
|
7
|
+
attr_accessor :path_end
|
8
|
+
|
9
|
+
def initialize(label = nil, parent = nil)
|
10
|
+
@label = label
|
11
|
+
@parent = parent
|
12
|
+
@paths = PathSet.new
|
13
|
+
@path_end = false
|
14
|
+
end
|
15
|
+
|
16
|
+
def hash
|
17
|
+
[@label, @paths.hash].hash
|
18
|
+
end
|
19
|
+
|
20
|
+
def append(path)
|
21
|
+
if path == ''
|
22
|
+
@paths.append(Path.new('', self))
|
23
|
+
else
|
24
|
+
if @parent.nil?
|
25
|
+
_, label, rest = path.split('/', 3)
|
26
|
+
else
|
27
|
+
label, rest = path.split('/', 2)
|
28
|
+
end
|
29
|
+
if label
|
30
|
+
unless p = @paths.find { |_p| _p.label == label }
|
31
|
+
p = Path.new(label, self)
|
32
|
+
@paths.append(p)
|
33
|
+
end
|
34
|
+
if rest.nil?
|
35
|
+
p.path_end = true
|
36
|
+
else
|
37
|
+
p.append(rest)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def to_regexp_s
|
44
|
+
s = @paths.to_regexp_s
|
45
|
+
s = "/#{s}" unless s.nil?
|
46
|
+
s = "(#{s})?" if @path_end && s.to_s != ''
|
47
|
+
s = "#{Regexp.quote(@label)}#{s}" if @label
|
48
|
+
s
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require_relative './node'
|
2
|
+
|
3
|
+
module UrlRegexp
|
4
|
+
class PathSet < Node
|
5
|
+
include Enumerable
|
6
|
+
extend Forwardable
|
7
|
+
|
8
|
+
def_delegators :@set, :size, :hash, *Enumerable.public_instance_methods(false)
|
9
|
+
|
10
|
+
def initialize(set = Set.new)
|
11
|
+
@set = set
|
12
|
+
end
|
13
|
+
|
14
|
+
def append(path)
|
15
|
+
set << path
|
16
|
+
end
|
17
|
+
|
18
|
+
def &(other)
|
19
|
+
self.class.new(set & other.set)
|
20
|
+
end
|
21
|
+
|
22
|
+
def |(other)
|
23
|
+
self.class.new(set & other.set)
|
24
|
+
end
|
25
|
+
|
26
|
+
def to_regexp_s
|
27
|
+
s = if 5 < size
|
28
|
+
"([^#?]*)"
|
29
|
+
elsif 1 < size
|
30
|
+
children_paths = map(&:paths).reduce { |s1, s2| s1 & s2 }
|
31
|
+
if children_paths.size == 1
|
32
|
+
"(#{map(&:label).join("|")})/#{children_paths.to_regexp_s}"
|
33
|
+
else
|
34
|
+
"(#{map(&:to_regexp_s).join("|")})"
|
35
|
+
end
|
36
|
+
elsif 1 == size
|
37
|
+
to_a.first.to_regexp_s
|
38
|
+
else
|
39
|
+
nil
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
protected
|
44
|
+
|
45
|
+
attr_reader :set
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module UrlRegexp
|
2
|
+
class Query
|
3
|
+
def initialize
|
4
|
+
@queries = []
|
5
|
+
end
|
6
|
+
|
7
|
+
def append(query)
|
8
|
+
if !query.nil?
|
9
|
+
@queries << query.to_s.split('&').reject(&:empty?)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def to_regexp_s
|
14
|
+
common_queries = @queries.reduce { |q1, q2| q1 & q2 } || []
|
15
|
+
if 1 < common_queries.size
|
16
|
+
"\\?(#{common_queries.map { |q| Regexp.quote(q) }.permutation.to_a.map { |qs| "(.*&)?#{qs.join('.*&')}(&.*)?" }.join('|')})"
|
17
|
+
elsif 1 == common_queries.size
|
18
|
+
"\\?(.*&)?#{Regexp.quote(common_queries.first)}(&.*)?"
|
19
|
+
else
|
20
|
+
"(\\?.*)?"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
%w(scheme host path query).each do |m|
|
2
|
+
require_relative "./#{m}"
|
3
|
+
end
|
4
|
+
|
5
|
+
module UrlRegexp
|
6
|
+
class Root < Node
|
7
|
+
def initialize
|
8
|
+
@scheme = Scheme.new
|
9
|
+
@host = Host.new
|
10
|
+
@path = Path.new
|
11
|
+
@query = Query.new
|
12
|
+
end
|
13
|
+
|
14
|
+
def append(url)
|
15
|
+
url = URI(url) unless url.is_a?(URI)
|
16
|
+
@path.append(url.path)
|
17
|
+
@scheme.append(url.scheme)
|
18
|
+
@host.append(url.host)
|
19
|
+
@query.append(url.query)
|
20
|
+
end
|
21
|
+
|
22
|
+
def to_regexp_s
|
23
|
+
"^#{@scheme.to_regexp_s}#{@host.to_regexp_s}#{@path.to_regexp_s}#{@query.to_regexp_s}(#|$)"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require_relative './node'
|
2
|
+
|
3
|
+
module UrlRegexp
|
4
|
+
class Scheme < Node
|
5
|
+
def initialize
|
6
|
+
@schemes = Set.new
|
7
|
+
end
|
8
|
+
|
9
|
+
def append(scheme)
|
10
|
+
@schemes << scheme
|
11
|
+
end
|
12
|
+
|
13
|
+
def to_regexp_s
|
14
|
+
schemes = @schemes.map { |s| Regexp.quote(s) }
|
15
|
+
s = if schemes == %w(http https)
|
16
|
+
"https?://"
|
17
|
+
elsif 1 < @schemes.size
|
18
|
+
"(#{schemes.join('|')})://"
|
19
|
+
elsif 1 == @schemes.size
|
20
|
+
"#{schemes.to_a.first.to_s}://"
|
21
|
+
else
|
22
|
+
""
|
23
|
+
end
|
24
|
+
s
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/url_regexp.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'url_regexp/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "url_regexp"
|
8
|
+
spec.version = UrlRegexp::VERSION
|
9
|
+
spec.authors = ["Daisuke Taniwaki"]
|
10
|
+
spec.email = ["daisuketaniwaki@gmail.com"]
|
11
|
+
spec.license = "MIT"
|
12
|
+
|
13
|
+
spec.summary = %q{Generate regular expression for URL}
|
14
|
+
spec.description = %q{Generate regular expression for URL}
|
15
|
+
spec.homepage = "https://github.com/dtaniwaki/url_regexp"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
21
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
22
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: url_regexp
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Daisuke Taniwaki
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-02-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.11'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.11'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description: Generate regular expression for URL
|
56
|
+
email:
|
57
|
+
- daisuketaniwaki@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".travis.yml"
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- lib/url_regexp.rb
|
69
|
+
- lib/url_regexp/host.rb
|
70
|
+
- lib/url_regexp/node.rb
|
71
|
+
- lib/url_regexp/path.rb
|
72
|
+
- lib/url_regexp/path_set.rb
|
73
|
+
- lib/url_regexp/query.rb
|
74
|
+
- lib/url_regexp/root.rb
|
75
|
+
- lib/url_regexp/scheme.rb
|
76
|
+
- lib/url_regexp/version.rb
|
77
|
+
- url_regexp.gemspec
|
78
|
+
homepage: https://github.com/dtaniwaki/url_regexp
|
79
|
+
licenses:
|
80
|
+
- MIT
|
81
|
+
metadata: {}
|
82
|
+
post_install_message:
|
83
|
+
rdoc_options: []
|
84
|
+
require_paths:
|
85
|
+
- lib
|
86
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
requirements: []
|
97
|
+
rubyforge_project:
|
98
|
+
rubygems_version: 2.5.1
|
99
|
+
signing_key:
|
100
|
+
specification_version: 4
|
101
|
+
summary: Generate regular expression for URL
|
102
|
+
test_files: []
|
103
|
+
has_rdoc:
|