Skip to content
Browse files

- improve handling queries that does not parse the regex

- handle nil results from routers better
  • Loading branch information...
1 parent fe0bdd4 commit 84bd168bbac8693420e288649aaf6124fdedfa38 @ripienaar committed
Showing with 30 additions and 24 deletions.
  1. +25 −21 lib/oldskool/router.rb
  2. +5 −3 lib/oldskool/sinatra_app.rb
View
46 lib/oldskool/router.rb
@@ -11,38 +11,42 @@ def error(msg)
def route(params)
defaulting = false
+ k = ""
+ q = params[:q]
+
if params[:q] =~ /^(\w+?)(\s.+)*$/
k = $1
q = $2.strip rescue ""
- default = nil
- handler = nil
+ end
- @config[:keywords].each do |keyword|
- default = keyword if [keyword[:keywords]].flatten.include?(:default)
+ default = nil
+ handler = nil
- handler = keyword if [keyword[:keywords]].flatten.include?(k)
+ @config[:keywords].each do |keyword|
+ default = keyword if [keyword[:keywords]].flatten.include?(:default)
- break if handler
- end
+ handler = keyword if [keyword[:keywords]].flatten.include?(k)
- if (!handler && default)
- defaulting = true
- handler = default
- elsif !handler
- return error("No handler for keyword #{k} found and no default handler specified")
- end
+ break if handler
+ end
+
+ if (!handler && default)
+ defaulting = true
+ handler = default
+ elsif !handler
+ return error("No handler for keyword #{k} found and no default handler specified")
+ end
- handler_class = "%sHandler" % [handler[:type].to_s.capitalize]
+ handler_class = "%sHandler" % [handler[:type].to_s.capitalize]
- if Oldskool.constants.include?(handler_class)
- if defaulting
- Oldskool.const_get(handler_class).new(params, handler, @config).handle_request("", params[:q])
- else
- Oldskool.const_get(handler_class).new(params, handler, @config).handle_request(k, q)
- end
+ if Oldskool.constants.include?(handler_class)
+ if defaulting
+ Oldskool.const_get(handler_class).new(params, handler, @config).handle_request("", params[:q])
else
- return error("Do not know how to handle type %s keywords" % [ handler[:type] ])
+ Oldskool.const_get(handler_class).new(params, handler, @config).handle_request(k, q)
end
+ else
+ return error("Do not know how to handle type %s keywords" % [ handler[:type] ])
end
end
end
View
8 lib/oldskool/sinatra_app.rb
@@ -43,9 +43,11 @@ def search_url
if params[:q]
@result = @router.route(params)
- @sidemenu = @result.delete(:sidemenu)
- @topmenu = @result.delete(:topmenu)
- @error = @result.delete(:error)
+ if @result
+ @sidemenu = @result.delete(:sidemenu)
+ @topmenu = @result.delete(:topmenu)
+ @error = @result.delete(:error)
+ end
case (template = @result[:template])
when :redirect

0 comments on commit 84bd168

Please sign in to comment.
Something went wrong with that request. Please try again.