Class Rddb::Server::DocumentHandler
In: lib/rddb/server/document_handler.rb
Parent: Mongrel::HttpHandler

DocumentHandler is a mongrel HTTP handler that is used for retrieving and storing documents via a REST interface. Note that only the GET and POST methods are implemented.

Methods

delete   get   new   post   process   put  

Attributes

document_store  [RW]  The underlying document store

Public Class methods

Initialize the handler with the given document store.

[Source]

    # File lib/rddb/server/document_handler.rb, line 12
12:       def initialize(document_store)
13:         @document_store = document_store
14:       end

Public Instance methods

Process the HTTP request

[Source]

    # File lib/rddb/server/document_handler.rb, line 17
17:       def process(request, response)
18:         #puts "Received #{request.method} request of type #{request.content_type} for #{request.host}:#{request.port}#{request.path}"
19:         case request.method
20:         when 'GET'
21:           get(request, response)
22:         when 'POST'
23:           post(request, response)
24:         when 'PUT'
25:           put(request, response)
26:         when 'DELETE'
27:           delete(request, response)
28:         else
29:           response.start(405) do |head, out|
30:             out.write("Unsupported method: #{request.method}.\n")
31:           end
32:         end
33:       end

Protected Instance methods

Implementation of the DELETE method (will always return 403).

[Source]

    # File lib/rddb/server/document_handler.rb, line 90
90:       def delete(request, response)
91:         begin
92:            response.start(403) do |head, out|
93:             out.write("The DELETE method is not allowed.\n")
94:           end
95:         rescue => e
96:           server_error(e, request, response)
97:         end
98:       end

Implementation of the GET method.

[Source]

    # File lib/rddb/server/document_handler.rb, line 37
37:       def get(request, response)
38:         document_id = request.path[0..-1]
39:         puts "Getting #{document_id}"
40:         begin
41:           if document_id.nil? || document_id.empty?
42:             response.start(403) do |head, out|
43:               out.write("The root path is not allowed.\n")
44:               return
45:             end
46:           end
47:           document = document_store.find(document_id)
48:           if document
49:             puts "Found document, returning 200 with document data"
50:             response.start(200) do |head, out|
51:               head['Content-Type'] = 'application/x-url-form-encoded'
52:               out.write(document.to_wire)
53:             end
54:           else
55:             response.start(404) do |head, out|
56:               out.write("The document #{document_id} was not found.\n")
57:             end
58:           end
59:         rescue => e
60:           server_error(e, request, response)
61:         end
62:       end

Implementation of the POST method.

[Source]

    # File lib/rddb/server/document_handler.rb, line 65
65:       def post(request, response)
66:         document = to_document(request.body.read)
67:         begin
68:           document = document_store.store(document)
69:           response.start(201) do |head, out|
70:             head['Location'] = "/documents/#{document.id}"
71:             out.write("The document was created.\n")
72:           end
73:         rescue => e
74:           server_error(e, request, response)
75:         end
76:       end

Implementation of the PUT method (will always return 403).

[Source]

    # File lib/rddb/server/document_handler.rb, line 79
79:       def put(request, response)
80:         begin
81:           response.start(403) do |head, out|
82:             out.write("The PUT method is not allowed.\n")
83:           end
84:         rescue => e
85:           server_error(e, request, response)
86:         end
87:       end

[Validate]