Class: Rambo::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/rambo/server.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Server

Returns a new instance of Server.



16
17
18
19
20
21
22
23
# File 'lib/rambo/server.rb', line 16

def initialize(options = {})
  @options = options
  env = Rambo::Env.new
  @contexts = {
    'default' => Rambo::ApplicationContext.new(),
    #'blog' => Rambo::ApplicationContext.new('blog')
  }
end

Instance Method Details

#call(env) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rambo/server.rb', line 25

def call(env)
  begin
    
    @contexts.each { |key, context| context.reload } if Rambo::Env.config
    
    request = Request.new(env)
    response = Response.new
    
    if @contexts.keys.include? request.controller.downcase
      current_context = @contexts[request.controller.downcase]
      request = Rambo::ApplicationRequest.new(env)
    end
    current_context ||= @contexts['default']
    request.application_context = current_context
    
    #puts "rambo: looking for #{request.controller_class}"
    
    begin
      controller = Object.module_eval("::#{request.controller_class}", __FILE__, __LINE__).new
    rescue Exception => e
      return [404, response.header, "<h2>Routing error: controller <span style='color:grey'>#{request.controller}</span> not found</h2>"]
    end
    controller.request = request
    controller.response = response
    
    controller.init if controller.respond_to? :init 
    
    unless controller.respond_to? request.action
      return [404, response.header, "<h2>Routing error: action <span style='color:grey'>#{request.action}</span> not found in <span style='color:grey'>#{request.controller}</span></h2>"]
    end
    
    result = controller.send(request.action) unless controller.already_rendered?
    
    response.body = result if result
    
    [response.status, response.header, response.body]
  rescue Exception => e
    puts e.message
    return [500, {}, "<pre><b>#{e.message.gsub("<","&lt;")}</b>\n#{e.backtrace.join("\n")}</pre>"]
  end
end