Class: Howl
- Inherits:
-
Object
- Object
- Howl
- Defined in:
- lib/howl-router.rb,
lib/howl-router/route.rb,
lib/howl-router/router.rb,
lib/howl-router/matcher.rb,
lib/howl-router/padrino.rb,
lib/howl-router/request.rb,
lib/howl-router/version.rb,
lib/howl-router/padrino/core.rb,
lib/howl-router/padrino/route.rb,
lib/howl-router/padrino/router.rb,
lib/howl-router/padrino/matcher.rb,
lib/howl-router/padrino/ext/class_methods.rb,
lib/howl-router/padrino/ext/instance_methods.rb
Direct Known Subclasses
Defined Under Namespace
Modules: Padrino Classes: InvalidRouteException, Matcher, Request, Route, Router
Constant Summary collapse
- HTTP_VERBS =
[:get, :post, :delete, :put, :head]
- RESPONSE_HEADERS =
{ :bad_request => 400, :not_found => 404, :method_not_allowed => 405, :server_error => 500 }
- VERSION =
'0.3.0'
Instance Method Summary collapse
-
#add(verb, path, options = {}) { ... } ⇒ Howl::Route
Generate a route, and add to routes.
-
#call(env) ⇒ Object
call method for Rack Application.
-
#compile ⇒ Array
Compile routes.
-
#compiled? ⇒ Boolean
Determines whether the compiled.
-
#initialize(&block) ⇒ Howl
constructor
A new instance of Howl.
-
#path(name, *args) ⇒ String
Find a route, and return a generated path of route.
-
#recognize(request) ⇒ Array
Recognize a request, and return a matched routes.
-
#recognize_path(path_info) ⇒ Array
Recognize a path_info, and return a matched first route’s name and params.
-
#reset! ⇒ Object
Reset a router.
-
#router ⇒ Howl::Router
Return a Router instance.
-
#routes ⇒ Array
Return a added routes.
Constructor Details
#initialize(&block) ⇒ Howl
Returns a new instance of Howl.
19 20 21 |
# File 'lib/howl-router.rb', line 19 def initialize(&block) instance_eval(&block) if block_given? end |
Instance Method Details
#add(verb, path, options = {}) { ... } ⇒ Howl::Route
Generate a route, and add to routes.
36 37 38 39 40 41 42 |
# File 'lib/howl-router.rb', line 36 def add(verb, path, = {}, &block) route = Route.new(path, &block) route.verb = verb.downcase.to_sym route.router = router router.routes << route route end |
#call(env) ⇒ Object
call method for Rack Application.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/howl-router.rb', line 45 def call(env) request = Request.new(env) return bad_request unless HTTP_VERBS.include?(request.request_method.downcase.to_sym) compile unless compiled? begin matched_routes = recognize(request) route, params = matched_routes.first result = route.arity != 0 ? route.call(params) : route.call [200, {'Content-Type' => 'text/html;charset=utf-8;'}, [result]] rescue => evar case evar when NotFound then not_found when MethodNotAllowed then method_not_allowed else server_error end end end |
#compile ⇒ Array
Compile routes.
75 76 77 78 |
# File 'lib/howl-router.rb', line 75 def compile @compiled = true router.compile end |
#compiled? ⇒ Boolean
Determines whether the compiled.
67 68 69 |
# File 'lib/howl-router.rb', line 67 def compiled? @compiled end |
#path(name, *args) ⇒ String
Find a route, and return a generated path of route.
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/howl-router.rb', line 138 def path(name, *args) params = args.delete_at(args.last.is_a?(Hash) ? -1 : 0) || {} saved_args = args.dup router.routes.each do |route| next unless route.name == name matcher = route.matcher if !args.empty? and matcher.mustermann? matcher_names = matcher.names = Hash[matcher_names.map{|matcher_name| [matcher_name.to_sym, (params[matcher_name.to_sym] || args.shift)] }] .merge!(Hash[params.select{|k, v| !matcher_names.include?(name.to_sym) }]) args = saved_args.dup else = params.dup end return matcher.mustermann? ? matcher.() : route.path end raise InvalidRouteException end |
#recognize(request) ⇒ Array
Recognize a request, and return a matched routes.
86 87 88 |
# File 'lib/howl-router.rb', line 86 def recognize(request) router.recognize(request) end |
#recognize_path(path_info) ⇒ Array
Recognize a path_info, and return a matched first route’s name and params.
95 96 97 98 99 |
# File 'lib/howl-router.rb', line 95 def recognize_path(path_info) response = router.recognize(Rack::MockRequest.env_for(path_info)) route, params = response.first [route.name, params] end |
#reset! ⇒ Object
Reset a router.
102 103 104 105 |
# File 'lib/howl-router.rb', line 102 def reset! @compiled = nil router.reset! end |
#router ⇒ Howl::Router
Return a Router instance.
111 112 113 |
# File 'lib/howl-router.rb', line 111 def router @router ||= Router.new end |
#routes ⇒ Array
Return a added routes.
119 120 121 |
# File 'lib/howl-router.rb', line 119 def routes router.routes end |