![]() |
# Module: Capcode [ "README", "AUTHORS", "COPYING", "lib/capcode.rb", nil].each do Capcode.view_html Capcode::Helpers.view_html Capcode::HTTPError.view_html Capcode::RouteError.view_html Capcode::ParameterError.view_html end |
Add routes to a controller class
module Capcode class Hello < Route '/hello/(.*)', '/hello/([^#]*)#(.*)' def get( arg1, arg2 ) ... end end end
In the get method, you will receive the maximum of parameters declared by the routes. In this example, you will receive 2 parameters. So if you go to /hello/world#friend then arg1 will be set to world and arg2 will be set to friend. Now if you go to /hello/you, then arg1 will be set to you and arg2 will be set to nil
If the regexp in the route does not match, all arguments will be nil
[ show source ]
# File lib/capcode.rb, line 130 130: def Route *u 131: Class.new { 132: meta_def(:__urls__){ 133: # < Route '/hello/world/([^\/]*)/id(\d*)', '/hello/(.*)' 134: # # => [ {'/hello/world' => '([^\/]*)/id(\d*)', '/hello' => '(.*)'}, 2, <Capcode::Klass> ] 135: h = {} 136: max = 0 137: u.each do |_u| 138: m = /\/([^\/]*\(.*)/.match( _u ) 139: if m.nil? 140: raise Capcode::RouteError, "Route `#{_u}' already defined with regexp `#{h[_u]}' !", caller if h.keys.include?(_u) 141: h[_u] = '' 142: else 143: _pre = m.pre_match 144: _pre = "/" if _pre.size == 0 145: raise Capcode::RouteError, "Route `#{_pre}' already defined with regexp `#{h[_pre]}' !", caller if h.keys.include?(_pre) 146: h[_pre] = m.captures[0] 147: max = Regexp.new(m.captures[0]).number_of_captures if max < Regexp.new(m.captures[0]).number_of_captures 148: end 149: end 150: [h, max, self] 151: } 152: 153: # Hash containing all the request parameters (GET or POST) 154: def params 155: @request.params 156: end 157: 158: # Hash containing all the environment variables 159: def env 160: @env 161: end 162: 163: # Hash session 164: def session 165: @env['rack.session'] 166: end 167: 168: # Return the Rack::Request object 169: def request 170: @request 171: end 172: 173: # Return the Rack::Response object 174: def response 175: @response 176: end 177: 178: def call( e ) #:nodoc: 179: @env = e 180: @response = Rack::Response.new 181: @request = Rack::Request.new(@env) 182: 183: r = case @env["REQUEST_METHOD"] 184: when "GET" 185: sc = @request.script_name 186: sc = "/" if sc.size == 0 187: regexp = Regexp.new( self.class.__urls__[0][sc] ) 188: nargs = self.class.__urls__[1] 189: 190: args = regexp.match( Rack::Utils.unescape(@request.path_info).gsub( /^\//, "" ) ) 191: if args.nil? 192: raise Capcode::ParameterError, "Path info `#{@request.path_info}' does not match route regexp `#{regexp.source}'" 193: else 194: args = args.captures 195: end 196: 197: while args.size < nargs 198: args << nil 199: end 200: 201: get( *args ) 202: when "POST" 203: post 204: end 205: if r.respond_to?(:to_ary) 206: @response.status = r[0] 207: r[1].each do |k,v| 208: @response[k] = v 209: end 210: @response.body = r[2] 211: else 212: @response.write r 213: end 214: 215: @response.finish 216: end 217: 218: include Capcode::Helpers 219: } 220: end
Hash containing all the environment variables
[ show source ]
# File lib/capcode.rb, line 159 159: def env 160: @env 161: end
This method help you to map and URL to a Rack or What you want Helper
Capcode.map( "/file" ) do Rack::File.new( "." ) end
[ show source ]
# File lib/capcode.rb, line 227 227: def map( r, &b ) 228: @@__ROUTES[r] = yield 229: end
Hash containing all the request parameters (GET or POST)
[ show source ]
# File lib/capcode.rb, line 154 154: def params 155: @request.params 156: end
Return the Rack::Request object
[ show source ]
# File lib/capcode.rb, line 169 169: def request 170: @request 171: end
Return the Rack::Response object
[ show source ]
# File lib/capcode.rb, line 174 174: def response 175: @response 176: end
Start your application.
Options :
[ show source ]
# File lib/capcode.rb, line 241 241: def run( args = {} ) 242: conf = { 243: :port => args[:port]||3000, 244: :host => args[:host]||"localhost", 245: :server => args[:server]||nil, 246: :log => args[:log]||$stdout, 247: :session => args[:session]||{}, 248: :pid => args[:pid]||"#{$0}.pid", 249: :daemonize => args[:daemonize]||false, 250: :db_config => args[:db_config]||"database.yml" 251: } 252: 253: # Check that mongrel exists 254: if conf[:server].nil? || conf[:server] == "mongrel" 255: begin 256: require 'mongrel' 257: conf[:server] = "mongrel" 258: rescue LoadError 259: puts "!! could not load mongrel. Falling back to webrick." 260: conf[:server] = "webrick" 261: end 262: end 263: 264: Capcode.constants.each do |k| 265: begin 266: if eval "Capcode::#{k}.public_methods(true).include?( '__urls__' )" 267: u, m, c = eval "Capcode::#{k}.__urls__" 268: u.keys.each do |_u| 269: raise Capcode::RouteError, "Route `#{_u}' already define !", caller if @@__ROUTES.keys.include?(_u) 270: @@__ROUTES[_u] = c.new 271: end 272: end 273: rescue => e 274: raise e.message 275: end 276: end 277: 278: app = Rack::URLMap.new(@@__ROUTES) 279: app = Rack::Session::Cookie.new( app, conf[:session] ) 280: app = Capcode::HTTPError.new(app) 281: app = Rack::ContentLength.new(app) 282: app = Rack::Lint.new(app) 283: app = Rack::ShowExceptions.new(app) 284: # app = Rack::Reloader.new(app) ## -- NE RELOAD QUE capcode.rb -- So !!! 285: app = Rack::CommonLogger.new( app, Logger.new(conf[:log]) ) 286: 287: # From rackup !!! 288: if conf[:daemonize] 289: if RUBY_VERSION < "1.9" 290: exit if fork 291: Process.setsid 292: exit if fork 293: # Dir.chdir "/" 294: File.umask 0000 295: STDIN.reopen "/dev/null" 296: STDOUT.reopen "/dev/null", "a" 297: STDERR.reopen "/dev/null", "a" 298: else 299: Process.daemon 300: end 301: 302: File.open(conf[:pid], 'w'){ |f| f.write("#{Process.pid}") } 303: at_exit { File.delete(conf[:pid]) if File.exist?(conf[:pid]) } 304: end 305: 306: # Start database 307: if self.methods.include? "db_connect" 308: db_connect( conf[:db_config], conf[:log] ) 309: end 310: 311: # Start server 312: case conf[:server] 313: when "mongrel" 314: puts "** Starting Mongrel on #{conf[:host]}:#{conf[:port]}" 315: Rack::Handler::Mongrel.run( app, {:Port => conf[:port], :Host => conf[:host]} ) { |server| 316: trap "SIGINT", proc { server.stop } 317: } 318: when "webrick" 319: puts "** Starting WEBrick on #{conf[:host]}:#{conf[:port]}" 320: Rack::Handler::WEBrick.run( app, {:Port => conf[:port], :BindAddress => conf[:host]} ) { |server| 321: trap "SIGINT", proc { server.shutdown } 322: } 323: end 324: end
Hash session
[ show source ]
# File lib/capcode.rb, line 164 164: def session 165: @env['rack.session'] 166: end