http_router 0.3.11 → 0.3.12
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/http_router.rb +10 -9
- data/lib/http_router/node.rb +1 -0
- data/lib/http_router/root.rb +5 -10
- data/lib/http_router/route.rb +4 -4
- data/lib/http_router/version.rb +1 -1
- metadata +3 -3
data/lib/http_router.rb
CHANGED
@@ -30,7 +30,7 @@ class HttpRouter
|
|
30
30
|
# Raised when there is a potential conflict of variable names within your Route.
|
31
31
|
AmbiguousVariableException = Class.new(RuntimeError)
|
32
32
|
|
33
|
-
attr_reader :named_routes, :routes, :root
|
33
|
+
attr_reader :named_routes, :routes, :root, :request_methods_specified
|
34
34
|
attr_accessor :url_mount
|
35
35
|
|
36
36
|
# Monkey-patches Rack::Builder to use HttpRouter.
|
@@ -56,14 +56,15 @@ class HttpRouter
|
|
56
56
|
def initialize(*args, &block)
|
57
57
|
default_app, options = args.first.is_a?(Hash) ? [nil, args.first] : [args.first, args[1]]
|
58
58
|
|
59
|
-
@options
|
60
|
-
@default_app
|
61
|
-
@ignore_trailing_slash
|
62
|
-
@redirect_trailing_slash
|
63
|
-
@middleware
|
64
|
-
@
|
65
|
-
@
|
66
|
-
@
|
59
|
+
@options = options
|
60
|
+
@default_app = default_app || options && options[:default_app] || proc{|env| Rack::Response.new("Not Found", 404).finish }
|
61
|
+
@ignore_trailing_slash = options && options.key?(:ignore_trailing_slash) ? options[:ignore_trailing_slash] : true
|
62
|
+
@redirect_trailing_slash = options && options.key?(:redirect_trailing_slash) ? options[:redirect_trailing_slash] : false
|
63
|
+
@middleware = options && options.key?(:middleware) ? options[:middleware] : false
|
64
|
+
@request_methods_specified = Set.new
|
65
|
+
@routes = []
|
66
|
+
@named_routes = {}
|
67
|
+
@init_block = block
|
67
68
|
reset!
|
68
69
|
if block
|
69
70
|
instance_eval(&block)
|
data/lib/http_router/node.rb
CHANGED
@@ -110,6 +110,7 @@ class HttpRouter
|
|
110
110
|
current_node.create_linear
|
111
111
|
current_node.linear << [request_value, new_node]
|
112
112
|
else
|
113
|
+
router.request_methods_specified << request_value if method == :request_method
|
113
114
|
current_node.create_lookup
|
114
115
|
current_nodes[index == 0 ? current_node_index : current_nodes.length] = (current_node.lookup[request_value] ||= router.request_node)
|
115
116
|
end
|
data/lib/http_router/root.rb
CHANGED
@@ -1,7 +1,5 @@
|
|
1
1
|
class HttpRouter
|
2
2
|
class Root < Node
|
3
|
-
HttpRequestMethods = %w(HEAD GET HEAD POST DELETE PUT)
|
4
|
-
|
5
3
|
class AlternativeRequestMethods < Array
|
6
4
|
attr_accessor :request_method_found
|
7
5
|
end
|
@@ -35,19 +33,16 @@ class HttpRouter
|
|
35
33
|
else
|
36
34
|
nil
|
37
35
|
end
|
38
|
-
|
39
|
-
alternate_methods = (
|
36
|
+
elsif !router.request_methods_specified.empty?
|
37
|
+
alternate_methods = (router.request_methods_specified - [request.request_method]).select do |alternate_method|
|
40
38
|
test_request = request.dup
|
41
39
|
test_request.env['REQUEST_METHOD'] = alternate_method
|
42
40
|
node = find_on_parts(test_request, get_parts(request), [])
|
43
41
|
node && node.value
|
44
42
|
end
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
else
|
49
|
-
Response.unmatched(405, {"Allow" => alternate_methods.join(", ")})
|
50
|
-
end
|
43
|
+
alternate_methods.empty? ? nil : Response.unmatched(405, {"Allow" => alternate_methods.join(", ")})
|
44
|
+
else
|
45
|
+
nil
|
51
46
|
end
|
52
47
|
end
|
53
48
|
end
|
data/lib/http_router/route.rb
CHANGED
@@ -294,7 +294,7 @@ class HttpRouter
|
|
294
294
|
end
|
295
295
|
|
296
296
|
def anonymous_variable(pos)
|
297
|
-
"$#{pos}"
|
297
|
+
"$#{pos}".to_sym
|
298
298
|
end
|
299
299
|
|
300
300
|
def compile_paths
|
@@ -309,11 +309,11 @@ class HttpRouter
|
|
309
309
|
new_path = split_path.map do |part|
|
310
310
|
processed_parts = case part
|
311
311
|
when /^:([a-zA-Z_0-9]*)$/
|
312
|
-
v_name =
|
312
|
+
v_name = $1.empty? ? anonymous_variable(counter += 1) : $1.to_sym
|
313
313
|
router.variable(v_name, @matches_with[v_name])
|
314
314
|
when /^\*([a-zA-Z_0-9]*)$/
|
315
315
|
if position != split_path.size - 1
|
316
|
-
v_name =
|
316
|
+
v_name = $1.empty? ? anonymous_variable(counter += 1) : $1.to_sym
|
317
317
|
splitting_indexes << variable_position
|
318
318
|
remaining_path_parts = split_path[position + 1, split_path.size]
|
319
319
|
look_ahead_variable = remaining_path_parts.find{|p| p[0] == ?: || p[0] == ?*}
|
@@ -321,7 +321,7 @@ class HttpRouter
|
|
321
321
|
remaining_path_parts.index(look_ahead_variable) if look_ahead_variable
|
322
322
|
router.variable(v_name, /^(#{@matches_with[v_name] || '[^\/]*?'}\/)+(?=#{Regexp.quote(remaining_matcher)})/)
|
323
323
|
else
|
324
|
-
v_name =
|
324
|
+
v_name = $1.empty? ? anonymous_variable(counter += 1) : $1.to_sym
|
325
325
|
router.glob(v_name, @matches_with[v_name])
|
326
326
|
end
|
327
327
|
else
|
data/lib/http_router/version.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: http_router
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 11
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 3
|
9
|
-
-
|
10
|
-
version: 0.3.
|
9
|
+
- 12
|
10
|
+
version: 0.3.12
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Joshua Hull
|