Class: Garufa::Connection

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(socket, logger) ⇒ Connection

Returns a new instance of Connection.



12
13
14
15
16
17
# File 'lib/garufa/connection.rb', line 12

def initialize(socket, logger)
  @socket = socket
  @logger = logger
  @socket_id = build_socket_id
  @subscriptions = {}
end

Instance Attribute Details

#socket_idObject (readonly)

Returns the value of attribute socket_id.



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

def socket_id
  @socket_id
end

Instance Method Details

#cleanupObject



56
57
58
59
# File 'lib/garufa/connection.rb', line 56

def cleanup
  @subscriptions.each { |_, subscription| unsubscribe(subscription) }
  @subscriptions.clear
end

#closeObject



52
53
54
# File 'lib/garufa/connection.rb', line 52

def close
  @socket.close
end

#error(code, message) ⇒ Object



42
43
44
# File 'lib/garufa/connection.rb', line 42

def error(code, message)
  send_message Message.error(code, message)
end

#establishObject



19
20
21
22
23
24
25
26
# File 'lib/garufa/connection.rb', line 19

def establish
  if valid_app_key?
    send_message Message.connection_established(@socket_id)
  else
    error(4001, "Could not find app by key #{app_key}")
    close
  end
end

#handle_incomming_data(data) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/garufa/connection.rb', line 28

def handle_incomming_data(data)
  @logger.debug "Incomming message. #{@socket_id}: #{data}"

  message = Message.new(JSON.parse(data))
  event, data = message.event, message.data

  case event
  when /^pusher:/
    handle_pusher_event(event, data)
  when /^client-/
    handle_client_event(event, data, message.channel)
  end
end

#send_message(message) ⇒ Object



46
47
48
49
50
# File 'lib/garufa/connection.rb', line 46

def send_message(message)
  @logger.debug "Outgoing message. #{@socket_id}: #{message.to_json}"

  @socket.send message.to_json
end