galago-router 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 +60 -0
- data/Rakefile +1 -0
- data/galago.gemspec +28 -0
- data/lib/galago.rb +2 -0
- data/lib/galago/router.rb +69 -0
- data/lib/galago/router/dsl.rb +28 -0
- data/lib/galago/router/errors.rb +22 -0
- data/lib/galago/router/path.rb +44 -0
- data/lib/galago/router/route.rb +36 -0
- data/lib/galago/router/version.rb +5 -0
- data/spec/galago/router/path_spec.rb +89 -0
- data/spec/galago/router/route_spec.rb +85 -0
- data/spec/galago/router/version_spec.rb +9 -0
- data/spec/galago/router_spec.rb +114 -0
- data/spec/spec_helper.rb +18 -0
- metadata +152 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 12e8872da7d883013a2edaa73193d11f9ebe00a6
|
4
|
+
data.tar.gz: 27e804be87b65711ab84cd2fe7c674f6cda45efb
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 05eadb6f0ad527e2a73d8087bc1a6c3724d894d5a75340b20118c4ac80e2b7e58c8536f1e41bf864b493e50967953284a618ce558acea7a27f3cd12ee3e9829c
|
7
|
+
data.tar.gz: 920319b3d504dd72f4f5dea98ec94cca38a661798cc7219e24f113773d0a5e77e1a461329f0e842999930166508f784fcdfb250d580acd75729310cfa0b301f7
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Joe Karayusuf
|
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,60 @@
|
|
1
|
+
# Galago
|
2
|
+
|
3
|
+
Galago is an API micro framework for Ruby.
|
4
|
+
I am building it to gain more appreciation for existing tools.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
gem 'galago'
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install galago
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
module GitHub::Application < Galago::Application
|
24
|
+
routes do
|
25
|
+
get '/users' do
|
26
|
+
users = User.all
|
27
|
+
end
|
28
|
+
|
29
|
+
post '/users' do
|
30
|
+
user = User.create! params['user']
|
31
|
+
end
|
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
|
48
|
+
end
|
49
|
+
|
50
|
+
run GitHub::Application
|
51
|
+
```
|
52
|
+
|
53
|
+
## Contributing
|
54
|
+
|
55
|
+
1. Fork it
|
56
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
57
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
58
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
59
|
+
5. Create new Pull Request
|
60
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/galago.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'galago/router/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "galago-router"
|
8
|
+
spec.version = Galago::Router::VERSION
|
9
|
+
spec.authors = ["Joe Karayusuf"]
|
10
|
+
spec.email = ["jkarayusuf@gmail.com"]
|
11
|
+
spec.description = %q{Rack router}
|
12
|
+
spec.summary = %q{}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency 'rack'
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
24
|
+
spec.add_development_dependency "pry"
|
25
|
+
spec.add_development_dependency "rack-test"
|
26
|
+
spec.add_development_dependency "rake"
|
27
|
+
spec.add_development_dependency "rspec"
|
28
|
+
end
|
data/lib/galago.rb
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
module Galago
|
2
|
+
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
|
+
|
21
|
+
REQUEST_METHODS = [
|
22
|
+
"GET",
|
23
|
+
"PATCH",
|
24
|
+
"POST",
|
25
|
+
"PUT",
|
26
|
+
"DELETE"
|
27
|
+
]
|
28
|
+
|
29
|
+
attr_reader :routes
|
30
|
+
|
31
|
+
def initialize
|
32
|
+
@routes = REQUEST_METHODS.each_with_object({}) do |request_method, routes|
|
33
|
+
routes[request_method] = []
|
34
|
+
routes
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def add_route(request_method, path, application)
|
39
|
+
route = Route.new(request_method, path, application)
|
40
|
+
routes[route.request_method] << route
|
41
|
+
end
|
42
|
+
|
43
|
+
def has_route?(request_method, path)
|
44
|
+
find_route(request_method, path) ? true : false
|
45
|
+
end
|
46
|
+
|
47
|
+
def process_request(env)
|
48
|
+
if route = find_route(env['REQUEST_METHOD'], env['PATH_INFO'])
|
49
|
+
route.call(env)
|
50
|
+
else
|
51
|
+
Rack::Response.new("Not Found", 404).finish
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
private
|
56
|
+
|
57
|
+
def find_route(request_method, path)
|
58
|
+
routes = routes_for_request_method(request_method)
|
59
|
+
route = routes.detect { |route| route.recognizes_path?(path) }
|
60
|
+
end
|
61
|
+
|
62
|
+
def routes_for_request_method(request_method)
|
63
|
+
routes.fetch(request_method.to_s.upcase) do
|
64
|
+
raise RequestMethodInvalid.new(request_method)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Galago
|
2
|
+
class Router::DSL
|
3
|
+
def initialize(router, block)
|
4
|
+
@router = router
|
5
|
+
instance_eval(&block)
|
6
|
+
end
|
7
|
+
|
8
|
+
def get(path, options)
|
9
|
+
@router.add_route("GET", path, options[:to])
|
10
|
+
end
|
11
|
+
|
12
|
+
def patch(path, options)
|
13
|
+
@router.add_route("PATCH", path, options[:to])
|
14
|
+
end
|
15
|
+
|
16
|
+
def post(path, options)
|
17
|
+
@router.add_route("POST", path, options[:to])
|
18
|
+
end
|
19
|
+
|
20
|
+
def put(path, options)
|
21
|
+
@router.add_route("PUT", path, options[:to])
|
22
|
+
end
|
23
|
+
|
24
|
+
def delete(path, options)
|
25
|
+
@router.add_route("DELETE", path, options[:to])
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Galago
|
2
|
+
class Router::ActionInvalid < ArgumentError
|
3
|
+
def initialize(action)
|
4
|
+
@action = action
|
5
|
+
end
|
6
|
+
|
7
|
+
def message
|
8
|
+
"Action does not have a call method: #{@action.inspect}"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class Router::RequestMethodInvalid < ArgumentError
|
13
|
+
def initialize(request_method)
|
14
|
+
@request_method = request_method
|
15
|
+
end
|
16
|
+
|
17
|
+
def message
|
18
|
+
"Got: #{@request_method}\nExpected one of: #{REQUEST_METHODS}"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module Galago
|
2
|
+
class Router::Path
|
3
|
+
|
4
|
+
def initialize(path)
|
5
|
+
@path = path.to_s
|
6
|
+
end
|
7
|
+
|
8
|
+
def recognizes?(request_path)
|
9
|
+
request_path =~ regex
|
10
|
+
end
|
11
|
+
|
12
|
+
def named_parameters
|
13
|
+
@path_parameters ||= @path.scan(/\:(\w+)/).flatten
|
14
|
+
end
|
15
|
+
|
16
|
+
def add_path_params_to_env(env)
|
17
|
+
request = Rack::Request.new(env)
|
18
|
+
identify_params_in_path(request.path).each do |key, value|
|
19
|
+
request.update_param(key, value)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def identify_params_in_path(request_path)
|
24
|
+
values = regex.match(request_path).captures
|
25
|
+
Hash[named_parameters.zip(values)]
|
26
|
+
end
|
27
|
+
|
28
|
+
def to_s
|
29
|
+
@path
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def regex
|
35
|
+
@regex_path ||= convert_path_to_regex(@path)
|
36
|
+
end
|
37
|
+
|
38
|
+
def convert_path_to_regex(path)
|
39
|
+
regexp = path.to_s.gsub(/\:\w+/, '([\w-]+)')
|
40
|
+
Regexp.new("^#{regexp}$")
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Galago
|
2
|
+
class Router::Route
|
3
|
+
|
4
|
+
attr_reader :request_method, :path, :action
|
5
|
+
|
6
|
+
def initialize(request_method, path, action)
|
7
|
+
@request_method = request_method.to_s.upcase
|
8
|
+
@path = Router::Path.new(path)
|
9
|
+
@action = action
|
10
|
+
|
11
|
+
validate_action!
|
12
|
+
validate_request_method!
|
13
|
+
end
|
14
|
+
|
15
|
+
def recognizes_path?(request_path)
|
16
|
+
@path.recognizes?(request_path)
|
17
|
+
end
|
18
|
+
|
19
|
+
def call(env)
|
20
|
+
@path.add_path_params_to_env(env)
|
21
|
+
action.call(env)
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def validate_action!
|
27
|
+
action.respond_to?(:call) or raise Router::ActionInvalid.new(action)
|
28
|
+
end
|
29
|
+
|
30
|
+
def validate_request_method!
|
31
|
+
unless Router::REQUEST_METHODS.include?(request_method)
|
32
|
+
raise Router::RequestMethodInvalid.new(request_method)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Galago
|
4
|
+
describe Router::Path do
|
5
|
+
|
6
|
+
describe "#recognizes?" do
|
7
|
+
it "recognizes exact matches" do
|
8
|
+
path = Router::Path.new('/users')
|
9
|
+
expect(path).to be_recognizes '/users'
|
10
|
+
end
|
11
|
+
|
12
|
+
it "recognizes parameters matches" do
|
13
|
+
path = Router::Path.new('/users/:id')
|
14
|
+
expect(path).to be_recognizes '/users/1'
|
15
|
+
end
|
16
|
+
|
17
|
+
it "does not recognize exact param matches" do
|
18
|
+
path = Router::Path.new('/users/:id')
|
19
|
+
expect(path).not_to be_recognizes '/users/:id'
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe "#to_s" do
|
24
|
+
it "returns the path as a string" do
|
25
|
+
path = Router::Path.new(:foo)
|
26
|
+
expect(path.to_s).to eql 'foo'
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe "#named_parameters" do
|
31
|
+
it "returns path segments starting with a ':'" do
|
32
|
+
path = Router::Path.new('/users/:user_id/posts/:id')
|
33
|
+
expect(path.named_parameters).to eql ['user_id', 'id']
|
34
|
+
end
|
35
|
+
|
36
|
+
it "returns empty when no segments begin with a ':'" do
|
37
|
+
path = Router::Path.new('/users')
|
38
|
+
expect(path.named_parameters).to be_empty
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe "#add_path_params_to_env" do
|
43
|
+
let(:env) do
|
44
|
+
{ 'rack.input' => anything }
|
45
|
+
end
|
46
|
+
|
47
|
+
it "adds the params" do
|
48
|
+
env['PATH_INFO'] = '/users/21/posts/42'
|
49
|
+
|
50
|
+
path = Router::Path.new('/users/:user_id/posts/:id')
|
51
|
+
path.add_path_params_to_env(env)
|
52
|
+
|
53
|
+
request = Rack::Request.new(env)
|
54
|
+
expect(request.params).to eql({
|
55
|
+
'user_id' => '21',
|
56
|
+
'id' => '42'
|
57
|
+
})
|
58
|
+
end
|
59
|
+
|
60
|
+
it "does not add params when no path params exist" do
|
61
|
+
env['PATH_INFO'] = '/users'
|
62
|
+
|
63
|
+
path = Router::Path.new('/users')
|
64
|
+
path.add_path_params_to_env(env)
|
65
|
+
|
66
|
+
request = Rack::Request.new(env)
|
67
|
+
expect(request.params).to be_empty
|
68
|
+
end
|
69
|
+
end
|
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
|
+
end
|
89
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Galago
|
4
|
+
describe Router::Route do
|
5
|
+
context "#initialize" do
|
6
|
+
context "request method" do
|
7
|
+
it "errors when an invalid request method is provided" do
|
8
|
+
expect { Router::Route.new('foo', anything, lambda {})
|
9
|
+
}.to raise_error Router::RequestMethodInvalid
|
10
|
+
end
|
11
|
+
|
12
|
+
it "remembers the request method" do
|
13
|
+
Router::REQUEST_METHODS.each do |request_method|
|
14
|
+
route = Router::Route.new(request_method, anything, lambda {})
|
15
|
+
expect(route.request_method).to eql request_method
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context "#recognizes?" do
|
21
|
+
it "recognizes a paths with no named parameters" do
|
22
|
+
path = Router::Path.new('/users')
|
23
|
+
expect(path).to be_recognizes('/users')
|
24
|
+
end
|
25
|
+
|
26
|
+
it "recognizes a path with named parameters" do
|
27
|
+
path = Router::Path.new('/users/:id')
|
28
|
+
expect(path).to be_recognizes('/users/32')
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
context "#named_parameters" do
|
33
|
+
it "has none when no segments start with a ':'" do
|
34
|
+
path = Router::Path.new('/users')
|
35
|
+
expect(path).to have(0).named_parameters
|
36
|
+
end
|
37
|
+
|
38
|
+
it "returns segments starting with a ':'" do
|
39
|
+
path = Router::Path.new('/accounts/:account_id/users/:id')
|
40
|
+
expect(path.named_parameters).to eql ['account_id', 'id']
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
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
|
+
context "path" do
|
64
|
+
it "remembers the path" do
|
65
|
+
route = Router::Route.new('GET', '/foo', lambda {})
|
66
|
+
expect(route.path.to_s).to eql '/foo'
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
context "action" do
|
71
|
+
it "raises an error when the action does not respond to call" do
|
72
|
+
expect { Router::Route.new('PATCH', '/foo', :action)
|
73
|
+
}.to raise_error Router::ActionInvalid
|
74
|
+
end
|
75
|
+
|
76
|
+
it "remembers the action" do
|
77
|
+
action = Proc.new { 'action' }
|
78
|
+
route = Router::Route.new('GET', '/foo', action)
|
79
|
+
expect(route.action).to eql action
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
@@ -0,0 +1,114 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Galago
|
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
|
+
describe '.routes' do
|
15
|
+
it 'adds the specified routes' do
|
16
|
+
router.routes do
|
17
|
+
get '/foo' , to: lambda { |env| 'bar' }
|
18
|
+
post '/foo' , to: lambda { |env| 'bar' }
|
19
|
+
patch '/foo' , to: lambda { |env| 'bar' }
|
20
|
+
put '/foo' , to: lambda { |env| 'bar' }
|
21
|
+
delete '/foo' , to: lambda { |env| 'bar' }
|
22
|
+
end
|
23
|
+
|
24
|
+
expect(router.router).to have_route(:get, '/foo')
|
25
|
+
expect(router.router).to have_route(:post, '/foo')
|
26
|
+
expect(router.router).to have_route(:patch, '/foo')
|
27
|
+
expect(router.router).to have_route(:put, '/foo')
|
28
|
+
expect(router.router).to have_route(:delete, '/foo')
|
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
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe "#add_route" do
|
44
|
+
let(:rack_app) do
|
45
|
+
lambda { |env| "RackApp" }
|
46
|
+
end
|
47
|
+
|
48
|
+
it "stores the route" do
|
49
|
+
router = Router.new
|
50
|
+
router.add_route(:get, '/users', rack_app)
|
51
|
+
|
52
|
+
expect(router).to have_route(:get, '/users')
|
53
|
+
end
|
54
|
+
|
55
|
+
it "raises an error when an invalid http verb is provided" do
|
56
|
+
router = Router.new
|
57
|
+
|
58
|
+
expect { router.add_route(:foo, '/foo', rack_app)
|
59
|
+
}.to raise_error Router::RequestMethodInvalid
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
describe "has_route?" do
|
64
|
+
it "returns true when the route has been added" do
|
65
|
+
router = Router.new
|
66
|
+
router.add_route(:post, '/users', lambda {})
|
67
|
+
|
68
|
+
expect(router).to have_route(:post, '/users')
|
69
|
+
end
|
70
|
+
|
71
|
+
it "returns true when the route has a path param" do
|
72
|
+
router = Router.new
|
73
|
+
router.add_route(:get, '/users/:id', lambda {})
|
74
|
+
|
75
|
+
expect(router).to have_route(:get, '/users/42')
|
76
|
+
end
|
77
|
+
|
78
|
+
it "returns false when the route has not been added" do
|
79
|
+
router = Router.new
|
80
|
+
router.add_route(:post, '/users', lambda {})
|
81
|
+
|
82
|
+
expect(router).not_to have_route(:get, '/users')
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
describe "process_request" do
|
87
|
+
it "calls the rack app when the route is found" do
|
88
|
+
router = Router.new
|
89
|
+
router.add_route(:get, '/foo', lambda { |env| [200, {}, 'bar'] })
|
90
|
+
|
91
|
+
response = router.process_request({
|
92
|
+
'REQUEST_METHOD' => 'GET',
|
93
|
+
'PATH_INFO' => '/foo'
|
94
|
+
})
|
95
|
+
|
96
|
+
expect(response).to eql [200, {}, 'bar']
|
97
|
+
end
|
98
|
+
|
99
|
+
it "returns 404 when no route matchs the path" do
|
100
|
+
router = Router.new
|
101
|
+
|
102
|
+
response = router.process_request({
|
103
|
+
'REQUEST_METHOD' => 'GET',
|
104
|
+
'PATH_INFO' => '/bar'
|
105
|
+
})
|
106
|
+
|
107
|
+
expect(response[0]).to eql(404)
|
108
|
+
expect(response[1]).to eql({ 'Content-Length' => '9' })
|
109
|
+
expect(response[2].body).to eql(['Not Found'])
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
end
|
114
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'galago/router'
|
2
|
+
require 'rack/test'
|
3
|
+
require 'pry'
|
4
|
+
|
5
|
+
RSpec.configure do |config|
|
6
|
+
config.color_enabled = true
|
7
|
+
config.formatter = :documentation
|
8
|
+
|
9
|
+
config.include Rack::Test::Methods
|
10
|
+
|
11
|
+
def application(&block)
|
12
|
+
let(:app) do
|
13
|
+
klass = Class.new(Galago::Router)
|
14
|
+
klass.class_eval(&block)
|
15
|
+
klass
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
metadata
ADDED
@@ -0,0 +1,152 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: galago-router
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Joe Karayusuf
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-03-03 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.3'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pry
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rack-test
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
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: rspec
|
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: Rack router
|
98
|
+
email:
|
99
|
+
- jkarayusuf@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
|
+
- galago.gemspec
|
110
|
+
- lib/galago.rb
|
111
|
+
- lib/galago/router.rb
|
112
|
+
- lib/galago/router/dsl.rb
|
113
|
+
- lib/galago/router/errors.rb
|
114
|
+
- lib/galago/router/path.rb
|
115
|
+
- lib/galago/router/route.rb
|
116
|
+
- lib/galago/router/version.rb
|
117
|
+
- spec/galago/router/path_spec.rb
|
118
|
+
- spec/galago/router/route_spec.rb
|
119
|
+
- spec/galago/router/version_spec.rb
|
120
|
+
- spec/galago/router_spec.rb
|
121
|
+
- spec/spec_helper.rb
|
122
|
+
homepage: ''
|
123
|
+
licenses:
|
124
|
+
- MIT
|
125
|
+
metadata: {}
|
126
|
+
post_install_message:
|
127
|
+
rdoc_options: []
|
128
|
+
require_paths:
|
129
|
+
- lib
|
130
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
131
|
+
requirements:
|
132
|
+
- - '>='
|
133
|
+
- !ruby/object:Gem::Version
|
134
|
+
version: '0'
|
135
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
136
|
+
requirements:
|
137
|
+
- - '>='
|
138
|
+
- !ruby/object:Gem::Version
|
139
|
+
version: '0'
|
140
|
+
requirements: []
|
141
|
+
rubyforge_project:
|
142
|
+
rubygems_version: 2.0.2
|
143
|
+
signing_key:
|
144
|
+
specification_version: 4
|
145
|
+
summary: ''
|
146
|
+
test_files:
|
147
|
+
- spec/galago/router/path_spec.rb
|
148
|
+
- spec/galago/router/route_spec.rb
|
149
|
+
- spec/galago/router/version_spec.rb
|
150
|
+
- spec/galago/router_spec.rb
|
151
|
+
- spec/spec_helper.rb
|
152
|
+
has_rdoc:
|