# Module: Capcode
[ "README", "AUTHORS", "COPYING", "lib/capcode.rb", nil].each do
Capcode.view_html
Capcode::Helpers.view_html
Capcode::HTTPError.view_html
Capcode::ParameterError.view_html
end

Module Capcode

(in files lib/capcode.rb )

Includes

Methods

Public Class method: Route(*u)

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

     # File lib/capcode.rb, line 127
127:     def Route *u
128:       Class.new {
129:         meta_def(:__urls__){
130:           # < Route '/hello/world/([^\/]*)/id(\d*)', '/hello/(.*)'
131:           # # => [ {'/hello/world' => '([^\/]*)/id(\d*)', '/hello' => '(.*)'}, 2, <Capcode::Klass> ]
132:           h = {}
133:           max = 0
134:           u.each do |_u|
135:             m = /\/([^\/]*\(.*)/.match( _u )
136:             if m.nil?
137:               h[_u] = ''
138:             else
139:               h[m.pre_match] = m.captures[0]
140:               max = Regexp.new(m.captures[0]).number_of_captures if max < Regexp.new(m.captures[0]).number_of_captures
141:             end
142:           end
143:           [h, max, self]
144:         }
145: 
146:         # Hash containing all the request parameters (GET or POST)
147:         def params
148:           @request.params
149:         end
150:         
151:         # Hash containing all the environment variables
152:         def env
153:           @env
154:         end
155:         
156:         # Hash session
157:         def session
158:           @env['rack.session']
159:         end
160:         
161:         # Return the Rack::Request object
162:         def request
163:           @request
164:         end
165:         
166:         # Return the Rack::Response object
167:         def response
168:           @response
169:         end
170:         
171:         def call( e ) #:nodoc:
172:           @env = e
173:           @response = Rack::Response.new
174:           @request = Rack::Request.new(@env)
175: 
176:           r = case @env["REQUEST_METHOD"]
177:             when "GET"
178:               sc = @request.script_name
179:               sc = "/" if sc.size == 0
180:               regexp = Regexp.new( self.class.__urls__[0][sc] )
181:               nargs = self.class.__urls__[1]
182:               
183:               args = regexp.match( Rack::Utils.unescape(@request.path_info).gsub( /^\//, "" ) )
184:               if args.nil?
185:                 raise Capcode::ParameterError, "Path info `#{@request.path_info}' does not match route regexp `#{regexp.source}'"
186:               else
187:                 args = args.captures
188:               end
189:               
190:               while args.size < nargs
191:                 args << nil
192:               end
193:                                           
194:               get( *args )
195:             when "POST"
196:               post
197:           end
198:           if r.respond_to?(:to_ary)
199:             @response.status = r[0]
200:             r[1].each do |k,v|
201:               @response[k] = v
202:             end
203:             @response.body = r[2]
204:           else
205:             @response.write r
206:           end
207:           
208:           @response.finish
209:         end
210:                 
211:         include Capcode::Helpers
212:       }      
213:     end

Public Class method: env()

Hash containing all the environment variables

     # File lib/capcode.rb, line 152
152:         def env
153:           @env
154:         end

Public Class method: map( r ) {|| ...}

This method help you to map and URL to a Rack or What you want Helper

  Capcode.map( "/file" ) do
    Rack::File.new( "." )
  end
     # File lib/capcode.rb, line 220
220:     def map( r, &b )
221:       @@__ROUTES[r] = yield
222:     end

Public Class method: params()

Hash containing all the request parameters (GET or POST)

     # File lib/capcode.rb, line 147
147:         def params
148:           @request.params
149:         end

Public Class method: request()

Return the Rack::Request object

     # File lib/capcode.rb, line 162
162:         def request
163:           @request
164:         end

Public Class method: response()

Return the Rack::Response object

     # File lib/capcode.rb, line 167
167:         def response
168:           @response
169:         end

Public Class method: run( args = {} )

Start your application.

Options :

  • :port = Listen port
  • :host = Listen host
  • :server = Server type (webrick or mongrel)
  • :log = Output logfile (default: STDOUT)
  • :session = Session parameters. See Rack::Session for more informations
     # File lib/capcode.rb, line 232
232:     def run( args = {} )
233:       conf = {
234:         :port => args[:port]||3000, 
235:         :host => args[:host]||"localhost",
236:         :server => args[:server]||nil,
237:         :log => args[:log]||$stdout,
238:         :session => args[:session]||{}
239:       }
240:       
241:       # Check that mongrel exists 
242:       if conf[:server].nil? || conf[:server] == "mongrel"
243:         begin
244:           require 'mongrel'
245:           conf[:server] = "mongrel"
246:         rescue LoadError 
247:           puts "!! could not load mongrel. Falling back to webrick."
248:           conf[:server] = "webrick"
249:         end
250:       end
251:           
252:       Capcode.constants.each do |k|
253:         begin
254:           if eval "Capcode::#{k}.public_methods(true).include?( '__urls__' )"
255:             u, m, c = eval "Capcode::#{k}.__urls__"
256:             u.keys.each do |_u|
257:               @@__ROUTES[_u] = c.new
258:             end
259:           end
260:         rescue => e
261:           raise e.message
262:         end
263:       end
264:       
265:       app = Rack::URLMap.new(@@__ROUTES)
266:       app = Rack::Session::Cookie.new( app, conf[:session] )
267:       app = Capcode::HTTPError.new(app)
268:       app = Rack::ContentLength.new(app)
269:       app = Rack::Lint.new(app)
270:       app = Rack::ShowExceptions.new(app)
271: #      app = Rack::Reloader.new(app) ## -- NE RELOAD QUE capcode.rb -- So !!!
272:       app = Rack::CommonLogger.new( app, Logger.new(conf[:log]) )
273:       
274:       
275:       case conf[:server]
276:       when "mongrel"
277:         puts "** Starting Mongrel on #{conf[:host]}:#{conf[:port]}"
278:         Rack::Handler::Mongrel.run( app, {:Port => conf[:port], :Host => conf[:host]} ) { |server|
279:           trap "SIGINT", proc { server.stop }
280:         }
281:       when "webrick"
282:         puts "** Starting WEBrick on #{conf[:host]}:#{conf[:port]}"
283:         Rack::Handler::WEBrick.run( app, {:Port => conf[:port], :BindAddress => conf[:host]} ) { |server|
284:           trap "SIGINT", proc { server.shutdown }
285:         }
286:       end
287:     end

Public Class method: session()

Hash session

     # File lib/capcode.rb, line 157
157:         def session
158:           @env['rack.session']
159:         end