Class: SpaceElevator::Client
- Inherits:
-
Object
- Object
- SpaceElevator::Client
- Defined in:
- lib/space_elevator.rb
Overview
Super-light utility for integrating with ActionCable-based backends.
Instance Attribute Summary collapse
-
#channel_handlers ⇒ Object
Returns the value of attribute channel_handlers.
-
#client ⇒ Object
Returns the value of attribute client.
-
#connected ⇒ Object
Returns the value of attribute connected.
-
#connection_handler ⇒ Object
Returns the value of attribute connection_handler.
-
#disconnect_handler ⇒ Object
Returns the value of attribute disconnect_handler.
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
- #connect(&block) ⇒ Object
- #create_publish_message(identifier, data) ⇒ Object
- #create_subscribe_message(identifier) ⇒ Object
-
#initialize(url, &disconnect_handler) ⇒ Client
constructor
A new instance of Client.
- #parse_message(message) ⇒ Object
- #publish(identifier, data) ⇒ Object
- #push(msg) ⇒ Object
- #subscribe(identifier, &block) ⇒ Object
Constructor Details
#initialize(url, &disconnect_handler) ⇒ Client
Returns a new instance of Client.
14 15 16 17 |
# File 'lib/space_elevator.rb', line 14 def initialize(url, &disconnect_handler) self.url = url self.disconnect_handler = disconnect_handler end |
Instance Attribute Details
#channel_handlers ⇒ Object
Returns the value of attribute channel_handlers.
10 11 12 |
# File 'lib/space_elevator.rb', line 10 def channel_handlers @channel_handlers end |
#client ⇒ Object
Returns the value of attribute client.
7 8 9 |
# File 'lib/space_elevator.rb', line 7 def client @client end |
#connected ⇒ Object
Returns the value of attribute connected.
8 9 10 |
# File 'lib/space_elevator.rb', line 8 def connected @connected end |
#connection_handler ⇒ Object
Returns the value of attribute connection_handler.
9 10 11 |
# File 'lib/space_elevator.rb', line 9 def connection_handler @connection_handler end |
#disconnect_handler ⇒ Object
Returns the value of attribute disconnect_handler.
11 12 13 |
# File 'lib/space_elevator.rb', line 11 def disconnect_handler @disconnect_handler end |
#url ⇒ Object
Returns the value of attribute url.
12 13 14 |
# File 'lib/space_elevator.rb', line 12 def url @url end |
Instance Method Details
#connect(&block) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/space_elevator.rb', line 19 def connect(&block) self.connection_handler = block self.channel_handlers = {} self.client = EventMachine::WebSocketClient.connect(url) client.callback do puts "Connected WebSocket to #{url}." self.connected = true end client.stream do |raw| = (raw.to_s) if ['identifier'] channel_handlers[['identifier'].to_json].call() else connection_handler.call() end end client.disconnect do self.connected = false disconnect_handler.call end end |
#create_publish_message(identifier, data) ⇒ Object
56 57 58 59 60 61 62 63 64 |
# File 'lib/space_elevator.rb', line 56 def (identifier, data) = { command: 'message', identifier: identifier } [:data] = data.to_json if data [:identifier] = [:identifier].to_json .to_json end |
#create_subscribe_message(identifier) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/space_elevator.rb', line 66 def (identifier) = { command: 'subscribe', identifier: identifier.to_json } # message[:identifier].merge!(data) if data # message[:data] = data.to_json if data # message[:identifier] = message[:identifier].to_json # puts message .to_json end |
#parse_message(message) ⇒ Object
78 79 80 81 82 83 |
# File 'lib/space_elevator.rb', line 78 def () result = JSON.parse() result['identifier'] = JSON.parse(result['identifier']) if result['identifier'] result['data'] = JSON.parse(result['data']) if result['data'] result end |
#publish(identifier, data) ⇒ Object
50 51 52 53 54 |
# File 'lib/space_elevator.rb', line 50 def publish(identifier, data) msg = (identifier, data) # puts "PUSHING: #{msg}" push(msg) end |
#push(msg) ⇒ Object
46 47 48 |
# File 'lib/space_elevator.rb', line 46 def push(msg) client.send_msg(msg) end |
#subscribe(identifier, &block) ⇒ Object
41 42 43 44 |
# File 'lib/space_elevator.rb', line 41 def subscribe(identifier, &block) push((identifier)) channel_handlers[identifier.to_json] = block end |