Class: EventMachine::MeshRuby
- Inherits:
-
Object
- Object
- EventMachine::MeshRuby
- Defined in:
- lib/meshruby.rb
Overview
Communicates with MeshBlu(tm) via websocket connection.
Instance Attribute Summary collapse
-
#queue ⇒ Object
Returns the value of attribute queue.
-
#socket ⇒ Object
Returns the value of attribute socket.
-
#token ⇒ Object
Returns the value of attribute token.
-
#url ⇒ Object
Returns the value of attribute url.
-
#uuid ⇒ Object
Returns the value of attribute uuid.
Instance Method Summary collapse
- #connect ⇒ Object
-
#create_socket_events ⇒ Object
Bootstraps all the events for MeshBlu in the correct order.
- #emit(devices, message_hash) ⇒ Object
-
#handle_message(message) ⇒ Object
Sanitize messages, making a best effort attempt to hashify them, otherwise returns message as-is.
-
#initialize(uuid, token, url = 'wss://meshblu.octoblu.com:443') ⇒ MeshRuby
constructor
A new instance of MeshRuby.
- #onmessage(&blk) ⇒ Object
Constructor Details
#initialize(uuid, token, url = 'wss://meshblu.octoblu.com:443') ⇒ MeshRuby
Returns a new instance of MeshRuby.
10 11 12 13 |
# File 'lib/meshruby.rb', line 10 def initialize(uuid, token, url = 'wss://meshblu.octoblu.com:443') @uuid, @token, @url, @queue = uuid, token, url, EventMachine::Queue.new @socket = SocketIO::Client::Simple.connect url end |
Instance Attribute Details
#queue ⇒ Object
Returns the value of attribute queue.
8 9 10 |
# File 'lib/meshruby.rb', line 8 def queue @queue end |
#socket ⇒ Object
Returns the value of attribute socket.
8 9 10 |
# File 'lib/meshruby.rb', line 8 def socket @socket end |
#token ⇒ Object
Returns the value of attribute token.
8 9 10 |
# File 'lib/meshruby.rb', line 8 def token @token end |
#url ⇒ Object
Returns the value of attribute url.
8 9 10 |
# File 'lib/meshruby.rb', line 8 def url @url end |
#uuid ⇒ Object
Returns the value of attribute uuid.
8 9 10 |
# File 'lib/meshruby.rb', line 8 def uuid @uuid end |
Instance Method Details
#connect ⇒ Object
15 16 17 18 |
# File 'lib/meshruby.rb', line 15 def connect socket.connect create_socket_events end |
#create_socket_events ⇒ Object
Bootstraps all the events for MeshBlu in the correct order.
21 22 23 24 25 26 27 28 29 |
# File 'lib/meshruby.rb', line 21 def create_socket_events #OTHER EVENTS: :identify, :identity, :ready, :disconnect, :message this = self socket.on :identify do |data| auth = {uuid: this.uuid, token: this.token, socketid: data['socketid']} emit :identity, auth end socket.on(:message) { |msg| this.(msg) } end |
#emit(devices, message_hash) ⇒ Object
39 40 41 |
# File 'lib/meshruby.rb', line 39 def emit(devices, ) socket.emit("message", devices: devices, message: ) end |
#handle_message(message) ⇒ Object
Sanitize messages, making a best effort attempt to hashify them, otherwise returns message as-is.
33 34 35 36 37 |
# File 'lib/meshruby.rb', line 33 def () @queue.push .is_a?(String) ? JSON.parse() : rescue JSON::ParserError @queue.push end |
#onmessage(&blk) ⇒ Object
43 44 45 |
# File 'lib/meshruby.rb', line 43 def (&blk) queue.pop(&blk) end |