![]() |
# 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::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 137
137: def Route *u
138: Class.new {
139: meta_def(:__urls__){
140: # < Route '/hello/world/([^\/]*)/id(\d*)', '/hello/(.*)'
141: # # => [ {'/hello/world' => '([^\/]*)/id(\d*)', '/hello' => '(.*)'}, 2, <Capcode::Klass> ]
142: h = {}
143: max = 0
144: u.each do |_u|
145: m = /\/([^\/]*\(.*)/.match( _u )
146: if m.nil?
147: raise Capcode::RouteError, "Route `#{_u}' already defined with regexp `#{h[_u]}' !", caller if h.keys.include?(_u)
148: h[_u] = ''
149: else
150: _pre = m.pre_match
151: _pre = "/" if _pre.size == 0
152: raise Capcode::RouteError, "Route `#{_pre}' already defined with regexp `#{h[_pre]}' !", caller if h.keys.include?(_pre)
153: h[_pre] = m.captures[0]
154: max = Regexp.new(m.captures[0]).number_of_captures if max < Regexp.new(m.captures[0]).number_of_captures
155: end
156: end
157: [h, max, self]
158: }
159:
160: # Hash containing all the request parameters (GET or POST)
161: def params
162: @request.params
163: end
164:
165: # Hash containing all the environment variables
166: def env
167: @env
168: end
169:
170: # Hash session
171: def session
172: @env['rack.session']
173: end
174:
175: # Return the Rack::Request object
176: def request
177: @request
178: end
179:
180: # Return the Rack::Response object
181: def response
182: @response
183: end
184:
185: def call( e ) #:nodoc:
186: @env = e
187: @response = Rack::Response.new
188: @request = Rack::Request.new(@env)
189:
190: r = case @env["REQUEST_METHOD"]
191: when "GET"
192: finalPath = nil
193: finalArgs = nil
194: finalNArgs = nil
195:
196: aPath = @request.path.gsub( /^\//, "" ).split( "/" )
197: self.class.__urls__[0].each do |p, r|
198: xPath = p.gsub( /^\//, "" ).split( "/" )
199: if (xPath - aPath).size == 0
200: diffArgs = aPath - xPath
201: diffNArgs = diffArgs.size
202: if finalNArgs.nil? or finalNArgs > diffNArgs
203: finalPath = p
204: finalNArgs = diffNArgs
205: finalArgs = diffArgs
206: end
207: end
208:
209: end
210:
211: nargs = self.class.__urls__[1]
212: regexp = Regexp.new( self.class.__urls__[0][finalPath] )
213:
214: args = regexp.match( Rack::Utils.unescape(@request.path).gsub( Regexp.new( "^#{finalPath}" ), "" ).gsub( /^\//, "" ) )
215: if args.nil?
216: raise Capcode::ParameterError, "Path info `#{@request.path_info}' does not match route regexp `#{regexp.source}'"
217: else
218: args = args.captures
219: end
220:
221: while args.size < nargs
222: args << nil
223: end
224:
225: get( *args )
226: when "POST"
227: post
228: end
229: if r.respond_to?(:to_ary)
230: @response.status = r[0]
231: r[1].each do |k,v|
232: @response[k] = v
233: end
234: @response.body = r[2]
235: else
236: @response.write r
237: end
238:
239: @response.finish
240: end
241:
242: include Capcode::Helpers
243: include Capcode::Views
244: }
245: end

Hash containing all the environment variables
[ show source ]
# File lib/capcode.rb, line 166
166: def env
167: @env
168: 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 252
252: def map( r, &b )
253: @@__ROUTES[r] = yield
254: end

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

Return the Rack::Request object
[ show source ]
# File lib/capcode.rb, line 176
176: def request
177: @request
178: end

Return the Rack::Response object
[ show source ]
# File lib/capcode.rb, line 181
181: def response
182: @response
183: end

Start your application.
Options :
[ show source ]
# File lib/capcode.rb, line 267
267: def run( args = {} )
268: conf = {
269: :port => args[:port]||3000,
270: :host => args[:host]||"localhost",
271: :server => args[:server]||nil,
272: :log => args[:log]||$stdout,
273: :session => args[:session]||{},
274: :pid => args[:pid]||"#{$0}.pid",
275: :daemonize => args[:daemonize]||false,
276: :db_config => args[:db_config]||"database.yml"
277: }
278:
279: # Check that mongrel exists
280: if conf[:server].nil? || conf[:server] == "mongrel"
281: begin
282: require 'mongrel'
283: conf[:server] = "mongrel"
284: rescue LoadError
285: puts "!! could not load mongrel. Falling back to webrick."
286: conf[:server] = "webrick"
287: end
288: end
289:
290: Capcode.constants.each do |k|
291: begin
292: if eval "Capcode::#{k}.public_methods(true).include?( '__urls__' )"
293: u, m, c = eval "Capcode::#{k}.__urls__"
294: u.keys.each do |_u|
295: raise Capcode::RouteError, "Route `#{_u}' already define !", caller if @@__ROUTES.keys.include?(_u)
296: @@__ROUTES[_u] = c.new
297: end
298: end
299: rescue => e
300: raise e.message
301: end
302: end
303:
304: app = Rack::URLMap.new(@@__ROUTES)
305: app = Rack::Session::Cookie.new( app, conf[:session] )
306: app = Capcode::HTTPError.new(app)
307: app = Rack::ContentLength.new(app)
308: app = Rack::Lint.new(app)
309: app = Rack::ShowExceptions.new(app)
310: # app = Rack::Reloader.new(app) ## -- NE RELOAD QUE capcode.rb -- So !!!
311: app = Rack::CommonLogger.new( app, Logger.new(conf[:log]) )
312:
313: # From rackup !!!
314: if conf[:daemonize]
315: if RUBY_VERSION < "1.9"
316: exit if fork
317: Process.setsid
318: exit if fork
319: # Dir.chdir "/"
320: File.umask 0000
321: STDIN.reopen "/dev/null"
322: STDOUT.reopen "/dev/null", "a"
323: STDERR.reopen "/dev/null", "a"
324: else
325: Process.daemon
326: end
327:
328: File.open(conf[:pid], 'w'){ |f| f.write("#{Process.pid}") }
329: at_exit { File.delete(conf[:pid]) if File.exist?(conf[:pid]) }
330: end
331:
332: # Start database
333: if self.methods.include? "db_connect"
334: db_connect( conf[:db_config], conf[:log] )
335: end
336:
337: # Start server
338: case conf[:server]
339: when "mongrel"
340: puts "** Starting Mongrel on #{conf[:host]}:#{conf[:port]}"
341: Rack::Handler::Mongrel.run( app, {:Port => conf[:port], :Host => conf[:host]} ) { |server|
342: trap "SIGINT", proc { server.stop }
343: }
344: when "webrick"
345: puts "** Starting WEBrick on #{conf[:host]}:#{conf[:port]}"
346: Rack::Handler::WEBrick.run( app, {:Port => conf[:port], :BindAddress => conf[:host]} ) { |server|
347: trap "SIGINT", proc { server.shutdown }
348: }
349: end
350: end

Hash session
[ show source ]
# File lib/capcode.rb, line 171
171: def session
172: @env['rack.session']
173: end