Class: HttpRouter
- Inherits:
-
Object
show all
- Defined in:
- lib/http_router.rb,
lib/http_router/glob.rb,
lib/http_router/node.rb,
lib/http_router/path.rb,
lib/http_router/root.rb,
lib/http_router/route.rb,
lib/http_router/response.rb,
lib/http_router/variable.rb,
lib/http_router/interface/sinatra.rb
Defined Under Namespace
Modules: Interface, Response
Classes: Glob, Node, Path, RequestNode, Root, Route, Variable
Constant Summary
collapse
- UngeneratableRouteException =
Class.new(RuntimeError)
- MissingParameterException =
Class.new(RuntimeError)
- TooManyParametersException =
Class.new(RuntimeError)
- AlreadyCompiledException =
Class.new(RuntimeError)
- AmbiguousRouteException =
Class.new(RuntimeError)
- UnsupportedRequestConditionError =
Class.new(RuntimeError)
- AmbiguousVariableException =
Class.new(RuntimeError)
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(options = nil, &block) ⇒ HttpRouter
Returns a new instance of HttpRouter.
24
25
26
27
28
29
30
31
32
|
# File 'lib/http_router.rb', line 24
def initialize(options = nil, &block)
@default_app = options && options[:default_app] || proc{|env| ::Rack::Response.new("Not Found", 404).finish }
@ignore_trailing_slash = options && options.key?(:ignore_trailing_slash) ? options[:ignore_trailing_slash] : true
@redirect_trailing_slash = options && options.key?(:redirect_trailing_slash) ? options[:redirect_trailing_slash] : false
@routes = []
@named_routes = {}
reset!
instance_eval(&block) if block
end
|
Instance Attribute Details
#named_routes ⇒ Object
Returns the value of attribute named_routes.
22
23
24
|
# File 'lib/http_router.rb', line 22
def named_routes
@named_routes
end
|
#root ⇒ Object
Returns the value of attribute root.
22
23
24
|
# File 'lib/http_router.rb', line 22
def root
@root
end
|
#routes ⇒ Object
Returns the value of attribute routes.
22
23
24
|
# File 'lib/http_router.rb', line 22
def routes
@routes
end
|
Instance Method Details
#add(path, options = nil) ⇒ Object
56
57
58
59
60
|
# File 'lib/http_router.rb', line 56
def add(path, options = nil)
route = Route.new(self, path.dup).with_options(options)
@routes << route
route
end
|
#call(env) ⇒ Object
Allow the router to be called via Rake / Middleware.
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
# File 'lib/http_router.rb', line 99
def call(env)
request = Rack::Request.new(env)
if redirect_trailing_slash? && (request.head? || request.get?) && request.path_info[-1] == ?/
response = Rack::Response.new
response.redirect(request.path_info[0, request.path_info.size - 1], 302)
response.finish
else
env['router'] = self
if response = recognize(request)
if response.matched? && response.route.dest && response.route.dest.respond_to?(:call)
process_params(env, response)
consume_path!(request, response) if response.partial_match?
return response.route.dest.call(env)
elsif !response.matched?
return [response.status, response., []]
end
end
@default_app.call(env)
end
end
|
#default(app) ⇒ Object
48
49
50
|
# File 'lib/http_router.rb', line 48
def default(app)
@default_app = app
end
|
#delete(path, options = nil) ⇒ Object
74
75
76
|
# File 'lib/http_router.rb', line 74
def delete(path, options = nil)
add(path, options).delete
end
|
#get(path, options = nil) ⇒ Object
62
63
64
|
# File 'lib/http_router.rb', line 62
def get(path, options = nil)
add(path, options).get
end
|
#glob(*args) ⇒ Object
136
137
138
|
# File 'lib/http_router.rb', line 136
def glob(*args)
Glob.new(self, *args)
end
|
#ignore_trailing_slash? ⇒ Boolean
34
35
36
|
# File 'lib/http_router.rb', line 34
def ignore_trailing_slash?
@ignore_trailing_slash
end
|
#node(*args) ⇒ Object
121
122
123
|
# File 'lib/http_router.rb', line 121
def node(*args)
Node.new(self, *args)
end
|
#only_get(path, options = nil) ⇒ Object
78
79
80
|
# File 'lib/http_router.rb', line 78
def only_get(path, options = nil)
add(path, options).only_get
end
|
#post(path, options = nil) ⇒ Object
66
67
68
|
# File 'lib/http_router.rb', line 66
def post(path, options = nil)
add(path, options).post
end
|
#put(path, options = nil) ⇒ Object
70
71
72
|
# File 'lib/http_router.rb', line 70
def put(path, options = nil)
add(path, options).put
end
|
#recognize(env) ⇒ Object
82
83
84
|
# File 'lib/http_router.rb', line 82
def recognize(env)
response = @root.find(env.is_a?(Hash) ? Rack::Request.new(env) : env)
end
|
#redirect_trailing_slash? ⇒ Boolean
38
39
40
|
# File 'lib/http_router.rb', line 38
def redirect_trailing_slash?
@redirect_trailing_slash
end
|
#request_node(*args) ⇒ Object
Returns a new request node
126
127
128
|
# File 'lib/http_router.rb', line 126
def request_node(*args)
RequestNode.new(self, *args)
end
|
#reset! ⇒ Object
42
43
44
45
46
|
# File 'lib/http_router.rb', line 42
def reset!
@root = Root.new(self)
@routes.clear
@named_routes.clear
end
|
#split(path) ⇒ Object
52
53
54
|
# File 'lib/http_router.rb', line 52
def split(path)
(path[0] == ?/ ? path[1, path.size] : path).split('/')
end
|
#url(route, *args) ⇒ Object
Generate a URL for a specified route.
87
88
89
90
91
92
93
94
95
96
|
# File 'lib/http_router.rb', line 87
def url(route, *args)
case route
when Symbol
url(@named_routes[route], *args)
when nil
raise UngeneratableRouteException.new
else
route.url(*args)
end
end
|
#variable(*args) ⇒ Object
131
132
133
|
# File 'lib/http_router.rb', line 131
def variable(*args)
Variable.new(self, *args)
end
|