Class: Docter::MongrelHandler

Inherits:
Mongrel::HttpHandler
  • Object
show all
Defined in:
lib/docter/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection, template, options = {}) ⇒ MongrelHandler

Returns a new instance of MongrelHandler.



7
8
9
# File 'lib/docter/server.rb', line 7

def initialize(collection, template, options = {})
  @collection, @template, @options = collection, template, options || {}
end

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection.



5
6
7
# File 'lib/docter/server.rb', line 5

def collection
  @collection
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/docter/server.rb', line 5

def options
  @options
end

#templateObject (readonly)

Returns the value of attribute template.



5
6
7
# File 'lib/docter/server.rb', line 5

def template
  @template
end

Instance Method Details

#process(request, response) ⇒ Object



15
16
17
18
19
20
21
22
23
24
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
# File 'lib/docter/server.rb', line 15

def process(request, response)
  # Absolute path to relative path, default index.
  path = request.params[Mongrel::Const::PATH_INFO].sub(/^\//, '')
  path = 'index.html' if path.empty?

  begin
    if file = template.find(path)
      # Files served directly from disk (CSS, images, RDoc, etc).
      since = request.params[Mongrel::Const::HTTP_IF_MODIFIED_SINCE]
      if since && Time.parse(since) >= File.stat(file).mtime
        response.status = 304
        response.finished
      else
        puts "Serving #{path}" if verbose
        response.start(200) do |head,out|
          head['Last-Modified'] = CGI.rfc1123_date(File.stat(file).mtime)
          out.write File.read(file)
        end
      end
    elsif options[:one_page] && path == 'index.html'
      puts "Serving #{path}" if verbose
      response.start(200) do |head,out|
        head['Content-Type'] = 'text/html'
        out.write collection.render(template, options)
      end
    elsif page = collection.page(path)
      puts "Serving #{path}" if verbose
      response.start(200) do |head,out|
        head['Content-Type'] = 'text/html'
        out.write collection.render(template, page, options)
      end
    else
      response.start 404 do |head, out|
        head['Content-Type'] = 'text/html'
        out.write "<h1>Did you accidentally rm #{path}, or did you forget to :w it?</h1>"
      end
    end
  rescue Exception=>error
    response.start(500) do |head, out|
      head['Content-Type'] = 'text/plain'
      error = ["#{error.class}: #{error}", error.backtrace.join('\n')]
      out.puts *error
      puts *error
    end
  end
end

#resourcesObject



11
12
13
# File 'lib/docter/server.rb', line 11

def resources
  @resources ||= Resources.new
end