Class: Docter::MongrelHandler
- Inherits:
-
Mongrel::HttpHandler
- Object
- Mongrel::HttpHandler
- Docter::MongrelHandler
- Defined in:
- lib/docter/server.rb
Instance Attribute Summary collapse
-
#collection ⇒ Object
readonly
Returns the value of attribute collection.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#template ⇒ Object
readonly
Returns the value of attribute template.
Instance Method Summary collapse
-
#initialize(collection, template, options = {}) ⇒ MongrelHandler
constructor
A new instance of MongrelHandler.
- #process(request, response) ⇒ Object
- #resources ⇒ Object
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, = {}) @collection, @template, @options = collection, template, || {} end |
Instance Attribute Details
#collection ⇒ Object (readonly)
Returns the value of attribute collection.
25 26 27 |
# File 'lib/docter/server.rb', line 25 def collection @collection end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
25 26 27 |
# File 'lib/docter/server.rb', line 25 def @options end |
#template ⇒ Object (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 [: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, ) 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, ) 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 |
#resources ⇒ Object
31 32 33 |
# File 'lib/docter/server.rb', line 31 def resources @resources ||= Resources.new end |