Class: NotepadqqApi::MessageInterpreter
- Inherits:
-
Object
- Object
- NotepadqqApi::MessageInterpreter
- Defined in:
- lib/notepadqq_api/message_interpreter.rb
Instance Method Summary collapse
-
#initialize(message_channel) ⇒ MessageInterpreter
constructor
A new instance of MessageInterpreter.
-
#invoke_api(object_id, method, args) ⇒ Object
Calls a method on the remote object object_id.
- #process_message(message) ⇒ Object
-
#register_event_handler(object_id, event, callback) ⇒ Object
Assign an event of a particular object_id to a callback.
Constructor Details
#initialize(message_channel) ⇒ MessageInterpreter
4 5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/notepadqq_api/message_interpreter.rb', line 4 def initialize() = # Hash of event handlers, for example # { # 1: { # "newWindow": [<callback1>, ..., <callbackn>] # }, # ... # } # Where 1 is an object_id and "newWindow" is an event of that object @event_handlers = {} end |
Instance Method Details
#invoke_api(object_id, method, args) ⇒ Object
Calls a method on the remote object object_id
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/notepadqq_api/message_interpreter.rb', line 29 def invoke_api(object_id, method, args) = { :objectId => object_id, :method => method, :args => args } .() reply = . result = [reply["result"]] convert_stubs!(result) result = result[0] if reply["err"] != MessageInterpreterError::ErrorCode::NONE error = MessageInterpreterError.new(reply["err"], reply["errStr"]) raise error, error.description end return result end |
#process_message(message) ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/notepadqq_api/message_interpreter.rb', line 51 def () if .has_key?("event") () elsif .has_key?("result") # We shouldn't have received it here... ignore it end end |
#register_event_handler(object_id, event, callback) ⇒ Object
Assign an event of a particular object_id to a callback
19 20 21 22 23 24 25 26 |
# File 'lib/notepadqq_api/message_interpreter.rb', line 19 def register_event_handler(object_id, event, callback) event = event.to_sym @event_handlers[object_id] ||= {} @event_handlers[object_id][event] ||= [] @event_handlers[object_id][event].push(callback) end |