Module: Qeweney::RoutingMethods
- Included in:
- Request
- Defined in:
- lib/qeweney/routing.rb
Constant Summary collapse
- @@regexp_cache =
{}
Instance Method Summary collapse
- #default ⇒ Object
- #enter_route(depth = 1) ⇒ Object
- #is(route = '/', &block) ⇒ Object
- #leave_route(depth = 1) ⇒ Object
- #on(route, &block) ⇒ Object
- #on_accept(accept, &block) ⇒ Object
- #on_get(route = nil, &block) ⇒ Object
- #on_host(route, &block) ⇒ Object
- #on_options(route = nil, &block) ⇒ Object
- #on_plain_http(route, &block) ⇒ Object
- #on_post(route = nil, &block) ⇒ Object
- #on_query_param(key) ⇒ Object
- #on_root(&block) ⇒ Object
- #on_upgrade(protocol, &block) ⇒ Object
- #on_websocket_upgrade(&block) ⇒ Object
- #reject(body, status) ⇒ Object
- #route(&block) ⇒ Object
- #route_found(&block) ⇒ Object
- #route_part ⇒ Object
- #route_relative_path ⇒ Object
- #stop_routing ⇒ Object
Instance Method Details
#default ⇒ Object
137 138 139 140 |
# File 'lib/qeweney/routing.rb', line 137 def default yield throw :stop, :found end |
#enter_route(depth = 1) ⇒ Object
33 34 35 |
# File 'lib/qeweney/routing.rb', line 33 def enter_route(depth = 1) @path_parts_idx += depth end |
#is(route = '/', &block) ⇒ Object
53 54 55 56 57 |
# File 'lib/qeweney/routing.rb', line 53 def is(route = '/', &block) return unless @path_parts[@path_parts_idx] == route && @path_parts_idx >= @path_parts.size route_found(&block) end |
#leave_route(depth = 1) ⇒ Object
37 38 39 |
# File 'lib/qeweney/routing.rb', line 37 def leave_route(depth = 1) @path_parts_idx -= depth end |
#on(route, &block) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/qeweney/routing.rb', line 41 def on(route, &block) return route_found(&block) unless route route_parts = route.split('/') route_length = route_parts.size return unless @path_parts[@path_parts_idx, route_length] == route_parts enter_route(route_length) route_found(&block) leave_route(route_length) end |
#on_accept(accept, &block) ⇒ Object
108 109 110 111 112 113 114 115 116 |
# File 'lib/qeweney/routing.rb', line 108 def on_accept(accept, &block) if accept.is_a?(Regexp) return unless headers['accept'] =~ accept else return unless headers['accept'] == accept end route_found(&block) end |
#on_get(route = nil, &block) ⇒ Object
77 78 79 80 81 |
# File 'lib/qeweney/routing.rb', line 77 def on_get(route = nil, &block) return unless method == 'get' on(route, &block) end |
#on_host(route, &block) ⇒ Object
65 66 67 68 69 |
# File 'lib/qeweney/routing.rb', line 65 def on_host(route, &block) return unless host == route route_found(&block) end |
#on_options(route = nil, &block) ⇒ Object
89 90 91 92 93 |
# File 'lib/qeweney/routing.rb', line 89 def (route = nil, &block) return unless method == 'options' on(route, &block) end |
#on_plain_http(route, &block) ⇒ Object
71 72 73 74 75 |
# File 'lib/qeweney/routing.rb', line 71 def on_plain_http(route, &block) return unless scheme == 'http' route_found(&block) end |
#on_post(route = nil, &block) ⇒ Object
83 84 85 86 87 |
# File 'lib/qeweney/routing.rb', line 83 def on_post(route = nil, &block) return unless method == 'post' on(route, &block) end |
#on_query_param(key) ⇒ Object
101 102 103 104 105 106 |
# File 'lib/qeweney/routing.rb', line 101 def on_query_param(key) value = query[key] return unless value route_found { yield value } end |
#on_root(&block) ⇒ Object
59 60 61 62 63 |
# File 'lib/qeweney/routing.rb', line 59 def on_root(&block) return unless @path_parts_idx > @path_parts.size - 1 route_found(&block) end |
#on_upgrade(protocol, &block) ⇒ Object
95 96 97 98 99 |
# File 'lib/qeweney/routing.rb', line 95 def on_upgrade(protocol, &block) return unless upgrade_protocol == protocol route_found(&block) end |
#on_websocket_upgrade(&block) ⇒ Object
124 125 126 |
# File 'lib/qeweney/routing.rb', line 124 def on_websocket_upgrade(&block) on_upgrade('websocket', &block) end |
#reject(body, status) ⇒ Object
128 129 130 131 |
# File 'lib/qeweney/routing.rb', line 128 def reject(body, status) respond(body, ':status' => status) throw :stop, :found end |
#route(&block) ⇒ Object
9 10 11 12 13 14 15 16 |
# File 'lib/qeweney/routing.rb', line 9 def route(&block) @path_parts ||= path.split('/') @path_parts_idx ||= 1 res = catch(:stop) { yield self } return if res == :found respond(nil, ':status' => 404) end |
#route_found(&block) ⇒ Object
18 19 20 21 |
# File 'lib/qeweney/routing.rb', line 18 def route_found(&block) catch(:stop, &block) throw :stop, :found end |
#route_part ⇒ Object
25 26 27 |
# File 'lib/qeweney/routing.rb', line 25 def route_part @path_parts[@path_parts_idx] end |
#route_relative_path ⇒ Object
29 30 31 |
# File 'lib/qeweney/routing.rb', line 29 def route_relative_path @path_parts.empty? ? '/' : "/#{@path_parts[@path_parts_idx..-1].join('/')}" end |
#stop_routing ⇒ Object
133 134 135 |
# File 'lib/qeweney/routing.rb', line 133 def stop_routing throw :stop, :found end |