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.



27
28
29
# File 'lib/docter/server.rb', line 27

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

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection.



25
26
27
# File 'lib/docter/server.rb', line 25

def collection
  @collection
end

#optionsObject (readonly)

Returns the value of attribute options.



25
26
27
# File 'lib/docter/server.rb', line 25

def options
  @options
end

#templateObject (readonly)

Returns the value of attribute template.



25
26
27
# File 'lib/docter/server.rb', line 25

def template
  @template
end

Instance Method Details

#process(request, response) ⇒ Object



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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/docter/server.rb', line 35

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



31
32
33
# File 'lib/docter/server.rb', line 31

def resources
  @resources ||= Resources.new
end