Class: RHR::Server

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

Instance Method Summary collapse

Constructor Details

#initializeServer

Returns a new instance of Server.



6
7
8
9
10
# File 'lib/rhr/server.rb', line 6

def initialize
  @files = Dir["**/*"]
  @servable_files = @files - %w[Gemfile Gemfile.lock Rakefile helpers.rb]
  @servable_files -= @servable_files.grep(/(^|\/)_/)
end

Instance Method Details

#call(env) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rhr/server.rb', line 12

def call(env)
  path = env['PATH_INFO']
  if template = find_template(path)
    if renderer = Tilt[template]
      request = Rack::Request.new(env)
      params = request.GET.merge(request.POST)

      view = build_view
      body = renderer.new(template).render(view, :request => request, :params => params)

      if layout = find_layout
        body = renderer.new(layout).render(view, :request => request, :params => params) { body }
      end

      [200, {}, [body]]
    else
      Rack::File.new('.').call(env.merge('PATH_INFO' => template))
    end
  else
    [404, {}, ['404 File not found']]
  end
end