Class: Tinker::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(socket) ⇒ Client

Returns a new instance of Client.



4
5
6
7
8
# File 'lib/tinker/client.rb', line 4

def initialize(socket)
  @socket = socket
  @id = SecureRandom.uuid
  @contexts = Set.new
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



2
3
4
# File 'lib/tinker/client.rb', line 2

def id
  @id
end

#socketObject (readonly)

Returns the value of attribute socket.



2
3
4
# File 'lib/tinker/client.rb', line 2

def socket
  @socket
end

Instance Method Details

#disconnectObject



20
21
22
23
24
# File 'lib/tinker/client.rb', line 20

def disconnect
  @contexts.each do |context|
    context.remove_client(self)
  end
end

#join(context) ⇒ Object



10
11
12
13
# File 'lib/tinker/client.rb', line 10

def join(context)
  @contexts.add context
  send(action: 'meta.context.join', context: context.id, params: { type: context.class })
end

#leave(context) ⇒ Object



15
16
17
18
# File 'lib/tinker/client.rb', line 15

def leave(context)
  @contexts.delete context
  send(action: 'meta.context.leave', context: context.id, params: { type: context.class })
end

#send(params) ⇒ Object



32
33
34
# File 'lib/tinker/client.rb', line 32

def send(params)
  socket.send(params)
end

#to_json(*a) ⇒ Object



26
27
28
29
30
# File 'lib/tinker/client.rb', line 26

def to_json(*a)
  {
    id: id
  }.to_json(*a)
end