http_router 0.10.2 → 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. data/.gitignore +2 -1
  2. data/Rakefile +7 -5
  3. data/benchmarks/gen2.rb +1 -1
  4. data/benchmarks/rack_mount.rb +8 -14
  5. data/examples/rack_mapper.ru +12 -13
  6. data/examples/variable_with_regex.ru +1 -1
  7. data/http_router.gemspec +1 -1
  8. data/lib/http_router.rb +159 -62
  9. data/lib/http_router/generation_helper.rb +29 -0
  10. data/lib/http_router/generator.rb +150 -0
  11. data/lib/http_router/node.rb +27 -17
  12. data/lib/http_router/node/abstract_request_node.rb +31 -0
  13. data/lib/http_router/node/host.rb +9 -0
  14. data/lib/http_router/node/lookup.rb +8 -10
  15. data/lib/http_router/node/path.rb +23 -38
  16. data/lib/http_router/node/request_method.rb +16 -0
  17. data/lib/http_router/node/root.rb +104 -10
  18. data/lib/http_router/node/scheme.rb +9 -0
  19. data/lib/http_router/node/user_agent.rb +9 -0
  20. data/lib/http_router/regex_route_generation.rb +10 -0
  21. data/lib/http_router/request.rb +7 -17
  22. data/lib/http_router/response.rb +4 -0
  23. data/lib/http_router/route.rb +16 -277
  24. data/lib/http_router/route_helper.rb +126 -0
  25. data/lib/http_router/util.rb +1 -37
  26. data/lib/http_router/version.rb +1 -1
  27. data/test/common/generate.txt +1 -1
  28. data/test/generation.rb +15 -11
  29. data/test/generic.rb +2 -3
  30. data/test/helper.rb +15 -10
  31. data/test/rack/test_route.rb +0 -5
  32. data/test/test_misc.rb +50 -40
  33. data/test/test_mounting.rb +27 -26
  34. data/test/test_recognition.rb +1 -76
  35. metadata +104 -161
  36. data/.rspec +0 -1
  37. data/lib/http_router/node/arbitrary.rb +0 -30
  38. data/lib/http_router/node/request.rb +0 -52
  39. data/lib/http_router/rack.rb +0 -19
  40. data/lib/http_router/rack/builder.rb +0 -61
  41. data/lib/http_router/rack/url_map.rb +0 -16
  42. data/lib/http_router/regex_route.rb +0 -39
@@ -1,61 +0,0 @@
1
- require 'http_router'
2
-
3
- # Replacement for {Rack::Builder} which using HttpRouter to map requests instead of a simple Hash.
4
- # As well, add convenience methods for the request methods.
5
- module HttpRouter::Rack::BuilderMixin
6
- def router
7
- @router ||= HttpRouter.new
8
- end
9
-
10
- # Maps a path to a block.
11
- # @param path [String] Path to map to.
12
- # @param options [Hash] Options for added path.
13
- # @see HttpRouter#add
14
- def map(path, options = {}, method = nil, &block)
15
- route = router.add(path, options)
16
- route.send(method) if method
17
- route.to(&block)
18
- @ins << router unless @ins.last == router
19
- route
20
- end
21
-
22
- # Maps a path with request methods `HEAD` and `GET` to a block.
23
- # @param path [String] Path to map to.
24
- # @param options [Hash] Options for added path.
25
- # @see HttpRouter#add
26
- def get(path, options = {}, &block)
27
- map(path, options, :get, &block)
28
- end
29
-
30
- # Maps a path with request methods `POST` to a block.
31
- # @param path [String] Path to map to.
32
- # @param options [Hash] Options for added path.
33
- # @see HttpRouter#add
34
- def post(path, options = {}, &block)
35
- map(path, options, :post, &block)
36
- end
37
-
38
- # Maps a path with request methods `PUT` to a block.
39
- # @param path [String] Path to map to.
40
- # @param options [Hash] Options for added path.
41
- # @see HttpRouter#add
42
- def put(path, options = {}, &block)
43
- map(path, options, :put, &block)
44
- end
45
-
46
- # Maps a path with request methods `DELETE` to a block.
47
- # @param path [String] Path to map to.
48
- # @param options [Hash] Options for added path.
49
- # @see HttpRouter#add
50
- def delete(path, options = {}, &block)
51
- map(path, options, :delete, &block)
52
- end
53
-
54
- def options(path, options = {}, &block)
55
- map(path, options, :options, &block)
56
- end
57
- end
58
-
59
- class HttpRouter::Rack::Builder < ::Rack::Builder
60
- include HttpRouter::Rack::BuilderMixin
61
- end
@@ -1,16 +0,0 @@
1
- require 'http_router'
2
-
3
- class HttpRouter
4
- module Rack
5
- class URLMap < ::Rack::URLMap
6
- def initialize(map = {})
7
- @router = HttpRouter.new
8
- map.each { |path, app| (path =~ /^(https?):\/\/(.*?)(\/.*)/ ? @router.add($3).host($2).scheme($1) : @router.add(path)).partial.to(app) }
9
- end
10
-
11
- def call(env)
12
- @router.call(env)
13
- end
14
- end
15
- end
16
- end
@@ -1,39 +0,0 @@
1
- class HttpRouter
2
- class RegexRoute < Route
3
- def initialize(router, path, opts = {})
4
- @router, @original_path, @opts = router, path, opts
5
- @param_names = @original_path.respond_to?(:names) ? @original_path.names.map(&:to_sym) : []
6
- @path_validation_regex = original_path
7
- Util.add_path_generation(self, self, opts.delete(:path_for_generation), @original_path) if opts.key?(:path_for_generation)
8
- process_opts
9
- end
10
-
11
- def add_path_to_tree
12
- @paths = [@original_path]
13
- add_non_path_to_tree(@router.root.add_free_match(@original_path), path, @param_names)
14
- end
15
-
16
- def significant_variable_names
17
- @param_names
18
- end
19
-
20
- def match_partially?
21
- true
22
- end
23
-
24
- def regex?
25
- true
26
- end
27
-
28
- def generate_from?(params)
29
- false
30
- end
31
-
32
- def url_with_params(*a)
33
- url_args_processing(a) do |args, options|
34
- respond_to?(:raw_url) or raise InvalidRouteException
35
- raw_url(args, options)
36
- end
37
- end
38
- end
39
- end