Module: Plezi::Base::Router
- Defined in:
- lib/plezi/router/router.rb,
lib/plezi/router/adclient.rb
Overview
this module is incharge of routing requests to the correct Controller.
Defined Under Namespace
Classes: ADClient
Class Method Summary collapse
-
.call(env) ⇒ Object
called when an HTTP request had arrived.
-
.call_method ⇒ Object
returns the ‘call` method.
- .list ⇒ Object
-
.new(app) ⇒ Object
Creates a new router.
-
.route(path, controller) ⇒ Object
Creates a new route.
-
.url_for(controller, method_sym, params = {}) ⇒ Object
Returns the URL for requested controller method and paramerets.
Class Method Details
.call(env) ⇒ Object
called when an HTTP request had arrived
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/plezi/router/router.rb', line 25 def call(env) request = Rack::Request.new(env) response = Rack::Response.new ret = nil @routes.each { |route| ret = route.call(request, response); break if ret } unless ret return @app.call(env) if @app ret = ::Plezi::Base::Err404Ctrl.new._pl_respond(request, response, request.params) end response.write(ret) if ret.is_a?(String) return response.finish rescue => e puts e., e.backtrace response = Rack::Response.new response.write ::Plezi::Base::Err500Ctrl.new._pl_respond(request, response, request.params) return response.finish end |
.call_method ⇒ Object
returns the ‘call` method. Used repeatedly in middleware mode and only once in application mode.
44 45 46 |
# File 'lib/plezi/router/router.rb', line 44 def call_method @call_method ||= Plezi::Base::Router.method(:call) end |
.list ⇒ Object
67 68 69 |
# File 'lib/plezi/router/router.rb', line 67 def list @routes end |
.new(app) ⇒ Object
Creates a new router
16 17 18 19 20 21 22 |
# File 'lib/plezi/router/router.rb', line 16 def new(app) if app && app != call_method puts "Plezi #{ Plezi::VERSION } as Middleware" @app = app end Plezi.app end |
.route(path, controller) ⇒ Object
Creates a new route.
- ‘path`
-
should be a string describing the route. Named parameters are allowed.
- ‘controller`
-
should be a Class object that will receive all the class properties of a Plezi Controller, or one of the allowed keywords.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/plezi/router/router.rb', line 52 def route(path, controller) path = path.chomp('/'.freeze) unless path == '/'.freeze case controller when :client controller = ::Plezi::Base::Router::ADClient when :assets controller = ::Plezi::Base::Assets path << '/*'.freeze unless path[-1] == '*'.freeze when Regexp path << '/*'.freeze unless path[-1] == '*'.freeze return @routes << RouteRewrite.new(path, controller) end @routes << Route.new(path, controller) end |
.url_for(controller, method_sym, params = {}) ⇒ Object
Returns the URL for requested controller method and paramerets.
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/plezi/router/router.rb', line 72 def url_for(controller, method_sym, params = {}) # GET,PUT,POST,DELETE r = nil url = '/'.dup @routes.each do |tmp| case tmp.controller when Class next if tmp.controller != controller r = tmp break when Regexp nm = nil nm = tmp.param_names[0] if params[tmp.param_names[0]] nm ||= tmp.param_names[0].to_sym url << "#{params.delete nm}/" if params[nm] && params[nm].to_s =~ tmp.controller else next end end return nil if r.nil? case method_sym.to_sym when :new params.delete :id params.delete :_method params.delete '_method'.freeze params['id'.freeze] = :new when :create params['id'.freeze] = :new params.delete :id params['_method'.freeze] = :post params.delete :_method when :update params.delete :_method params['_method'.freeze] = :put when :delete params.delete :_method params['_method'.freeze] = :delete when :index params.delete 'id'.freeze params.delete '_method'.freeze params.delete :id params.delete :_method when :show raise "The URL for ':show' MUST contain a valid 'id' parameter for the object's index to display." unless params['id'.freeze].nil? && params[:id].nil? params.delete '_method'.freeze params.delete :_method else params.delete :id params['id'.freeze] = method_sym end names = r.param_names url.chomp! '/'.freeze url << r.prefix url.clear if url == '/'.freeze while names.any? && params[name[0]] url << "/#{Rack::Utils.escape params[names.shift]}" end url << '/'.freeze if url.empty? (url << '?') << Rack::Utils.build_nested_query(params) if params.any? url end |