![]() |
# Module: Capcode [ "README", "AUTHORS", "COPYING", "lib/capcode.rb", nil].each do Capcode.view_html Capcode::Views.view_html Capcode::Helpers.view_html Capcode::HTTPError.view_html Capcode::RenderError.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 202
202: def Route *u
203: Class.new {
204: meta_def(:__urls__){
205: # < Route '/hello/world/([^\/]*)/id(\d*)', '/hello/(.*)'
206: # # => [ {'/hello/world' => '([^\/]*)/id(\d*)', '/hello' => '(.*)'}, 2, <Capcode::Klass> ]
207: h = {}
208: max = 0
209: u.each do |_u|
210: m = /\/([^\/]*\(.*)/.match( _u )
211: if m.nil?
212: raise Capcode::RouteError, "Route `#{_u}' already defined with regexp `#{h[_u]}' !", caller if h.keys.include?(_u)
213: h[_u] = ''
214: else
215: _pre = m.pre_match
216: _pre = "/" if _pre.size == 0
217: raise Capcode::RouteError, "Route `#{_pre}' already defined with regexp `#{h[_pre]}' !", caller if h.keys.include?(_pre)
218: h[_pre] = m.captures[0]
219: max = Regexp.new(m.captures[0]).number_of_captures if max < Regexp.new(m.captures[0]).number_of_captures
220: end
221: end
222: [h, max, self]
223: }
224:
225: # Hash containing all the request parameters (GET or POST)
226: def params
227: @request.params
228: end
229:
230: # Hash containing all the environment variables
231: def env
232: @env
233: end
234:
235: # Hash session
236: def session
237: @env['rack.session']
238: end
239:
240: # Return the Rack::Request object
241: def request
242: @request
243: end
244:
245: # Return the Rack::Response object
246: def response
247: @response
248: end
249:
250: def call( e ) #:nodoc:
251: @env = e
252: @response = Rack::Response.new
253: @request = Rack::Request.new(@env)
254:
255: r = case @env["REQUEST_METHOD"]
256: when "GET"
257: finalPath = nil
258: finalArgs = nil
259: finalNArgs = nil
260:
261: aPath = @request.path.gsub( /^\//, "" ).split( "/" )
262: self.class.__urls__[0].each do |p, r|
263: xPath = p.gsub( /^\//, "" ).split( "/" )
264: if (xPath - aPath).size == 0
265: diffArgs = aPath - xPath
266: diffNArgs = diffArgs.size
267: if finalNArgs.nil? or finalNArgs > diffNArgs
268: finalPath = p
269: finalNArgs = diffNArgs
270: finalArgs = diffArgs
271: end
272: end
273:
274: end
275:
276: nargs = self.class.__urls__[1]
277: regexp = Regexp.new( self.class.__urls__[0][finalPath] )
278: args = regexp.match( Rack::Utils.unescape(@request.path).gsub( Regexp.new( "^#{finalPath}" ), "" ).gsub( /^\//, "" ) )
279: if args.nil?
280: raise Capcode::ParameterError, "Path info `#{@request.path_info}' does not match route regexp `#{regexp.source}'"
281: else
282: args = args.captures.map { |x| (x.size == 0)?nil:x }
283: end
284:
285: while args.size < nargs
286: args << nil
287: end
288:
289: get( *args )
290: when "POST"
291: post
292: end
293: if r.respond_to?(:to_ary)
294: @response.status = r[0]
295: r[1].each do |k,v|
296: @response[k] = v
297: end
298: @response.body = r[2]
299: else
300: @response.write r
301: end
302:
303: @response.finish
304: end
305:
306: include Capcode::Helpers
307: include Capcode::Views
308: }
309: end

Hash containing all the environment variables
[ show source ]
# File lib/capcode.rb, line 231
231: def env
232: @env
233: 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 316
316: def map( r, &b )
317: @@__ROUTES[r] = yield
318: end

Hash containing all the request parameters (GET or POST)
[ show source ]
# File lib/capcode.rb, line 226
226: def params
227: @request.params
228: end

Return the Rack::Request object
[ show source ]
# File lib/capcode.rb, line 241
241: def request
242: @request
243: end

Return the Rack::Response object
[ show source ]
# File lib/capcode.rb, line 246
246: def response
247: @response
248: end

Start your application.
Options :
[ show source ]
# File lib/capcode.rb, line 333
333: def run( args = {} )
334: conf = {
335: :port => args[:port]||3000,
336: :host => args[:host]||"localhost",
337: :server => args[:server]||nil,
338: :log => args[:log]||$stdout,
339: :session => args[:session]||{},
340: :pid => args[:pid]||"#{$0}.pid",
341: :daemonize => args[:daemonize]||false,
342: :db_config => args[:db_config]||"database.yml",
343: :root => args[:root]||".",
344: :working_directory => args[:working_directory]||File.expand_path(File.dirname($0))
345: }
346:
347: # Run in the Working directory
348: Dir.chdir( conf[:working_directory] ) do
349: # Set root directory for helpers
350: Capcode::Helpers.root = File.expand_path( conf[:root] )
351:
352: # Check that mongrel exists
353: if conf[:server].nil? || conf[:server] == "mongrel"
354: begin
355: require 'mongrel'
356: conf[:server] = "mongrel"
357: rescue LoadError
358: puts "!! could not load mongrel. Falling back to webrick."
359: conf[:server] = "webrick"
360: end
361: end
362:
363: Capcode.constants.each do |k|
364: begin
365: if eval "Capcode::#{k}.public_methods(true).include?( '__urls__' )"
366: u, m, c = eval "Capcode::#{k}.__urls__"
367: u.keys.each do |_u|
368: raise Capcode::RouteError, "Route `#{_u}' already define !", caller if @@__ROUTES.keys.include?(_u)
369: @@__ROUTES[_u] = c.new
370: end
371: end
372: rescue => e
373: raise e.message
374: end
375: end
376:
377: app = Rack::URLMap.new(@@__ROUTES)
378: app = Rack::Session::Cookie.new( app, conf[:session] )
379: app = Capcode::HTTPError.new(app, conf[:root])
380: app = Rack::ContentLength.new(app)
381: app = Rack::Lint.new(app)
382: app = Rack::ShowExceptions.new(app)
383: # app = Rack::Reloader.new(app) ## -- NE RELOAD QUE capcode.rb -- So !!!
384: app = Rack::CommonLogger.new( app, Logger.new(conf[:log]) )
385:
386: # From rackup !!!
387: if conf[:daemonize]
388: if RUBY_VERSION < "1.9"
389: exit if fork
390: Process.setsid
391: exit if fork
392: # Dir.chdir "/"
393: File.umask 0000
394: STDIN.reopen "/dev/null"
395: STDOUT.reopen "/dev/null", "a"
396: STDERR.reopen "/dev/null", "a"
397: else
398: Process.daemon
399: end
400:
401: File.open(conf[:pid], 'w'){ |f| f.write("#{Process.pid}") }
402: at_exit { File.delete(conf[:pid]) if File.exist?(conf[:pid]) }
403: end
404:
405: # Start database
406: if self.methods.include? "db_connect"
407: db_connect( conf[:db_config], conf[:log] )
408: end
409:
410: # Start server
411: case conf[:server]
412: when "mongrel"
413: puts "** Starting Mongrel on #{conf[:host]}:#{conf[:port]}"
414: Rack::Handler::Mongrel.run( app, {:Port => conf[:port], :Host => conf[:host]} ) { |server|
415: trap "SIGINT", proc { server.stop }
416: }
417: when "webrick"
418: puts "** Starting WEBrick on #{conf[:host]}:#{conf[:port]}"
419: Rack::Handler::WEBrick.run( app, {:Port => conf[:port], :BindAddress => conf[:host]} ) { |server|
420: trap "SIGINT", proc { server.shutdown }
421: }
422: end
423: end
424: end

Hash session
[ show source ]
# File lib/capcode.rb, line 236
236: def session
237: @env['rack.session']
238: end