rack-multiplexer 0.0.1
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 +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +25 -0
- data/Rakefile +1 -0
- data/config.ru +11 -0
- data/lib/rack-multiplexer.rb +1 -0
- data/lib/rack/multiplexer.rb +105 -0
- data/lib/rack/multiplexer/version.rb +5 -0
- data/rack-multiplexer.gemspec +26 -0
- data/spec/rack/multiplexer_spec.rb +105 -0
- data/spec/spec_helper.rb +8 -0
- metadata +143 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 8d1e0bdf595729dcdd3fac639b011df34a1b61c7
|
4
|
+
data.tar.gz: b65076613cb6838caadf1c331e95b45c2c5caed9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f07494becb8829ad92ec7162e1c13bf639f1d1ef6f24a4a66d53bf984d19962d1e0afd8de52d233953ef0f79ed01248cbfec6c32fad1bbf45afd7af5df02e3fb
|
7
|
+
data.tar.gz: 1fca83e571656bc8d76b62b61d9e2bc96a014cf29dafaa262c8db2d13e45d15c0dd89744003807f04e5eabb42ddb05b7114351d5d12333a15a2bac63574c1d9f
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Ryo Nakamura
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# Rack::Multiplexer
|
2
|
+
Provides a simple router & dispatcher for Rack applications.
|
3
|
+
|
4
|
+
## Installation
|
5
|
+
```
|
6
|
+
gem install rack-multiplexer
|
7
|
+
```
|
8
|
+
|
9
|
+
## Usage
|
10
|
+
For `rackup`.
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
# config.ru
|
14
|
+
require "rack-multiplexer"
|
15
|
+
|
16
|
+
multiplexer = Rack::Multiplexer.new
|
17
|
+
multiplexer.get("/a", ->(env) { [200, {}, ["a"]] })
|
18
|
+
multiplexer.get("/b", ->(env) { [200, {}, ["b"]] })
|
19
|
+
multiplexer.post("/c", ->(env) { [200, {}, ["c"]] })
|
20
|
+
multiplexer.put("/d", ->(env) { [200, {}, ["c"]] })
|
21
|
+
multiplexer.delete("/e", ->(env) { [200, {}, ["c"]] })
|
22
|
+
multiplexer.get("/f/:g", ->(env) { [200, {}, [env["rack.request.query_hash"]["g"]]] })
|
23
|
+
|
24
|
+
run multiplexer
|
25
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/config.ru
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require "rack-multiplexer"
|
2
|
+
|
3
|
+
multiplexer = Rack::Multiplexer.new
|
4
|
+
multiplexer.get("/a", ->(env) { [200, {}, ["a"]] })
|
5
|
+
multiplexer.get("/b", ->(env) { [200, {}, ["b"]] })
|
6
|
+
multiplexer.post("/c", ->(env) { [200, {}, ["c"]] })
|
7
|
+
multiplexer.put("/d", ->(env) { [200, {}, ["c"]] })
|
8
|
+
multiplexer.delete("/e", ->(env) { [200, {}, ["c"]] })
|
9
|
+
multiplexer.get("/f/:g", ->(env) { [200, {}, [env["rack.request.query_hash"]["g"]]] })
|
10
|
+
|
11
|
+
run multiplexer
|
@@ -0,0 +1 @@
|
|
1
|
+
require "rack/multiplexer"
|
@@ -0,0 +1,105 @@
|
|
1
|
+
require "rack/multiplexer/version"
|
2
|
+
require "rack/request"
|
3
|
+
|
4
|
+
module Rack
|
5
|
+
class Multiplexer
|
6
|
+
DEFAULT_NOT_FOUND_APPLICATION = ->(env) {
|
7
|
+
[
|
8
|
+
404,
|
9
|
+
{
|
10
|
+
"Content-Type" => "text/plain",
|
11
|
+
"Content-Length" => "0",
|
12
|
+
},
|
13
|
+
[""],
|
14
|
+
]
|
15
|
+
}
|
16
|
+
|
17
|
+
def initialize(not_found_application = DEFAULT_NOT_FOUND_APPLICATION, &block)
|
18
|
+
@not_found_application = not_found_application
|
19
|
+
instance_eval(&block) if block
|
20
|
+
end
|
21
|
+
|
22
|
+
def call(env)
|
23
|
+
path = env["PATH_INFO"]
|
24
|
+
(
|
25
|
+
routes[env["REQUEST_METHOD"]].find {|route| route.match?(path) } ||
|
26
|
+
routes["ANY"].find {|route| route.match?(path) } ||
|
27
|
+
@not_found_application
|
28
|
+
).call(env)
|
29
|
+
end
|
30
|
+
|
31
|
+
def get(pattern, application)
|
32
|
+
append("GET", pattern, application)
|
33
|
+
end
|
34
|
+
|
35
|
+
def post(pattern, application)
|
36
|
+
append("POST", pattern, application)
|
37
|
+
end
|
38
|
+
|
39
|
+
def put(pattern, application)
|
40
|
+
append("PUT", pattern, application)
|
41
|
+
end
|
42
|
+
|
43
|
+
def delete(pattern, application)
|
44
|
+
append("DELETE", pattern, application)
|
45
|
+
end
|
46
|
+
|
47
|
+
def any(pattern, application)
|
48
|
+
append("ANY", pattern, application)
|
49
|
+
end
|
50
|
+
|
51
|
+
def append(method, pattern, application)
|
52
|
+
routes[method] << Route.new(pattern, application)
|
53
|
+
end
|
54
|
+
|
55
|
+
# @routes are indexed by method.
|
56
|
+
def routes
|
57
|
+
@routes ||= Hash.new {|hash, key| hash[key] = [] }
|
58
|
+
end
|
59
|
+
|
60
|
+
def default_not_found_application
|
61
|
+
->(env) {
|
62
|
+
[
|
63
|
+
404,
|
64
|
+
{
|
65
|
+
"Content-Type" => "text/plain",
|
66
|
+
"Content-Length" => 0,
|
67
|
+
},
|
68
|
+
[""],
|
69
|
+
]
|
70
|
+
}
|
71
|
+
end
|
72
|
+
|
73
|
+
class Route
|
74
|
+
PLACEHOLDER_REGEXP = /:(\w+)/
|
75
|
+
|
76
|
+
def initialize(pattern, application)
|
77
|
+
@application = application
|
78
|
+
@regexp, @keys = compile(pattern)
|
79
|
+
end
|
80
|
+
|
81
|
+
def call(env)
|
82
|
+
request = Rack::Request.new(env)
|
83
|
+
data = @regexp.match(env["PATH_INFO"])
|
84
|
+
(data.size - 1).times {|i| request.update_param(@keys[i], data[i + 1]) }
|
85
|
+
@application.call(request.env)
|
86
|
+
end
|
87
|
+
|
88
|
+
def match?(path)
|
89
|
+
@regexp === path
|
90
|
+
end
|
91
|
+
|
92
|
+
def compile(pattern)
|
93
|
+
keys = []
|
94
|
+
segments = []
|
95
|
+
pattern.split("/").each do |segment|
|
96
|
+
segments << segment.gsub(PLACEHOLDER_REGEXP, "([^#?/]+)")
|
97
|
+
if key = Regexp.last_match(1)
|
98
|
+
keys << key
|
99
|
+
end
|
100
|
+
end
|
101
|
+
return Regexp.new("\\A#{segments.join(?/)}\\z"), keys
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'rack/multiplexer/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "rack-multiplexer"
|
8
|
+
spec.version = Rack::Multiplexer::VERSION
|
9
|
+
spec.authors = ["Ryo Nakamura"]
|
10
|
+
spec.email = ["r7kamura@gmail.com"]
|
11
|
+
spec.summary = "Provides a simple router & dispatcher for Rack."
|
12
|
+
spec.homepage = "https://github.com/r7kamura/rack-multiplexer"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files`.split($/)
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_dependency "rack"
|
21
|
+
spec.add_dependency "rspec", ">= 2.14.1"
|
22
|
+
spec.add_development_dependency "activesupport", ">= 3.2.14"
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
24
|
+
spec.add_development_dependency "pry"
|
25
|
+
spec.add_development_dependency "rake"
|
26
|
+
end
|
@@ -0,0 +1,105 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "active_support/core_ext/object/to_query"
|
3
|
+
require "active_support/core_ext/object/try"
|
4
|
+
|
5
|
+
describe Rack::Multiplexer do
|
6
|
+
let(:application) do
|
7
|
+
->(env) {
|
8
|
+
[
|
9
|
+
200,
|
10
|
+
{},
|
11
|
+
["#{env['PATH_INFO']}?#{env['rack.request.query_hash'].try(:to_param)}&#{env['QUERY_STRING']}"]
|
12
|
+
]
|
13
|
+
}
|
14
|
+
end
|
15
|
+
|
16
|
+
let(:env) do
|
17
|
+
{
|
18
|
+
"SCRIPT_NAME" => "",
|
19
|
+
"SERVER_NAME" => "localhost",
|
20
|
+
"SERVER_PORT" => "80",
|
21
|
+
"rack.input" => StringIO.new(""),
|
22
|
+
}
|
23
|
+
end
|
24
|
+
|
25
|
+
describe "#call" do
|
26
|
+
context "with unrelated path request" do
|
27
|
+
it "sends request to default not found application" do
|
28
|
+
multiplexer = described_class.new
|
29
|
+
multiplexer.call(env.merge("REQUEST_METHOD" => "GET", "PATH_INFO" => "/a")).should == [
|
30
|
+
404,
|
31
|
+
{ "Content-Type" => "text/plain", "Content-Length" => 0 },
|
32
|
+
[""],
|
33
|
+
]
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
context "with custom not found application" do
|
38
|
+
it "sends request to custom not found application" do
|
39
|
+
multiplexer = described_class.new(->(env) { [404, {}, ["custom"]] })
|
40
|
+
multiplexer.call(env.merge("REQUEST_METHOD" => "GET", "PATH_INFO" => "/a")).should == [
|
41
|
+
404,
|
42
|
+
{},
|
43
|
+
["custom"],
|
44
|
+
]
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
context "with unrelated method" do
|
49
|
+
it "delegates to not found application" do
|
50
|
+
multiplexer = described_class.new
|
51
|
+
multiplexer.post("/a", application)
|
52
|
+
multiplexer.call(env.merge("REQUEST_METHOD" => "GET", "PATH_INFO" => "/a"))[0].should == 404
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
context "with related method" do
|
57
|
+
it "delegates to registered application" do
|
58
|
+
multiplexer = described_class.new
|
59
|
+
multiplexer.get("/a", application)
|
60
|
+
multiplexer.call(env.merge("REQUEST_METHOD" => "GET", "PATH_INFO" => "/a"))[0].should == 200
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
context "with 2 registered routes" do
|
65
|
+
it "delegates to first-registered application" do
|
66
|
+
multiplexer = described_class.new
|
67
|
+
multiplexer.get("/a", application)
|
68
|
+
multiplexer.get("/:any", application)
|
69
|
+
multiplexer.call(env.merge("REQUEST_METHOD" => "GET", "PATH_INFO" => "/a"))[2][0].should == "/a?&"
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
context "with path parameters pattern" do
|
74
|
+
it "delegates with rack.request.query_hash" do
|
75
|
+
multiplexer = described_class.new
|
76
|
+
multiplexer.get("/:any", application)
|
77
|
+
multiplexer.call(env.merge("REQUEST_METHOD" => "GET", "PATH_INFO" => "/a"))[2][0].should == "/a?any=a&"
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
context "with duplicated params in path & query string" do
|
82
|
+
it "delegates with rack.request.query_hash & QUERY_STRING" do
|
83
|
+
multiplexer = described_class.new
|
84
|
+
multiplexer.get("/:any", application)
|
85
|
+
multiplexer.call(
|
86
|
+
env.merge(
|
87
|
+
"REQUEST_METHOD" => "GET",
|
88
|
+
"PATH_INFO" => "/a",
|
89
|
+
"QUERY_STRING" => "any=b"
|
90
|
+
)
|
91
|
+
)[2][0].should == "/a?any=a&any=b"
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
context "with any routing" do
|
96
|
+
it "matches any method" do
|
97
|
+
multiplexer = described_class.new
|
98
|
+
multiplexer.any("/a", application)
|
99
|
+
multiplexer.call(env.merge("REQUEST_METHOD" => "GET", "PATH_INFO" => "/a"))[0].should == 200
|
100
|
+
multiplexer.call(env.merge("REQUEST_METHOD" => "POST", "PATH_INFO" => "/a"))[0].should == 200
|
101
|
+
multiplexer.call(env.merge("REQUEST_METHOD" => "HEAD", "PATH_INFO" => "/a"))[0].should == 200
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,143 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rack-multiplexer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ryo Nakamura
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-10-19 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: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 2.14.1
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 2.14.1
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: activesupport
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 3.2.14
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 3.2.14
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: bundler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.3'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.3'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pry
|
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
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rake
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description:
|
98
|
+
email:
|
99
|
+
- r7kamura@gmail.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- .gitignore
|
105
|
+
- Gemfile
|
106
|
+
- LICENSE.txt
|
107
|
+
- README.md
|
108
|
+
- Rakefile
|
109
|
+
- config.ru
|
110
|
+
- lib/rack-multiplexer.rb
|
111
|
+
- lib/rack/multiplexer.rb
|
112
|
+
- lib/rack/multiplexer/version.rb
|
113
|
+
- rack-multiplexer.gemspec
|
114
|
+
- spec/rack/multiplexer_spec.rb
|
115
|
+
- spec/spec_helper.rb
|
116
|
+
homepage: https://github.com/r7kamura/rack-multiplexer
|
117
|
+
licenses:
|
118
|
+
- MIT
|
119
|
+
metadata: {}
|
120
|
+
post_install_message:
|
121
|
+
rdoc_options: []
|
122
|
+
require_paths:
|
123
|
+
- lib
|
124
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
125
|
+
requirements:
|
126
|
+
- - '>='
|
127
|
+
- !ruby/object:Gem::Version
|
128
|
+
version: '0'
|
129
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
130
|
+
requirements:
|
131
|
+
- - '>='
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: '0'
|
134
|
+
requirements: []
|
135
|
+
rubyforge_project:
|
136
|
+
rubygems_version: 2.0.3
|
137
|
+
signing_key:
|
138
|
+
specification_version: 4
|
139
|
+
summary: Provides a simple router & dispatcher for Rack.
|
140
|
+
test_files:
|
141
|
+
- spec/rack/multiplexer_spec.rb
|
142
|
+
- spec/spec_helper.rb
|
143
|
+
has_rdoc:
|