Module: Appcelerator::Dispatcher

Defined in:
lib/appcelerator/dispatcher.rb

Defined Under Namespace

Classes: Message

Class Method Summary collapse

Class Method Details

.dispatch_message(request, response, session, message_type, params, request_id) ⇒ Object

dispatch a message that was generated in some other way, like the upload_controller



17
18
19
20
21
22
23
# File 'lib/appcelerator/dispatcher.rb', line 17

def self.dispatch_message(request, response, session, message_type, params, request_id)
  msg = Message.new(request, session, message_type, params, request_id)
  
  message_queue = ServiceBroker.send(msg)
  
  serialize(message_queue, session.session_id, response.body)
end

.dispatch_request(request, response, session) ⇒ Object

dispatch a standard request, called by the service_broker_controller



6
7
8
9
10
11
12
13
# File 'lib/appcelerator/dispatcher.rb', line 6

def self.dispatch_request(request, response, session)
  message_queue = []
  extract_messages(request, session) do |in_message|
    out_messages = ServiceBroker.send(in_message)
    message_queue.concat(out_messages)
  end
  serialize(message_queue, session.session_id, response.body)
end

.extract_messages(request, session) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/appcelerator/dispatcher.rb', line 25

def self.extract_messages(request, session)
  body = request.env['RAW_POST_DATA']
  if body and body != ''
    node = REXML::Document.new(body)
    node.root.each_element('//message') do |message|
      
      request_id = message.attributes['requestid']
      message_type = message.attributes['type']
      params = JSON.parse(message.text)
      
      yield Message.new(request, session, message_type, params, request_id)
    end
  end
end

.serialize(message_queue, session_id, output) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/appcelerator/dispatcher.rb', line 40

def self.serialize(message_queue, session_id, output)
  output << '<?xml version="1.0"?>'
  output << "<messages version='1.0' sessionid='#{session_id}'>"
  
  message_queue.compact!
  message_queue.each do |msg|
    if msg.response_type
      output << "<message requestid='#{msg.request_id}' direction='OUTGOING' datatype='JSON' type='#{msg.response_type}'><![CDATA["
      output << msg.response.to_json
      output << ']]></message>'
    end
  end
  
  output << '</messages>'
end