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 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 = 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
- @routes = []
65
- @named_routes = {}
66
- @init_block = block
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)
@@ -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
@@ -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
- else
39
- alternate_methods = (HttpRequestMethods - [request.request_method]).select do |alternate_method|
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
- if alternate_methods.empty?
47
- nil
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
@@ -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 = ($1.empty? ? anonymous_variable(counter += 1) : $1).to_sym
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 = ($1.empty? ? anonymous_variable(counter += 1) : $1).to_sym
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 = ($1.empty? ? anonymous_variable(counter += 1) : $1).to_sym
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
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  class HttpRouter #:nodoc
3
- VERSION = '0.3.11'
3
+ VERSION = '0.3.12'
4
4
  end
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: 5
4
+ hash: 11
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 3
9
- - 11
10
- version: 0.3.11
9
+ - 12
10
+ version: 0.3.12
11
11
  platform: ruby
12
12
  authors:
13
13
  - Joshua Hull