rack-router 0.2.1 → 0.3.0
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.
- data/.gitignore +1 -0
- data/lib/rack/route.rb +9 -4
- data/lib/rack/router.rb +9 -7
- data/lib/rack/version.rb +1 -1
- data/rack-router.gemspec +2 -2
- data/test/route_test.rb +2 -1
- data/test/router_test.rb +1 -0
- metadata +4 -5
data/.gitignore
CHANGED
data/lib/rack/route.rb
CHANGED
@@ -37,10 +37,13 @@ module Rack
|
|
37
37
|
pattern_match[1].to_sym
|
38
38
|
end
|
39
39
|
pattern.gsub(WILDCARD_PATTERN,'(?:/(.*)|)')
|
40
|
-
elsif pattern_match = pattern.match(NAMED_SEGMENTS_PATTERN)
|
41
|
-
pattern.gsub(NAMED_SEGMENTS_REPLACEMENT_PATTERN, '/(?<\1>[^$/]+)')
|
42
40
|
else
|
43
|
-
pattern
|
41
|
+
p = if pattern_match = pattern.match(NAMED_SEGMENTS_PATTERN)
|
42
|
+
pattern.gsub(NAMED_SEGMENTS_REPLACEMENT_PATTERN, '/(?<\1>[^.$/]+)')
|
43
|
+
else
|
44
|
+
pattern
|
45
|
+
end
|
46
|
+
p + '(?:\.(?<format>.*))?'
|
44
47
|
end
|
45
48
|
Regexp.new("\\A#{src}\\Z")
|
46
49
|
end
|
@@ -50,13 +53,15 @@ module Rack
|
|
50
53
|
raise ArgumentError.new("path is required")
|
51
54
|
end
|
52
55
|
|
53
|
-
if path_match = path.
|
56
|
+
if path_match = path.match(regexp)
|
54
57
|
params = if @wildcard_name
|
55
58
|
{ @wildcard_name => path_match[1].to_s.split('/') }
|
56
59
|
else
|
57
60
|
Hash[path_match.names.map(&:to_sym).zip(path_match.captures)]
|
58
61
|
end
|
59
62
|
|
63
|
+
params.delete(:format) if params.has_key?(:format) && params[:format].nil?
|
64
|
+
|
60
65
|
if meets_constraints(params)
|
61
66
|
params
|
62
67
|
end
|
data/lib/rack/router.rb
CHANGED
@@ -13,12 +13,6 @@ module Rack
|
|
13
13
|
REQUEST_METHOD = 'REQUEST_METHOD'.freeze
|
14
14
|
PATH_INFO = 'PATH_INFO'.freeze
|
15
15
|
ROUTE_PARAMS = 'rack.route_params'.freeze
|
16
|
-
DEFAULT_NOT_FOUND_BODY = '<h1>Not Found</h1>'.freeze
|
17
|
-
DEFAULT_NOT_FOUND_RESPONSE = [404,
|
18
|
-
{
|
19
|
-
"Content-Type" => "text/html",
|
20
|
-
"Content-Length" => DEFAULT_NOT_FOUND_BODY.length.to_s
|
21
|
-
}, [DEFAULT_NOT_FOUND_BODY]]
|
22
16
|
|
23
17
|
def initialize(&block)
|
24
18
|
@routes = {}
|
@@ -85,7 +79,15 @@ module Rack
|
|
85
79
|
end
|
86
80
|
|
87
81
|
def not_found(env)
|
88
|
-
|
82
|
+
body = "<h1>Not Found</h1><p>No route matches #{env[REQUEST_METHOD]} #{env[PATH_INFO]}</p>"
|
83
|
+
[
|
84
|
+
404,
|
85
|
+
{
|
86
|
+
"Content-Type" => "text/html",
|
87
|
+
"Content-Length" => body.length.to_s
|
88
|
+
},
|
89
|
+
[body]
|
90
|
+
]
|
89
91
|
end
|
90
92
|
end
|
91
93
|
end
|
data/lib/rack/version.rb
CHANGED
data/rack-router.gemspec
CHANGED
@@ -3,7 +3,7 @@ Gem::Specification.new do |gem|
|
|
3
3
|
gem.authors = ["Paul Barry"]
|
4
4
|
gem.email = ["mail@paulbarry.com"]
|
5
5
|
gem.description = %q{A simple router for Rack apps}
|
6
|
-
gem.summary = %q{A simple router for
|
6
|
+
gem.summary = %q{A simple router for Rack apps}
|
7
7
|
gem.homepage = "https://github.com/pjb3/rack-router"
|
8
8
|
|
9
9
|
gem.files = `git ls-files`.split($\)
|
@@ -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.
|
14
|
+
gem.version = "0.3.0"
|
15
15
|
end
|
data/test/route_test.rb
CHANGED
@@ -25,7 +25,8 @@ class RouteTest < Test::Unit::TestCase
|
|
25
25
|
match "/posts/:id" , "/posts/42" , { :id => "42" }
|
26
26
|
match "/posts/:id" , "/posts" , nil
|
27
27
|
match "/:x/:y" , "/a/b" , { :x => "a" , :y => "b" }
|
28
|
-
match "/posts
|
28
|
+
match "/posts" , "/posts.json" , { :format => "json" }
|
29
|
+
match "/posts/:id" , "/posts/42.json", { :id => "42", :format => "json" }
|
29
30
|
end
|
30
31
|
|
31
32
|
def test_match_with_constraints
|
data/test/router_test.rb
CHANGED
@@ -21,6 +21,7 @@ class RouterTest < Test::Unit::TestCase
|
|
21
21
|
}, router.routes)
|
22
22
|
|
23
23
|
assert_equal ["42"], router.call("REQUEST_METHOD" => "GET", "PATH_INFO" => "/42").last
|
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
|
24
25
|
assert_equal "/stuff", router[:stuff]
|
25
26
|
assert_equal "/it", router[:it]
|
26
27
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-router
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-02-
|
12
|
+
date: 2013-02-27 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: A simple router for Rack apps
|
15
15
|
email:
|
@@ -53,11 +53,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
53
53
|
version: '0'
|
54
54
|
requirements: []
|
55
55
|
rubyforge_project:
|
56
|
-
rubygems_version: 1.8.
|
56
|
+
rubygems_version: 1.8.25
|
57
57
|
signing_key:
|
58
58
|
specification_version: 3
|
59
|
-
summary: A simple router for
|
59
|
+
summary: A simple router for Rack apps
|
60
60
|
test_files:
|
61
61
|
- test/route_test.rb
|
62
62
|
- test/router_test.rb
|
63
|
-
has_rdoc:
|