rack-router 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c67f53c99d9372515a3c78af3d35750437ce2464
4
+ data.tar.gz: 568d4a2e57f583839652a74e7c8978243764b930
5
+ SHA512:
6
+ metadata.gz: a311a6448794f53522e8c0fc4858414277c18a07cb3b27c3e716d9c32c2c91f6852561b4bff724bed6121eae5da2af65d1ec6b450d3f3c8eb138d54a3d8cd655
7
+ data.tar.gz: be6af03eb657e91d2e7b4c2a633eb95eea3463c07c356fd0f6d3c46ca5c626f61eabd9bd41e7d8cc4d1c4386f5a0138e386e235f133dd189e9b8bbc6effb7e48
data/README.md CHANGED
@@ -18,7 +18,38 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
- See [config.ru](http://github.com/pjb3/rack-router/tree/master/config.ru)
21
+ Here's an example showing a simple rack app that prints the value of a route parameter:
22
+
23
+ ``` ruby
24
+ require 'rack/router'
25
+ require 'rack/lobster'
26
+
27
+ hello = ->(env) do
28
+ [
29
+ 200,
30
+ { "Content-Type" => "text/html" },
31
+ ["<h1>Hello, #{env['rack.route_params'][:name]}</h1>"]
32
+ ]
33
+ end
34
+
35
+ router = Rack::Router.new do
36
+ get "/hello/:name" => hello
37
+ get "/lobster" => Rack::Lobster.new, :as => "lobster"
38
+ end
39
+
40
+ run router
41
+ ```
42
+
43
+ In this example, `hello` is just a rack app defined inline, in order to give us something to route to. The route to our hello app includes a parameter `:name`. The hello rack app is able to access that parameter via the rack env.
44
+
45
+ This is a valid Rackup file, so if you put this in a file named `config.ru` and run `rackup`, you will be app to hit the application like this:
46
+
47
+ $ curl http://localhost:9292/hello/paul
48
+ <h1>Hello, paul</h1>
49
+
50
+ Don't forget to try the lobster!
51
+
52
+ $ open http://localhost:9292/lobster
22
53
 
23
54
  ## Contributing
24
55
 
data/config.ru CHANGED
@@ -1,8 +1,16 @@
1
1
  require 'rack/router'
2
2
  require 'rack/lobster'
3
3
 
4
+ hello = ->(env) do
5
+ [
6
+ 200,
7
+ { "Content-Type" => "text/html" },
8
+ ["<h1>Hello, #{env['rack.route_params'][:name]}</h1>"]
9
+ ]
10
+ end
11
+
4
12
  router = Rack::Router.new do
5
- get "/hello/:name" => proc{|env| [200, { "Content-Type" => "text/html" }, ["<h1>Hello, #{env['rack.route_params'][:name]}</h1>"] ] }
13
+ get "/hello/:name" => hello
6
14
  get "/lobster" => Rack::Lobster.new, :as => "lobster"
7
15
  end
8
16
 
@@ -1,7 +1,6 @@
1
1
  module Rack
2
2
  class Route
3
-
4
- attr_accessor :pattern, :app, :constraints, :name
3
+ attr_accessor :request_method, :pattern, :app, :constraints, :name
5
4
 
6
5
  PATH_INFO = 'PATH_INFO'.freeze
7
6
  DEFAULT_WILDCARD_NAME = :paths
@@ -10,7 +9,7 @@ module Rack
10
9
  NAMED_SEGMENTS_REPLACEMENT_PATTERN = /\/:([^$\/]+)/.freeze
11
10
  DOT = '.'.freeze
12
11
 
13
- def initialize(pattern, app, options={})
12
+ def initialize(request_method, pattern, app, options={})
14
13
  if pattern.to_s.strip.empty?
15
14
  raise ArgumentError.new("pattern cannot be blank")
16
15
  end
@@ -19,6 +18,7 @@ module Rack
19
18
  raise ArgumentError.new("app must be callable")
20
19
  end
21
20
 
21
+ @request_method = request_method
22
22
  @pattern = pattern
23
23
  @app = app
24
24
  @constraints = options && options[:constraints]
@@ -48,7 +48,11 @@ module Rack
48
48
  Regexp.new("\\A#{src}\\Z")
49
49
  end
50
50
 
51
- def match(path)
51
+ def match(request_method, path)
52
+ unless request_method == self.request_method
53
+ return nil
54
+ end
55
+
52
56
  if path.to_s.strip.empty?
53
57
  raise ArgumentError.new("path is required")
54
58
  end
@@ -81,6 +85,7 @@ module Rack
81
85
 
82
86
  def eql?(o)
83
87
  o.is_a?(self.class) &&
88
+ o.request_method == request_method &&
84
89
  o.pattern == pattern &&
85
90
  o.app == app &&
86
91
  o.constraints == constraints
@@ -88,7 +93,7 @@ module Rack
88
93
  alias == eql?
89
94
 
90
95
  def hash
91
- pattern.hash ^ app.hash ^ constraints.hash
96
+ request_method.hash ^ pattern.hash ^ app.hash ^ constraints.hash
92
97
  end
93
98
  end
94
99
  end
@@ -1,9 +1,9 @@
1
1
  require 'rack/route'
2
- require 'rack/version'
3
2
 
4
3
  module Rack
5
4
 
6
5
  class Router
6
+ VERSION = "0.4.0"
7
7
 
8
8
  HEAD = 'HEAD'.freeze
9
9
  GET = 'GET'.freeze
@@ -15,7 +15,6 @@ module Rack
15
15
  ROUTE_PARAMS = 'rack.route_params'.freeze
16
16
 
17
17
  def initialize(&block)
18
- @routes = {}
19
18
  @named_routes = {}
20
19
  routes(&block)
21
20
  end
@@ -46,9 +45,9 @@ module Rack
46
45
  end
47
46
 
48
47
  def route(method, route_spec)
49
- route = Route.new(route_spec.first.first, route_spec.first.last, route_spec.reject{|k,_| k == route_spec.first.first })
50
- @routes[method] ||= []
51
- @routes[method] << route
48
+ route = Route.new(method, route_spec.first.first, route_spec.first.last, route_spec.reject{|k,_| k == route_spec.first.first })
49
+ @routes ||= []
50
+ @routes << route
52
51
  if route_spec && route_spec[:as]
53
52
  # Using ||= so the first route with that name will be returned
54
53
  @named_routes[route_spec[:as].to_sym] ||= route_spec.first.first
@@ -67,15 +66,13 @@ module Rack
67
66
  def match(env)
68
67
  request_method = env[REQUEST_METHOD]
69
68
  request_method = GET if request_method == HEAD
70
- if method_routes = @routes[request_method]
71
- method_routes.each do |route|
72
- if params = route.match(env[PATH_INFO])
73
- env[ROUTE_PARAMS] = params
74
- return route.app
75
- end
69
+ routes.each do |route|
70
+ if params = route.match(request_method, env[PATH_INFO])
71
+ env[ROUTE_PARAMS] = params
72
+ return route.app
76
73
  end
77
- nil
78
74
  end
75
+ nil
79
76
  end
80
77
 
81
78
  def not_found(env)
@@ -11,5 +11,5 @@ Gem::Specification.new do |gem|
11
11
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
12
12
  gem.name = "rack-router"
13
13
  gem.require_paths = ["lib"]
14
- gem.version = "0.3.0"
14
+ gem.version = "0.4.0"
15
15
  end
@@ -30,7 +30,7 @@ class RouteTest < Test::Unit::TestCase
30
30
  end
31
31
 
32
32
  def test_match_with_constraints
33
- r = route("/posts/:year/:month/:day/:slug",
33
+ r = route('GET', "/posts/:year/:month/:day/:slug",
34
34
  :constraints => {
35
35
  :year => /\A\d{4}\Z/,
36
36
  :month => /\A\d{1,2}\Z/,
@@ -41,22 +41,23 @@ class RouteTest < Test::Unit::TestCase
41
41
  :month => "9",
42
42
  :day => "20",
43
43
  :slug => "test"
44
- }, r.match("/posts/2012/9/20/test"))
45
- assert_equal(nil, r.match("/posts/2012/9/20"))
46
- assert_equal(nil, r.match("/posts/2012/x/20/test"))
44
+ }, r.match('GET', "/posts/2012/9/20/test"))
45
+ assert_equal(nil, r.match('POST', "/posts/2012/9/20/test"))
46
+ assert_equal(nil, r.match('GET', "/posts/2012/9/20"))
47
+ assert_equal(nil, r.match('GET', "/posts/2012/x/20/test"))
47
48
  assert_equal "article", r.name
48
49
  end
49
50
 
50
51
  def test_eql
51
52
  app = lambda{|env| [200, {}, [""]] }
52
53
  assert_equal(
53
- Rack::Route.new("/", app),
54
- Rack::Route.new("/", app))
54
+ Rack::Route.new('GET', "/", app),
55
+ Rack::Route.new('GET', "/", app))
55
56
  end
56
57
 
57
58
  private
58
- def route(path, options={})
59
- Rack::Route.new(path, lambda{|env| [200, {}, [""]] }, options)
59
+ def route(request_method, path, options={})
60
+ Rack::Route.new(request_method, path, lambda{|env| [200, {}, [""]] }, options)
60
61
  end
61
62
 
62
63
  def match(pattern, path, params)
@@ -66,7 +67,7 @@ class RouteTest < Test::Unit::TestCase
66
67
  else
67
68
  msg << "no match #{path}"
68
69
  end
69
- assert_equal(params, route(pattern).match(path), msg)
70
+ assert_equal(params, route('GET', pattern).match('GET', path), msg)
70
71
  end
71
72
  end
72
73
 
@@ -13,12 +13,12 @@ class RouterTest < Test::Unit::TestCase
13
13
  get "/:id" => app1
14
14
  end
15
15
 
16
- assert_equal({
17
- "POST" => [Rack::Route.new("/stuff", app2)],
18
- "PUT" => [Rack::Route.new("/it", app2)],
19
- "DELETE" => [Rack::Route.new("/remove", app2)],
20
- "GET" => [Rack::Route.new("/:id", app1)]
21
- }, router.routes)
16
+ assert_equal([
17
+ Rack::Route.new('POST', '/stuff', app2),
18
+ Rack::Route.new('PUT', '/it', app2),
19
+ Rack::Route.new('DELETE', '/remove', app2),
20
+ Rack::Route.new('GET', '/:id', app1)
21
+ ], router.routes)
22
22
 
23
23
  assert_equal ["42"], router.call("REQUEST_METHOD" => "GET", "PATH_INFO" => "/42").last
24
24
  assert_equal ["<h1>Not Found</h1><p>No route matches GET /not/found</p>"], router.call("REQUEST_METHOD" => "GET", "PATH_INFO" => "/not/found").last
metadata CHANGED
@@ -1,15 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-router
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
5
- prerelease:
4
+ version: 0.4.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Paul Barry
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-02-27 00:00:00.000000000 Z
11
+ date: 2013-05-16 00:00:00.000000000 Z
13
12
  dependencies: []
14
13
  description: A simple router for Rack apps
15
14
  email:
@@ -28,35 +27,34 @@ files:
28
27
  - lib/rack-router.rb
29
28
  - lib/rack/route.rb
30
29
  - lib/rack/router.rb
31
- - lib/rack/version.rb
32
30
  - lib/rack_router.rb
33
31
  - rack-router.gemspec
34
32
  - test/route_test.rb
35
33
  - test/router_test.rb
36
34
  homepage: https://github.com/pjb3/rack-router
37
35
  licenses: []
36
+ metadata: {}
38
37
  post_install_message:
39
38
  rdoc_options: []
40
39
  require_paths:
41
40
  - lib
42
41
  required_ruby_version: !ruby/object:Gem::Requirement
43
- none: false
44
42
  requirements:
45
- - - ! '>='
43
+ - - '>='
46
44
  - !ruby/object:Gem::Version
47
45
  version: '0'
48
46
  required_rubygems_version: !ruby/object:Gem::Requirement
49
- none: false
50
47
  requirements:
51
- - - ! '>='
48
+ - - '>='
52
49
  - !ruby/object:Gem::Version
53
50
  version: '0'
54
51
  requirements: []
55
52
  rubyforge_project:
56
- rubygems_version: 1.8.25
53
+ rubygems_version: 2.0.0
57
54
  signing_key:
58
- specification_version: 3
55
+ specification_version: 4
59
56
  summary: A simple router for Rack apps
60
57
  test_files:
61
58
  - test/route_test.rb
62
59
  - test/router_test.rb
60
+ has_rdoc:
@@ -1,5 +0,0 @@
1
- module Rack
2
- class Router
3
- VERSION = "0.3.0"
4
- end
5
- end