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.
7 8 9 |
# File 'lib/docter/server.rb', line 7 def initialize(collection, template, = {}) @collection, @template, @options = collection, template, || {} end |
Instance Attribute Details
#collection ⇒ Object (readonly)
Returns the value of attribute collection.
5 6 7 |
# File 'lib/docter/server.rb', line 5 def collection @collection end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
5 6 7 |
# File 'lib/docter/server.rb', line 5 def @options end |
#template ⇒ Object (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 [: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
11 12 13 |
# File 'lib/docter/server.rb', line 11 def resources @resources ||= Resources.new end |