rack-joint 0.1.1 → 0.2.0
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -1
- data/README.md +14 -11
- data/lib/rack/joint.rb +23 -21
- data/lib/rack/joint/context.rb +5 -4
- data/lib/rack/joint/redirect.rb +4 -4
- data/lib/rack/joint/redirect_interface.rb +31 -11
- data/lib/rack/joint/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f3e792eebe6018b20bcfcd52b8585ed66527ef3f726d66c262870713de146734
|
4
|
+
data.tar.gz: 637b67d842b4a9d8fd1ad282eb411d2d643e939bc06fd870f268d9e44dccdf0e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8a4903e59817ca4e4bdb9a0a8beb65e31a152dd36f575fb9f53f45b52863e01afe126c4f6ea0edcfb3fb597f84b8b318c79bb9687d85c1ce8e5a371285530161
|
7
|
+
data.tar.gz: 414c0224b3ec9097f2d600fcc70c36ae92ad4fc0cc7dd27859b51005c3742aff5f5ed0e8d469b94c854bcfbe93016a0b0633a4f85bc34bfc23c63cd54e9a18a4
|
data/CHANGELOG.md
CHANGED
@@ -2,8 +2,12 @@
|
|
2
2
|
|
3
3
|
## master
|
4
4
|
|
5
|
+
### 0.2.0 (2018-08-30)
|
6
|
+
* Enable to use SSL when redirecting. [#4](https://github.com/akito19/rack-joint/pull/4)
|
7
|
+
* Refactor to run redirect process. [#5](https://github.com/akito19/rack-joint/pull/5)
|
8
|
+
|
5
9
|
### 0.1.1 (2018-08-27)
|
6
10
|
* Update a class name, method name and filename. [#1](https://github.com/akito19/rack-joint/pull/1)
|
7
11
|
|
8
|
-
### 0.1.0 (2018-08-
|
12
|
+
### 0.1.0 (2018-08-25)
|
9
13
|
Initial :muscle:
|
data/README.md
CHANGED
@@ -32,7 +32,7 @@ require 'rack/joint'
|
|
32
32
|
use Rack::Joint do
|
33
33
|
host 'example.com' do
|
34
34
|
# If you access `http://example.com/dogs/bark.html`,
|
35
|
-
# redirect to `http://example.org/bowwow.html` with 301.
|
35
|
+
# redirect to `http://example.org/bowwow.html` with 301.
|
36
36
|
redirect '/dogs/bark.html' do
|
37
37
|
new_host 'example.org'
|
38
38
|
to '/bowwow.html'
|
@@ -40,12 +40,14 @@ use Rack::Joint do
|
|
40
40
|
end
|
41
41
|
|
42
42
|
# If you access `http://example.com/cats.html`,
|
43
|
-
# redirect to `
|
43
|
+
# redirect to `https://example.org/meow/mew` with 302.
|
44
44
|
redirect '/cats.html' do
|
45
45
|
new_host 'example.org'
|
46
|
+
ssl true
|
46
47
|
to '/meow/mew'
|
47
48
|
status 302
|
48
49
|
end
|
50
|
+
end
|
49
51
|
|
50
52
|
host 'example.net' do
|
51
53
|
# If you access `http://example.net/frog.html`,
|
@@ -53,11 +55,11 @@ use Rack::Joint do
|
|
53
55
|
redirect '/frog.html' do
|
54
56
|
to '/croak'
|
55
57
|
end
|
56
|
-
|
58
|
+
|
57
59
|
# If you access `http://example.net/quack`,
|
58
60
|
# redirect to `http://example.org/quack` with 301.
|
59
61
|
redirect '/quack' do
|
60
|
-
new_host 'example.org'
|
62
|
+
new_host 'example.org'
|
61
63
|
end
|
62
64
|
end
|
63
65
|
end
|
@@ -67,13 +69,14 @@ run lambda { |env| [200, {'Content-Type' => 'text/plain'}, ['Hello World!']] }
|
|
67
69
|
|
68
70
|
You can use following resources with block.
|
69
71
|
|
70
|
-
| Resource | Description |
|
71
|
-
| :------- | :---------- |
|
72
|
-
| `host` | Host name. |
|
73
|
-
| `redirect` | Path name. |
|
74
|
-
| `
|
75
|
-
| `
|
76
|
-
| `
|
72
|
+
| Resource | Type | Description |
|
73
|
+
| :------- | :--- | :---------- |
|
74
|
+
| `host` | String | Host name. |
|
75
|
+
| `redirect` | String | Path name. |
|
76
|
+
| `ssl` | Boolean | Whether to enable SSL or not; which is `false` to default. |
|
77
|
+
| `new_host` | String | A new host name redirects to. Optional. |
|
78
|
+
| `to` | String | A new path name redirects to. Optional. |
|
79
|
+
| `status` | Integer | Status when redirecting. You can use `301`, `302`, `303`, `307`, `308`; which is `301` to default. |
|
77
80
|
|
78
81
|
## Development
|
79
82
|
|
data/lib/rack/joint.rb
CHANGED
@@ -6,21 +6,20 @@ require "rack/joint/version"
|
|
6
6
|
|
7
7
|
module Rack
|
8
8
|
class Joint
|
9
|
-
attr_reader :app
|
10
|
-
attr_reader :mappings
|
9
|
+
attr_reader :app, :block
|
11
10
|
def initialize(app, &block)
|
12
11
|
@app = app
|
13
|
-
@
|
12
|
+
@block = block if block_given?
|
14
13
|
end
|
15
14
|
|
16
15
|
def call(env)
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
16
|
+
rack_request = Rack::Request.new(env)
|
17
|
+
mappings = Context.new(rack_request).instance_exec(&block)
|
18
|
+
env_host = rack_request.host
|
19
|
+
responses = valid_mapping(mappings, env_host)
|
21
20
|
|
22
|
-
if check_redirect?(
|
23
|
-
redirect_info(
|
21
|
+
if check_redirect?(rack_request, responses)
|
22
|
+
redirect_info(rack_request, responses)
|
24
23
|
else
|
25
24
|
app.call(env)
|
26
25
|
end
|
@@ -28,28 +27,31 @@ module Rack
|
|
28
27
|
|
29
28
|
private
|
30
29
|
|
31
|
-
# @param
|
30
|
+
# @param mapping [Array] URI mappings for redirecting.
|
31
|
+
# @param host [String] Requested hostname(env).
|
32
32
|
# @return [Array] Return URL mapped responses.
|
33
|
-
def valid_mapping(host)
|
33
|
+
def valid_mapping(mappings, host)
|
34
34
|
return [] unless mappings
|
35
35
|
mappings.select { |res| res[host] }.first.values.flatten(1)
|
36
36
|
end
|
37
37
|
|
38
|
-
# @param
|
39
|
-
# @param
|
38
|
+
# @param request [Rack::Request] Request env.
|
39
|
+
# @param responses [Array] Mapped redirect response information.
|
40
40
|
# @return [Boolean]
|
41
|
-
def check_redirect?(
|
42
|
-
|
43
|
-
|
41
|
+
def check_redirect?(request, responses)
|
42
|
+
current_url = request.url
|
43
|
+
responses.map do |res|
|
44
|
+
res[2].to_s.include?(current_url)
|
44
45
|
end.include?(true)
|
45
46
|
end
|
46
47
|
|
47
|
-
# @param
|
48
|
-
# @param
|
48
|
+
# @param request [Rack::Request] Request env.
|
49
|
+
# @param responses [Array] Mapped redirect response information.
|
49
50
|
# @return [Array] Return response corresponded request.
|
50
|
-
def redirect_info(
|
51
|
-
|
52
|
-
|
51
|
+
def redirect_info(request, responses)
|
52
|
+
current_url = request.url
|
53
|
+
responses.select do |res|
|
54
|
+
res[2].to_s.include?(current_url)
|
53
55
|
end.to_a.flatten(1)
|
54
56
|
end
|
55
57
|
end
|
data/lib/rack/joint/context.rb
CHANGED
@@ -1,8 +1,9 @@
|
|
1
1
|
module Rack
|
2
2
|
class Joint
|
3
3
|
class Context
|
4
|
-
attr_reader :responses
|
5
|
-
def initialize
|
4
|
+
attr_reader :request, :responses
|
5
|
+
def initialize(request)
|
6
|
+
@request = request
|
6
7
|
@responses = []
|
7
8
|
end
|
8
9
|
|
@@ -11,9 +12,9 @@ module Rack
|
|
11
12
|
# @return [Array] Return Array consisted of block under `redirect`.
|
12
13
|
def host(old_host, &block)
|
13
14
|
responses << {
|
14
|
-
"#{old_host}" => Redirect.new(old_host).instance_exec(&block)
|
15
|
+
"#{old_host}" => Redirect.new(request, old_host).instance_exec(&block)
|
15
16
|
}
|
16
17
|
end
|
17
18
|
end
|
18
19
|
end
|
19
|
-
end
|
20
|
+
end
|
data/lib/rack/joint/redirect.rb
CHANGED
@@ -3,10 +3,10 @@
|
|
3
3
|
module Rack
|
4
4
|
class Joint
|
5
5
|
class Redirect
|
6
|
-
attr_reader :responses
|
7
|
-
|
8
|
-
def initialize(old_host)
|
6
|
+
attr_reader :responses, :request, :old_host
|
7
|
+
def initialize(request, old_host)
|
9
8
|
@responses = []
|
9
|
+
@request = request
|
10
10
|
@old_host = old_host
|
11
11
|
end
|
12
12
|
|
@@ -14,7 +14,7 @@ module Rack
|
|
14
14
|
# @param &block [block] Given block with `redirect`.
|
15
15
|
# @return [Array] Return Array consisted of block under `redirect`.
|
16
16
|
def redirect(old_path, &block)
|
17
|
-
responses << RedirectInterface.new(old_host, old_path, &block).apply!
|
17
|
+
responses << RedirectInterface.new(request, old_host, old_path, &block).apply!
|
18
18
|
end
|
19
19
|
end
|
20
20
|
end
|
@@ -6,22 +6,22 @@ module Rack
|
|
6
6
|
class BadRedirectError < StandardError; end
|
7
7
|
|
8
8
|
class RedirectInterface
|
9
|
-
attr_reader :old_host
|
10
|
-
|
11
|
-
|
12
|
-
|
9
|
+
attr_reader :request, :old_host, :old_path, :old_url
|
10
|
+
def initialize(request, old_host, old_path, &block)
|
11
|
+
@status = 301
|
12
|
+
@ssl = false
|
13
|
+
@request = request
|
13
14
|
@old_host = old_host
|
14
15
|
@old_path = old_path
|
15
|
-
@old_url = build_uri(old_host, old_path)
|
16
|
+
@old_url = build_uri(request.scheme, old_host, old_path)
|
16
17
|
instance_exec(&block)
|
17
18
|
end
|
18
19
|
|
19
20
|
# @return [Array] Return response given parameters in `config.ru`.
|
20
21
|
def apply!
|
21
|
-
@status ||= 301
|
22
22
|
@new_host ||= old_host
|
23
23
|
@new_path ||= old_path
|
24
|
-
new_location = build_uri(@new_host, @new_path)
|
24
|
+
new_location = build_uri(uri_scheme, @new_host, @new_path)
|
25
25
|
if old_url == new_location
|
26
26
|
raise BadRedirectError.new('Redirect URL has been declared the same as current URL.')
|
27
27
|
end
|
@@ -30,11 +30,31 @@ module Rack
|
|
30
30
|
|
31
31
|
private
|
32
32
|
|
33
|
-
# @
|
34
|
-
|
33
|
+
# @return [String] `http` or `https`
|
34
|
+
def uri_scheme
|
35
|
+
if @ssl
|
36
|
+
'https'
|
37
|
+
else
|
38
|
+
'http'
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# @param scheme [String] 'http' or 'https'
|
43
|
+
# @param host [String] Host name
|
44
|
+
# @param path [String] Path name
|
35
45
|
# @return [URI]
|
36
|
-
def build_uri(host, path)
|
37
|
-
|
46
|
+
def build_uri(scheme, host, path)
|
47
|
+
if scheme == 'https'
|
48
|
+
URI::HTTPS.build({ scheme: scheme, host: host, path: path }).to_s
|
49
|
+
else
|
50
|
+
URI::HTTP.build({ scheme: scheme, host: host, path: path }).to_s
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
# @param scheme [Boolean] Wether enabling SSL or not.
|
55
|
+
# @return [Boolean]
|
56
|
+
def ssl(scheme)
|
57
|
+
@ssl = scheme
|
38
58
|
end
|
39
59
|
|
40
60
|
# @param status [Integer] `status` parameter when redirecting in `config.ru`.
|
data/lib/rack/joint/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-joint
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Akito Kasai
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-08-
|
11
|
+
date: 2018-08-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack
|