Class: SpaceElevator::Client

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

Overview

Super-light utility for integrating with ActionCable-based backends.

Instance Attribute Summary collapse

Instance Method Summary collapse

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_handlersObject

Returns the value of attribute channel_handlers.



10
11
12
# File 'lib/space_elevator.rb', line 10

def channel_handlers
  @channel_handlers
end

#clientObject

Returns the value of attribute client.



7
8
9
# File 'lib/space_elevator.rb', line 7

def client
  @client
end

#connectedObject

Returns the value of attribute connected.



8
9
10
# File 'lib/space_elevator.rb', line 8

def connected
  @connected
end

#connection_handlerObject

Returns the value of attribute connection_handler.



9
10
11
# File 'lib/space_elevator.rb', line 9

def connection_handler
  @connection_handler
end

#disconnect_handlerObject

Returns the value of attribute disconnect_handler.



11
12
13
# File 'lib/space_elevator.rb', line 11

def disconnect_handler
  @disconnect_handler
end

#urlObject

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|
       message = parse_message(raw.to_s)
       if message['identifier']
           channel_handlers[message['identifier'].to_json].call(message)
       else
           connection_handler.call(message)
       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 create_publish_message(identifier, data)
    message = {
        command: 'message',
        identifier: identifier
    }
    message[:data] = data.to_json if data
    message[:identifier] = message[:identifier].to_json
    message.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 create_subscribe_message(identifier)
    message = {
        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
    message.to_json
end

#parse_message(message) ⇒ Object



78
79
80
81
82
83
# File 'lib/space_elevator.rb', line 78

def parse_message(message)
    result = JSON.parse(message)
    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 = create_publish_message(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(create_subscribe_message(identifier))
    channel_handlers[identifier.to_json] = block
end