Class: ExtDirect::Dispatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/ext_direct/dispatcher.rb

Class Method Summary collapse

Class Method Details

.dispatch(request, response) ⇒ Object

dispatch to a service method



4
5
6
7
8
9
10
11
12
13
14
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
# File 'lib/ext_direct/dispatcher.rb', line 4

def self.dispatch(request, response)
  params = request.data # the class to use
  action = request.action.underscore.to_sym # the method to execute
  method = request.method.underscore.to_sym # the arguments to be passed to the method
  tid    = request.tid # the transaction id to associate with this request
  
  begin
    data = {} # the response will be json encoded
    
    # the result of the method call
    result = ExtDirect::Service::Provider.execute(action, method, params)
    
    if request.event?
      data["type"] = "event"
      data["name"] = "message"
      data["data"] = result
    else
      data["type"]   = request.type
      data["action"] = request.action
      data["method"] = request.method
      data["tid"]    = tid
      data["result"] = result
    end
  rescue Exception => e
    # a exception on the server-side
    data["type"]    = "exception"
    data["message"] = e.message
    data["where"]   = e.backtrace
  ensure
    if request.rpc? and request.upload?
      response.write "<html><body><textarea>#{data.to_json}</textarea></body></html>"
    else
      response.write data.to_json
    end
  end
end