rack-joint 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 5e1153300da20c9b6f0743ad1987ed7c6981fe845fc50773de47d249600bc71b
4
+ data.tar.gz: 4fd6e1c37560e6d03df409728216ebb2e694282fd9273103355129b626d7dea9
5
+ SHA512:
6
+ metadata.gz: d61bb17986048e07e0a29450b1a2d93a2d98d6e59932b2353fb3ec21d005f2f47c30a36fe9c910ecc28edce64582978bb809019a81340d8a11ce67feac19db94
7
+ data.tar.gz: 7234c8b134d1f9858e32e51c747f95e3a908372f22c2659697878e77222e75dc767c70f7fc04f925a02818fecd238816bac06a193ef3cd3eeba28a09fd540463
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /.idea
10
+ tags
11
+ .ruby-version
12
+ Gemfile.lock
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.4
5
+ - 2.5.1
6
+ before_install: gem install bundler -v 1.16.4
7
+ script: bundle exec rake test
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in rack-joint.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Akito Kasai
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.md ADDED
@@ -0,0 +1,90 @@
1
+ # Rack::Joint
2
+
3
+ Rack::Joint is a [Rack](https://github.com/rack/rack) middleware which helps to redirect with Ruby DSL.
4
+
5
+ ### Requirements
6
+ Rack::Joint requires Ruby 2.4+.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'rack-joint'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install rack-joint
23
+
24
+ ## Usage
25
+
26
+ Write settings in `config.ru`. This is a sample:
27
+
28
+ ```ruby
29
+ # config.ru
30
+ require 'rack/joint'
31
+
32
+ use Rack::Joint do
33
+ host 'example.com' do
34
+ # If you access `http://example.com/dogs/bark.html`,
35
+ # redirect to `http://example.org/bowwow.html` with 301.
36
+ redirect '/dogs/bark.html' do
37
+ new_host 'example.org'
38
+ to '/bowwow.html'
39
+ status 301
40
+ end
41
+
42
+ # If you access `http://example.com/cats.html`,
43
+ # redirect to `http://example.org/meow/mew` with 302.
44
+ redirect '/cats.html' do
45
+ new_host 'example.org'
46
+ to '/meow/mew'
47
+ status 302
48
+ end
49
+
50
+ host 'example.net' do
51
+ # If you access `http://example.net/frog.html`,
52
+ # redirect to `http://example.net/croak` with 301.
53
+ redirect '/frog.html' do
54
+ to '/croak'
55
+ end
56
+
57
+ # If you access `http://example.net/quack`,
58
+ # redirect to `http://example.org/quack` with 301.
59
+ redirect '/quack' do
60
+ new_host 'example.org'
61
+ end
62
+ end
63
+ end
64
+
65
+ run lambda { |env| [200, {'Content-Type' => 'text/plain'}, ['Hello World!']] }
66
+ ```
67
+
68
+ You can use following resources with block.
69
+
70
+ | Resource | Description |
71
+ | :------- | :---------- |
72
+ | `host` | Host name. |
73
+ | `redirect` | Path name. |
74
+ | `new_host` | A new host name redirects to. Optional. |
75
+ | `to` | A new path name redirects to. Optional. |
76
+ | `status` | Status when redirecting. You can use `301`, `302`, `303`, `307`, `308`; which is `301` to default. |
77
+
78
+ ## Development
79
+
80
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
81
+
82
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
83
+
84
+ ## Contributing
85
+
86
+ Bug reports and pull requests are welcome on GitHub at https://github.com/akito19/rack-joint.
87
+
88
+ ## License
89
+
90
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rack/joint"
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/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,19 @@
1
+ module Rack
2
+ class Joint
3
+ class Context
4
+ attr_reader :responses
5
+ def initialize
6
+ @responses = []
7
+ end
8
+
9
+ # @param old_host [String] Hostname set as argument in `config.ru`.
10
+ # @param &block [block] Given block with `host`.
11
+ # @return [Array] Return Array consisted of block under `redirect`.
12
+ def host(old_host, &block)
13
+ responses << {
14
+ "#{old_host}" => Redirect.new(old_host).instance_exec(&block)
15
+ }
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rack
4
+ class Joint
5
+ class Redirect
6
+ attr_reader :responses
7
+ attr_reader :old_host
8
+ def initialize(old_host)
9
+ @responses = []
10
+ @old_host = old_host
11
+ end
12
+
13
+ # @param old_path [String] Path set as argument in `config.ru`.
14
+ # @param &block [block] Given block with `redirect`.
15
+ # @return [Array] Return Array consisted of block under `redirect`.
16
+ def redirect(old_path, &block)
17
+ responses << RedirectInterface.new(old_host, old_path, &block).apply!
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+ require 'uri'
3
+
4
+ module Rack
5
+ class Joint
6
+ class BadRedirectError < StandardError; end
7
+
8
+ class RedirectInterface
9
+ attr_reader :old_host
10
+ attr_reader :old_path
11
+ attr_reader :old_url
12
+ def initialize(old_host, old_path, &block)
13
+ @old_host = old_host
14
+ @old_path = old_path
15
+ @old_url = build_uri(old_host, old_path)
16
+ instance_exec(&block)
17
+ end
18
+
19
+ # @return [Array] Return response given parameters in `config.ru`.
20
+ def apply!
21
+ @status ||= 301
22
+ @new_host ||= old_host
23
+ @new_path ||= old_path
24
+ new_location = build_uri(@new_host, @new_path)
25
+ if old_url == new_location
26
+ raise BadRedirectError.new('Redirect URL has been declared the same as current URL.')
27
+ end
28
+ [@status, { 'Location' => new_location, 'Content-Type' => 'text/html', 'Content-Language' => '0' }, ["Redirect from: #{old_url}"]]
29
+ end
30
+
31
+ private
32
+
33
+ # @param host [String]
34
+ # @param path [String]
35
+ # @return [URI]
36
+ def build_uri(host, path)
37
+ URI::HTTP.build({ host: host, path: path }).to_s
38
+ end
39
+
40
+ # @param status [Integer] `status` parameter when redirecting in `config.ru`.
41
+ # @return [Integer] Return status code.
42
+ def status(status)
43
+ @status =
44
+ case status
45
+ when 302
46
+ 302
47
+ when 303
48
+ 303
49
+ when 307
50
+ 307
51
+ when 308
52
+ 308
53
+ else
54
+ 301
55
+ end
56
+ end
57
+
58
+ # @param host [String] `new_host` parameter to redirect hostname in `config.ru`.
59
+ # @return [String] Return hostname where redirects to.
60
+ def new_host(host)
61
+ @new_host = host
62
+ end
63
+
64
+ # @param path [String] `to` parameter to redirect path in `config.ru`
65
+ # @return [String] Return pathname where redirects to.
66
+ def to(path)
67
+ @new_path = path
68
+ end
69
+ end
70
+ end
71
+ end
72
+
@@ -0,0 +1,5 @@
1
+ module Rack
2
+ class Joint
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
data/lib/rack/joint.rb ADDED
@@ -0,0 +1,56 @@
1
+ require "rack"
2
+ require "rack/joint/context"
3
+ require "rack/joint/redirect"
4
+ require "rack/joint/redirect_interface"
5
+ require "rack/joint/version"
6
+
7
+ module Rack
8
+ class Joint
9
+ attr_reader :app
10
+ attr_reader :mappings
11
+ def initialize(app, &block)
12
+ @app = app
13
+ @mappings = Context.new.instance_exec(&block) if block_given?
14
+ end
15
+
16
+ def call(env)
17
+ rack_env = Rack::Request.new(env)
18
+ env_host = rack_env.host
19
+ env_path = rack_env.fullpath
20
+ requests = valid_mapping(env_host)
21
+
22
+ if check_redirect?(requests, env_path)
23
+ redirect_info(requests, env_path)
24
+ else
25
+ app.call(env)
26
+ end
27
+ end
28
+
29
+ private
30
+
31
+ # @param host [String] Requested hostname(env)
32
+ # @return [Array] Return URL mapped responses.
33
+ def valid_mapping(host)
34
+ return [] unless mappings
35
+ mappings.select { |res| res[host] }.first.values.flatten(1)
36
+ end
37
+
38
+ # @param requests [Array] Mapped redirect response information.
39
+ # @param path [String] Requested pathname(env)
40
+ # @return [Boolean]
41
+ def check_redirect?(requests, path)
42
+ requests.map do |req|
43
+ req[2].to_s.include?(path)
44
+ end.include?(true)
45
+ end
46
+
47
+ # @param requests [Array] Mapped redirect response information.
48
+ # @param path [String] Requested pathname(env)
49
+ # @return [Array] Return response corresponded request.
50
+ def redirect_info(requests, path)
51
+ requests.select do |req|
52
+ req[2].to_s.include?(path)
53
+ end.to_a.flatten(1)
54
+ end
55
+ end
56
+ end
data/lib/rack-joint.rb ADDED
@@ -0,0 +1 @@
1
+ require 'rack/joint'
@@ -0,0 +1,29 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "rack/joint/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rack-joint"
8
+ spec.version = Rack::Joint::VERSION
9
+ spec.authors = ["Akito Kasai"]
10
+ spec.email = ["kasai@akito19.com"]
11
+
12
+ spec.summary = %q{A rack middleware for redirecting.}
13
+ spec.description = %q{A rack middleware to help you to redirect.}
14
+ spec.homepage = "https://github.com/akito19/rack-joint"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
18
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_dependency "rack"
25
+ spec.add_development_dependency "bundler", "~> 1.16"
26
+ spec.add_development_dependency "rake", "~> 12.0"
27
+ spec.add_development_dependency "minitest", "~> 5.0"
28
+ spec.add_development_dependency "rack-test"
29
+ end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-joint
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Akito Kasai
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-08-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.16'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.16'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '12.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '12.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rack-test
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: A rack middleware to help you to redirect.
84
+ email:
85
+ - kasai@akito19.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".travis.yml"
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - bin/console
97
+ - bin/setup
98
+ - lib/rack-joint.rb
99
+ - lib/rack/joint.rb
100
+ - lib/rack/joint/context.rb
101
+ - lib/rack/joint/redirect.rb
102
+ - lib/rack/joint/redirect_interface.rb
103
+ - lib/rack/joint/version.rb
104
+ - rack-joint.gemspec
105
+ homepage: https://github.com/akito19/rack-joint
106
+ licenses:
107
+ - MIT
108
+ metadata: {}
109
+ post_install_message:
110
+ rdoc_options: []
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements: []
124
+ rubyforge_project:
125
+ rubygems_version: 2.7.6
126
+ signing_key:
127
+ specification_version: 4
128
+ summary: A rack middleware for redirecting.
129
+ test_files: []