soar_sc-rack-router 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 +9 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +117 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/soar_sc/rack/router/builder_syntax.rb +47 -0
- data/lib/soar_sc/rack/router/errors.rb +13 -0
- data/lib/soar_sc/rack/router/route.rb +51 -0
- data/lib/soar_sc/rack/router/version.rb +7 -0
- data/lib/soar_sc/rack/router.rb +53 -0
- data/soar_sc-rack-router.gemspec +27 -0
- metadata +117 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ea1b4cfbce696717b0a1ba75823c3dc7eb1ce77b
|
4
|
+
data.tar.gz: af2320579e449daa1b2716fbbbb4b7a7b6a15f1c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9e412c1fbb014a7743f5950bf9a1aadd03f7b9aff5f827a1cedfd38ed66fa66582a2b67ad204f59991b89092c3e757b05a74b5e06731c92f29a6cafa8a9780f7
|
7
|
+
data.tar.gz: edb1ecd42f5ec8a6a3699deeaf7516cc306b01a7212a5005289087bc8ab675daa787876e2b6ec5b72532208ae43ede486a328017789536bc9c783e5a8d41b967
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Sheldon Hearn
|
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,117 @@
|
|
1
|
+
# SoarSc::Rack::Router
|
2
|
+
|
3
|
+
SoarSc::Rack::Router is a middleware-centric rack router. It has three key features:
|
4
|
+
|
5
|
+
* It is implemented as middleware.
|
6
|
+
* Its routing actions are rack apps (possibly supporting middleware through Rack::Builder).
|
7
|
+
* It supports parameterized URLs.
|
8
|
+
|
9
|
+
Because it is implemented as middleware, it supports fall-through. Therefore, it is possible to layer multiple routers into a single stack.
|
10
|
+
|
11
|
+
Because its routing actions are rack apps, it supports the selection of different stacks of middleware for different routes in a single router.
|
12
|
+
|
13
|
+
Because it supports parameterized URLs, it is familiar to users of several Ruby web frameworks, but also supports trivial translation of routes
|
14
|
+
into WADL and other service descriptions. The parameter support is compatible with Rack::Request, without pushing a dependency on Rack::Request
|
15
|
+
down into the router action API; the only requirement for router actions is that they are rack apps.
|
16
|
+
|
17
|
+
## Installation
|
18
|
+
|
19
|
+
Add this line to your application's Gemfile:
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
gem 'soar_sc-rack-router'
|
23
|
+
```
|
24
|
+
|
25
|
+
And then execute:
|
26
|
+
|
27
|
+
$ bundle
|
28
|
+
|
29
|
+
Or install it yourself as:
|
30
|
+
|
31
|
+
$ gem install soar_sc-rack-router
|
32
|
+
|
33
|
+
## Usage
|
34
|
+
|
35
|
+
The following are not examples of sensible ways to structure your router actions. They just demonstrate the SoarSc::Rack::Router API.
|
36
|
+
|
37
|
+
An example of a mult-router stack:
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
require 'soar_sc/rack/router'
|
41
|
+
|
42
|
+
stack = Rack::Builder.new do
|
43
|
+
use SoarSc::Rack::Router do
|
44
|
+
map "/", ->(env) { ... }
|
45
|
+
end
|
46
|
+
|
47
|
+
use Authentication
|
48
|
+
|
49
|
+
use SoarSc::Rack::Router do
|
50
|
+
get "/business/:id", ->(env) { ... }
|
51
|
+
post "/business", ->(env) { ... }
|
52
|
+
end
|
53
|
+
|
54
|
+
run ->(env) [404, {'Content-Type' => 'text/plain'}, ['Object Not Found']]
|
55
|
+
end
|
56
|
+
|
57
|
+
run stack
|
58
|
+
```
|
59
|
+
|
60
|
+
An example of stacked routing actions under a single router:
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
require 'soar_sc/rack/router'
|
64
|
+
|
65
|
+
stack = Rack::Builder.new do
|
66
|
+
use SoarSc::Rack::Router do
|
67
|
+
map "/", ->(env) { ... }
|
68
|
+
get "/business/:id", Rack::Builder.new do
|
69
|
+
use Authentication
|
70
|
+
run ->(...)
|
71
|
+
end
|
72
|
+
post "/business", Rack::Builder.new do
|
73
|
+
use Authentication
|
74
|
+
use Auditing
|
75
|
+
run ->(...)
|
76
|
+
end
|
77
|
+
|
78
|
+
run ->(env) [404, {'Content-Type' => 'text/plain'}, ['Object Not Found']]
|
79
|
+
end
|
80
|
+
end
|
81
|
+
```
|
82
|
+
|
83
|
+
An example of parameterized URL support:
|
84
|
+
|
85
|
+
```ruby
|
86
|
+
require 'soar_sc/rack/router'
|
87
|
+
|
88
|
+
stack = Rack::Builder.new do
|
89
|
+
use SoarSc::Rack::Router do
|
90
|
+
get "/product/:id", ->(env) {
|
91
|
+
id = Rack::Request.new(env).params["id"]
|
92
|
+
...
|
93
|
+
}
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
run ->(env) [404, {'Content-Type' => 'text/plain'}, ['Object Not Found']]
|
98
|
+
end
|
99
|
+
|
100
|
+
run stack
|
101
|
+
```
|
102
|
+
|
103
|
+
## Development
|
104
|
+
|
105
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
106
|
+
|
107
|
+
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).
|
108
|
+
|
109
|
+
## Contributing
|
110
|
+
|
111
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/hetznerZA/soar_sc-rack-router.
|
112
|
+
|
113
|
+
|
114
|
+
## License
|
115
|
+
|
116
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
117
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "soar_sc/rack/router"
|
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
|
data/bin/setup
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
module SoarSc
|
2
|
+
module Rack
|
3
|
+
class Router
|
4
|
+
|
5
|
+
module BuilderSyntax
|
6
|
+
|
7
|
+
def map(path, app)
|
8
|
+
add_route(Route.new('*', path, app))
|
9
|
+
end
|
10
|
+
|
11
|
+
def get(path, app)
|
12
|
+
add_route(Route.new('GET', path, app))
|
13
|
+
end
|
14
|
+
|
15
|
+
def post(path, app)
|
16
|
+
add_route(Route.new('POST', path, app))
|
17
|
+
end
|
18
|
+
|
19
|
+
def put(path, app)
|
20
|
+
add_route(Route.new('PUT', path, app))
|
21
|
+
end
|
22
|
+
|
23
|
+
def delete(path, app)
|
24
|
+
add_route(Route.new('DELETE', path, app))
|
25
|
+
end
|
26
|
+
|
27
|
+
def options(path, app)
|
28
|
+
add_route(Route.new('OPTIONS', path, app))
|
29
|
+
end
|
30
|
+
|
31
|
+
def head(path, app)
|
32
|
+
add_route(Route.new('HEAD', path, app))
|
33
|
+
end
|
34
|
+
|
35
|
+
def trace(path, app)
|
36
|
+
add_route(Route.new('TRACE', path, app))
|
37
|
+
end
|
38
|
+
|
39
|
+
def connect(path, app)
|
40
|
+
add_route(Route.new('CONNECT', path, app))
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module SoarSc
|
2
|
+
module Rack
|
3
|
+
class Router
|
4
|
+
|
5
|
+
class Route
|
6
|
+
attr_reader :method, :path, :action
|
7
|
+
|
8
|
+
def initialize(method, path, action)
|
9
|
+
@method, @path, @action = method, path, action
|
10
|
+
end
|
11
|
+
|
12
|
+
def static?
|
13
|
+
components(path).none? { |c| c.start_with?(":") }
|
14
|
+
end
|
15
|
+
|
16
|
+
def matches?(env)
|
17
|
+
return false unless (method == env["REQUEST_METHOD"] or method == "*")
|
18
|
+
|
19
|
+
req_components = components(request_path(env))
|
20
|
+
components(path).each_with_index do |c, i|
|
21
|
+
return false unless c.start_with?(":") or c == req_components[i]
|
22
|
+
end
|
23
|
+
|
24
|
+
return true
|
25
|
+
end
|
26
|
+
|
27
|
+
def extract_path_parameters(env)
|
28
|
+
req_components = components(request_path(env))
|
29
|
+
parameters = {}
|
30
|
+
components(path).each_with_index do |c, i|
|
31
|
+
parameters[c[1..-1]] = req_components[i] if c.start_with?(":")
|
32
|
+
end
|
33
|
+
parameters
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def request_path(env)
|
39
|
+
env["SCRIPT_NAME"].to_s + env["PATH_INFO"].to_s
|
40
|
+
end
|
41
|
+
|
42
|
+
def components(path)
|
43
|
+
path.split('/')
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require "soar_sc/rack/router/builder_syntax"
|
2
|
+
require "soar_sc/rack/router/errors"
|
3
|
+
require "soar_sc/rack/router/route"
|
4
|
+
require "soar_sc/rack/router/version"
|
5
|
+
|
6
|
+
require "uri"
|
7
|
+
|
8
|
+
module SoarSc
|
9
|
+
module Rack
|
10
|
+
class Router
|
11
|
+
|
12
|
+
include BuilderSyntax
|
13
|
+
|
14
|
+
def initialize(app, routes = {}, &block)
|
15
|
+
@app = app
|
16
|
+
@static_routes = []
|
17
|
+
@parameterized_routes = []
|
18
|
+
routes.each { |(p, a)| add_route(Route.new('*', p, a)) }
|
19
|
+
instance_eval(&block) if block_given?
|
20
|
+
end
|
21
|
+
|
22
|
+
def call(env)
|
23
|
+
if route = @static_routes.detect { |r| r.matches?(env) }
|
24
|
+
route.action.call(env)
|
25
|
+
elsif route = @parameterized_routes.detect { |r| r.matches?(env) }
|
26
|
+
route.extract_path_parameters(env).each { |p, v| update_path_parameter!(env, p, v) }
|
27
|
+
route.action.call(env)
|
28
|
+
else
|
29
|
+
@app.call(env)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
# Called by BuilderSyntax methods
|
36
|
+
def add_route(route)
|
37
|
+
if (@static_routes + @parameterized_routes).detect { |r| r.path == route.path and (r.method == route.method or r.method = "*") }
|
38
|
+
raise DuplicateRouteError.for_route(route)
|
39
|
+
elsif route.static?
|
40
|
+
@static_routes << route
|
41
|
+
else
|
42
|
+
@parameterized_routes << route
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def update_path_parameter!(env, param, value)
|
47
|
+
env["QUERY_STRING"] << "&" unless env["QUERY_STRING"].empty?
|
48
|
+
env["QUERY_STRING"] << "#{param}=#{URI.escape(value)}"
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'soar_sc/rack/router/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "soar_sc-rack-router"
|
8
|
+
spec.version = SoarSc::Rack::Router::VERSION
|
9
|
+
spec.authors = ["Sheldon Hearn"]
|
10
|
+
spec.email = ["sheldonh@starjuice.net"]
|
11
|
+
|
12
|
+
spec.summary = %q{Rack routing middleware}
|
13
|
+
spec.description = %q{Rack routing middleware supporting rack apps as actions, and parameterized paths}
|
14
|
+
spec.homepage = "https://github.com/hetznerZA/soar_sc-rack-router"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_dependency "rack", "~> 1.6"
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: soar_sc-rack-router
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sheldon Hearn
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-04-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: '1.6'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
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.11'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.11'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
description: Rack routing middleware supporting rack apps as actions, and parameterized
|
70
|
+
paths
|
71
|
+
email:
|
72
|
+
- sheldonh@starjuice.net
|
73
|
+
executables: []
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- ".gitignore"
|
78
|
+
- ".rspec"
|
79
|
+
- ".travis.yml"
|
80
|
+
- Gemfile
|
81
|
+
- LICENSE.txt
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- bin/console
|
85
|
+
- bin/setup
|
86
|
+
- lib/soar_sc/rack/router.rb
|
87
|
+
- lib/soar_sc/rack/router/builder_syntax.rb
|
88
|
+
- lib/soar_sc/rack/router/errors.rb
|
89
|
+
- lib/soar_sc/rack/router/route.rb
|
90
|
+
- lib/soar_sc/rack/router/version.rb
|
91
|
+
- soar_sc-rack-router.gemspec
|
92
|
+
homepage: https://github.com/hetznerZA/soar_sc-rack-router
|
93
|
+
licenses:
|
94
|
+
- MIT
|
95
|
+
metadata: {}
|
96
|
+
post_install_message:
|
97
|
+
rdoc_options: []
|
98
|
+
require_paths:
|
99
|
+
- lib
|
100
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
requirements: []
|
111
|
+
rubyforge_project:
|
112
|
+
rubygems_version: 2.5.1
|
113
|
+
signing_key:
|
114
|
+
specification_version: 4
|
115
|
+
summary: Rack routing middleware
|
116
|
+
test_files: []
|
117
|
+
has_rdoc:
|