galago-router 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/README.md +15 -31
- data/examples/echo_param.ru +19 -0
- data/examples/lobster.ru +9 -0
- data/{galago.gemspec → galago-router.gemspec} +0 -0
- data/lib/galago/router/dsl.rb +21 -19
- data/lib/galago/router/errors.rb +15 -13
- data/lib/galago/router/path.rb +35 -30
- data/lib/galago/router/route.rb +26 -23
- data/lib/galago/router/version.rb +1 -1
- data/lib/galago/router.rb +10 -19
- data/spec/galago/router/path_spec.rb +0 -17
- data/spec/galago/router/route_spec.rb +24 -19
- data/spec/galago/router/version_spec.rb +2 -2
- data/spec/galago/router_spec.rb +9 -29
- data/spec/spec_helper.rb +0 -8
- metadata +4 -3
- data/lib/galago.rb +0 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 24dc9a36ea2bca4e8d75e2722b5ae5a78010d19f
|
4
|
+
data.tar.gz: 39497251884dfba4e67bc8746029734b13a87642
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7cba7c52f27784cb3fe98ab0b199f8d88dfd911808bdeece4039d969fa44eec49feefc99f9b908505a5003268a58aa82780daea0a197b722dcbcf2791a372286
|
7
|
+
data.tar.gz: 41bf1fdb47a22e7cdd7a873c3f8209c5ceedabaa60387bf37e2f0eb1e9a229c028dc7065383b4fa3a9b1cebaf34f1a3bf3dd289e4111e442e299f938794f56ef
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -1,13 +1,12 @@
|
|
1
|
-
# Galago
|
1
|
+
# Galago Router
|
2
2
|
|
3
|
-
|
4
|
-
I am building it to gain more appreciation for existing tools.
|
3
|
+
A rack router.
|
5
4
|
|
6
5
|
## Installation
|
7
6
|
|
8
7
|
Add this line to your application's Gemfile:
|
9
8
|
|
10
|
-
gem 'galago'
|
9
|
+
gem 'galago-router'
|
11
10
|
|
12
11
|
And then execute:
|
13
12
|
|
@@ -15,39 +14,24 @@ And then execute:
|
|
15
14
|
|
16
15
|
Or install it yourself as:
|
17
16
|
|
18
|
-
$ gem install galago
|
17
|
+
$ gem install galago-router
|
19
18
|
|
20
19
|
## Usage
|
21
20
|
|
22
21
|
```ruby
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
patch '/users/:name' do
|
34
|
-
user = User.find_by_name params['name']
|
35
|
-
user.update! params['user']
|
36
|
-
end
|
37
|
-
|
38
|
-
put '/users/:name' do
|
39
|
-
user = User.find_by_name params['name']
|
40
|
-
user.update! params['user']
|
41
|
-
end
|
42
|
-
|
43
|
-
delete '/users/:name' do
|
44
|
-
user = User.find_by_name params['name']
|
45
|
-
user.destroy!
|
46
|
-
end
|
47
|
-
end
|
22
|
+
# config.ru
|
23
|
+
require 'galago/router'
|
24
|
+
require 'rack/lobster'
|
25
|
+
|
26
|
+
router = Galago::Router.new do
|
27
|
+
get '/lobsters', to: Rack::Lobster.new
|
28
|
+
post '/lobsters', to: Rack::Lobster.new
|
29
|
+
patch '/lobsters/:name', to: Rack::Lobster.new
|
30
|
+
put '/lobsters/:name', to: Rack::Lobster.new
|
31
|
+
delete '/lobsters/:name', to: Rack::Lobster.new
|
48
32
|
end
|
49
33
|
|
50
|
-
run
|
34
|
+
run router
|
51
35
|
```
|
52
36
|
|
53
37
|
## Contributing
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'galago/router'
|
2
|
+
|
3
|
+
EchoParams = lambda { |env|
|
4
|
+
params = Rack::Request.new(env).params
|
5
|
+
Rack::Response.new(params).finish
|
6
|
+
}
|
7
|
+
|
8
|
+
router = Galago::Router.new do
|
9
|
+
get '/echo_params', to: EchoParams
|
10
|
+
get '/echo_params/:name', to: EchoParams
|
11
|
+
|
12
|
+
post '/echo_params', to: EchoParams
|
13
|
+
put '/echo_params/:name', to: EchoParams
|
14
|
+
|
15
|
+
delete '/echo_params', to: EchoParams
|
16
|
+
end
|
17
|
+
|
18
|
+
run router
|
19
|
+
|
data/examples/lobster.ru
ADDED
File without changes
|
data/lib/galago/router/dsl.rb
CHANGED
@@ -1,28 +1,30 @@
|
|
1
1
|
module Galago
|
2
|
-
class Router
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
2
|
+
class Router
|
3
|
+
class DSL
|
4
|
+
def initialize(router, block)
|
5
|
+
@router = router
|
6
|
+
instance_eval(&block)
|
7
|
+
end
|
7
8
|
|
8
|
-
|
9
|
-
|
10
|
-
|
9
|
+
def get(path, options)
|
10
|
+
@router.add_route("GET", path, options[:to])
|
11
|
+
end
|
11
12
|
|
12
|
-
|
13
|
-
|
14
|
-
|
13
|
+
def patch(path, options)
|
14
|
+
@router.add_route("PATCH", path, options[:to])
|
15
|
+
end
|
15
16
|
|
16
|
-
|
17
|
-
|
18
|
-
|
17
|
+
def post(path, options)
|
18
|
+
@router.add_route("POST", path, options[:to])
|
19
|
+
end
|
19
20
|
|
20
|
-
|
21
|
-
|
22
|
-
|
21
|
+
def put(path, options)
|
22
|
+
@router.add_route("PUT", path, options[:to])
|
23
|
+
end
|
23
24
|
|
24
|
-
|
25
|
-
|
25
|
+
def delete(path, options)
|
26
|
+
@router.add_route("DELETE", path, options[:to])
|
27
|
+
end
|
26
28
|
end
|
27
29
|
end
|
28
30
|
end
|
data/lib/galago/router/errors.rb
CHANGED
@@ -1,21 +1,23 @@
|
|
1
1
|
module Galago
|
2
|
-
class Router
|
3
|
-
|
4
|
-
|
5
|
-
|
2
|
+
class Router
|
3
|
+
class ActionInvalid < ArgumentError
|
4
|
+
def initialize(action)
|
5
|
+
@action = action
|
6
|
+
end
|
6
7
|
|
7
|
-
|
8
|
-
|
8
|
+
def message
|
9
|
+
"Action does not have a call method: #{@action.inspect}"
|
10
|
+
end
|
9
11
|
end
|
10
|
-
end
|
11
12
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
class RequestMethodInvalid < ArgumentError
|
14
|
+
def initialize(request_method)
|
15
|
+
@request_method = request_method
|
16
|
+
end
|
16
17
|
|
17
|
-
|
18
|
-
|
18
|
+
def message
|
19
|
+
"Got: #{@request_method}\nExpected one of: #{REQUEST_METHODS}"
|
20
|
+
end
|
19
21
|
end
|
20
22
|
end
|
21
23
|
end
|
data/lib/galago/router/path.rb
CHANGED
@@ -1,44 +1,49 @@
|
|
1
1
|
module Galago
|
2
|
-
class Router
|
2
|
+
class Router
|
3
|
+
class Path
|
3
4
|
|
4
|
-
|
5
|
-
|
6
|
-
|
5
|
+
def initialize(path)
|
6
|
+
@path = path.to_s
|
7
|
+
end
|
7
8
|
|
8
|
-
|
9
|
-
|
10
|
-
|
9
|
+
def recognizes?(request_path)
|
10
|
+
request_path =~ regex
|
11
|
+
end
|
11
12
|
|
12
|
-
|
13
|
-
|
14
|
-
|
13
|
+
def named_parameters
|
14
|
+
@path_parameters ||= @path.scan(/\:(\w+)/).flatten
|
15
|
+
end
|
16
|
+
|
17
|
+
def add_path_params_to_env(env)
|
18
|
+
request = Rack::Request.new(env)
|
15
19
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
request.update_param(key, value)
|
20
|
+
if path_params = identify_params_in_path(request.path)
|
21
|
+
path_params.each { |key, value| request.update_param(key, value) }
|
22
|
+
end
|
20
23
|
end
|
21
|
-
end
|
22
24
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
end
|
25
|
+
def to_s
|
26
|
+
@path
|
27
|
+
end
|
27
28
|
|
28
|
-
|
29
|
-
@path
|
30
|
-
end
|
29
|
+
private
|
31
30
|
|
32
|
-
|
31
|
+
def regex
|
32
|
+
@regex_path ||= convert_path_to_regex(@path)
|
33
|
+
end
|
33
34
|
|
34
|
-
|
35
|
-
|
36
|
-
|
35
|
+
def convert_path_to_regex(path)
|
36
|
+
regexp = path.to_s.gsub(/\:\w+/, '([\w-]+)')
|
37
|
+
Regexp.new("^#{regexp}$")
|
38
|
+
end
|
37
39
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
40
|
+
def identify_params_in_path(path)
|
41
|
+
if match = regex.match(path)
|
42
|
+
named_parameters.zip(match.captures)
|
43
|
+
end
|
44
|
+
end
|
42
45
|
|
46
|
+
end
|
43
47
|
end
|
44
48
|
end
|
49
|
+
|
data/lib/galago/router/route.rb
CHANGED
@@ -1,36 +1,39 @@
|
|
1
1
|
module Galago
|
2
|
-
class Router
|
2
|
+
class Router
|
3
|
+
class Route
|
3
4
|
|
4
|
-
|
5
|
+
attr_reader :request_method, :path, :action
|
5
6
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
7
|
+
def initialize(request_method, path, action)
|
8
|
+
@request_method = request_method.to_s.upcase
|
9
|
+
@path = Router::Path.new(path)
|
10
|
+
@action = action
|
10
11
|
|
11
|
-
|
12
|
-
|
13
|
-
|
12
|
+
validate_action!
|
13
|
+
validate_request_method!
|
14
|
+
end
|
14
15
|
|
15
|
-
|
16
|
-
|
17
|
-
|
16
|
+
def recognizes_path?(request_path)
|
17
|
+
@path.recognizes?(request_path)
|
18
|
+
end
|
18
19
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
20
|
+
def call(env)
|
21
|
+
@path.add_path_params_to_env(env)
|
22
|
+
action.call(env)
|
23
|
+
end
|
23
24
|
|
24
|
-
|
25
|
+
private
|
25
26
|
|
26
|
-
|
27
|
-
|
28
|
-
|
27
|
+
def validate_action!
|
28
|
+
action.respond_to?(:call) or raise Router::ActionInvalid.new(action)
|
29
|
+
end
|
29
30
|
|
30
|
-
|
31
|
-
|
32
|
-
|
31
|
+
def validate_request_method!
|
32
|
+
unless Router::REQUEST_METHODS.include?(request_method)
|
33
|
+
raise Router::RequestMethodInvalid.new(request_method)
|
34
|
+
end
|
33
35
|
end
|
34
36
|
end
|
35
37
|
end
|
36
38
|
end
|
39
|
+
|
data/lib/galago/router.rb
CHANGED
@@ -1,22 +1,11 @@
|
|
1
|
+
require 'galago/router/dsl'
|
2
|
+
require 'galago/router/errors'
|
3
|
+
require 'galago/router/path'
|
4
|
+
require 'galago/router/route'
|
5
|
+
require 'galago/router/version'
|
6
|
+
|
1
7
|
module Galago
|
2
8
|
class Router
|
3
|
-
require 'galago/router/dsl'
|
4
|
-
require 'galago/router/errors'
|
5
|
-
require 'galago/router/path'
|
6
|
-
require 'galago/router/route'
|
7
|
-
require 'galago/router/version'
|
8
|
-
|
9
|
-
def self.call(env)
|
10
|
-
router.process_request(env)
|
11
|
-
end
|
12
|
-
|
13
|
-
def self.routes(&block)
|
14
|
-
Router::DSL.new(router, block)
|
15
|
-
end
|
16
|
-
|
17
|
-
def self.router
|
18
|
-
@router ||= Router.new
|
19
|
-
end
|
20
9
|
|
21
10
|
REQUEST_METHODS = [
|
22
11
|
"GET",
|
@@ -28,11 +17,13 @@ module Galago
|
|
28
17
|
|
29
18
|
attr_reader :routes
|
30
19
|
|
31
|
-
def initialize
|
20
|
+
def initialize(&block)
|
32
21
|
@routes = REQUEST_METHODS.each_with_object({}) do |request_method, routes|
|
33
22
|
routes[request_method] = []
|
34
23
|
routes
|
35
24
|
end
|
25
|
+
|
26
|
+
Router::DSL.new(self, block) if block_given?
|
36
27
|
end
|
37
28
|
|
38
29
|
def add_route(request_method, path, application)
|
@@ -44,7 +35,7 @@ module Galago
|
|
44
35
|
find_route(request_method, path) ? true : false
|
45
36
|
end
|
46
37
|
|
47
|
-
def
|
38
|
+
def call(env)
|
48
39
|
if route = find_route(env['REQUEST_METHOD'], env['PATH_INFO'])
|
49
40
|
route.call(env)
|
50
41
|
else
|
@@ -68,22 +68,5 @@ module Galago
|
|
68
68
|
end
|
69
69
|
end
|
70
70
|
|
71
|
-
describe "#identify_params_in_path" do
|
72
|
-
it "identifies each param and its value" do
|
73
|
-
path = Router::Path.new('/users/:user_id/posts/:id')
|
74
|
-
path_params = path.identify_params_in_path('/users/1/posts/2')
|
75
|
-
|
76
|
-
expect(path_params).to eql({
|
77
|
-
'user_id' => '1',
|
78
|
-
'id' => '2'
|
79
|
-
})
|
80
|
-
end
|
81
|
-
|
82
|
-
it "returns empty when no path params are present" do
|
83
|
-
path = Router::Path.new('/users')
|
84
|
-
expect(path.identify_params_in_path('/users')).to be_empty
|
85
|
-
end
|
86
|
-
end
|
87
|
-
|
88
71
|
end
|
89
72
|
end
|
@@ -2,6 +2,30 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
module Galago
|
4
4
|
describe Router::Route do
|
5
|
+
context "#call" do
|
6
|
+
let(:env) do
|
7
|
+
{ 'PATH_INFO' => '/foo' }
|
8
|
+
end
|
9
|
+
|
10
|
+
let(:action) do
|
11
|
+
lambda { |env| "foo" }
|
12
|
+
end
|
13
|
+
|
14
|
+
it "adds the path's params to the env" do
|
15
|
+
route = Router::Route.new('GET', '/foo', action)
|
16
|
+
expect(route.path).to receive(:add_path_params_to_env).with(env)
|
17
|
+
|
18
|
+
route.call(env)
|
19
|
+
end
|
20
|
+
|
21
|
+
it "calls the route's action" do
|
22
|
+
route = Router::Route.new('GET', '/bar', action)
|
23
|
+
expect(action).to receive(:call).with(env)
|
24
|
+
|
25
|
+
route.call(env)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
5
29
|
context "#initialize" do
|
6
30
|
context "request method" do
|
7
31
|
it "errors when an invalid request method is provided" do
|
@@ -41,25 +65,6 @@ module Galago
|
|
41
65
|
end
|
42
66
|
end
|
43
67
|
|
44
|
-
context "#identify_params_in_path" do
|
45
|
-
it "does not find named parameters when the path has none" do
|
46
|
-
path = Router::Path.new('/accounts')
|
47
|
-
identified_parameters = path.identify_params_in_path('/accounts')
|
48
|
-
|
49
|
-
expect(identified_parameters).to be_empty
|
50
|
-
end
|
51
|
-
|
52
|
-
it "maps the names for params in the path" do
|
53
|
-
path = Router::Path.new('/accounts/:account_id/users/:id')
|
54
|
-
identified_parameters = path.identify_params_in_path('/accounts/42/users/11')
|
55
|
-
|
56
|
-
expect(identified_parameters).to eql({
|
57
|
-
'account_id' => '42',
|
58
|
-
'id' => '11'
|
59
|
-
})
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
68
|
context "path" do
|
64
69
|
it "remembers the path" do
|
65
70
|
route = Router::Route.new('GET', '/foo', lambda {})
|
data/spec/galago/router_spec.rb
CHANGED
@@ -2,18 +2,9 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
module Galago
|
4
4
|
describe Router do
|
5
|
-
let(:router) { Class.new(Router) }
|
6
|
-
|
7
|
-
describe '.call' do
|
8
|
-
it 'tells the router to process the request' do
|
9
|
-
expect(router.router).to receive(:process_request).with('env')
|
10
|
-
router.call('env')
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
5
|
describe '.routes' do
|
15
6
|
it 'adds the specified routes' do
|
16
|
-
router.
|
7
|
+
router = Router.new do
|
17
8
|
get '/foo' , to: lambda { |env| 'bar' }
|
18
9
|
post '/foo' , to: lambda { |env| 'bar' }
|
19
10
|
patch '/foo' , to: lambda { |env| 'bar' }
|
@@ -21,22 +12,11 @@ module Galago
|
|
21
12
|
delete '/foo' , to: lambda { |env| 'bar' }
|
22
13
|
end
|
23
14
|
|
24
|
-
expect(router
|
25
|
-
expect(router
|
26
|
-
expect(router
|
27
|
-
expect(router
|
28
|
-
expect(router
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
describe '.router' do
|
33
|
-
it 'builds an instance if the router' do
|
34
|
-
expect(router.router).to be_a Router
|
35
|
-
end
|
36
|
-
|
37
|
-
it 'remembers the router that was built' do
|
38
|
-
router_id = router.router.object_id
|
39
|
-
expect(router.router.object_id).to eql router_id
|
15
|
+
expect(router).to have_route(:get, '/foo')
|
16
|
+
expect(router).to have_route(:post, '/foo')
|
17
|
+
expect(router).to have_route(:patch, '/foo')
|
18
|
+
expect(router).to have_route(:put, '/foo')
|
19
|
+
expect(router).to have_route(:delete, '/foo')
|
40
20
|
end
|
41
21
|
end
|
42
22
|
|
@@ -83,12 +63,12 @@ module Galago
|
|
83
63
|
end
|
84
64
|
end
|
85
65
|
|
86
|
-
describe "
|
66
|
+
describe "#call" do
|
87
67
|
it "calls the rack app when the route is found" do
|
88
68
|
router = Router.new
|
89
69
|
router.add_route(:get, '/foo', lambda { |env| [200, {}, 'bar'] })
|
90
70
|
|
91
|
-
response = router.
|
71
|
+
response = router.call({
|
92
72
|
'REQUEST_METHOD' => 'GET',
|
93
73
|
'PATH_INFO' => '/foo'
|
94
74
|
})
|
@@ -99,7 +79,7 @@ module Galago
|
|
99
79
|
it "returns 404 when no route matchs the path" do
|
100
80
|
router = Router.new
|
101
81
|
|
102
|
-
response = router.
|
82
|
+
response = router.call({
|
103
83
|
'REQUEST_METHOD' => 'GET',
|
104
84
|
'PATH_INFO' => '/bar'
|
105
85
|
})
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: galago-router
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joe Karayusuf
|
@@ -106,8 +106,9 @@ files:
|
|
106
106
|
- LICENSE.txt
|
107
107
|
- README.md
|
108
108
|
- Rakefile
|
109
|
-
-
|
110
|
-
-
|
109
|
+
- examples/echo_param.ru
|
110
|
+
- examples/lobster.ru
|
111
|
+
- galago-router.gemspec
|
111
112
|
- lib/galago/router.rb
|
112
113
|
- lib/galago/router/dsl.rb
|
113
114
|
- lib/galago/router/errors.rb
|
data/lib/galago.rb
DELETED