Class: KabustationClient::PushClient

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(callbacks = {}) ⇒ PushClient

Returns a new instance of PushClient.



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/kabustation_client/push_client.rb', line 9

def initialize(callbacks = {})
  @callbacks = {
    open: -> {},
    message: -> (_board) {},
    error: -> (_e) {},
    close: -> (_e) {}
  }.merge(callbacks)

  config = Configuration.default

  @uri = "ws://#{config.host}#{config.base_path}/websocket"
end

Instance Attribute Details

#wsObject (readonly)

Returns the value of attribute ws.



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

def ws
  @ws
end

Instance Method Details

#closeObject



49
50
51
# File 'lib/kabustation_client/push_client.rb', line 49

def close
  @ws.close
end

#connectObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/kabustation_client/push_client.rb', line 22

def connect
  callbacks = @callbacks

  @ws = WebSocket::Client::Simple.connect @uri do |ws|
    ws.on :open do
      callbacks[:open].call
    end

    ws.on :message do |msg|
      data = msg.data.force_encoding("ASCII-8BIT").force_encoding('UTF-8')
      json = JSON.parse(data)
      board = KabustationClient::ApiClient.new.convert_to_type(json, 'BoardSuccess')

      callbacks[:message].call(board)
    end

    ws.on :error do |e|
      callbacks[:error].call(e)
    end

    ws.on :close do |e|
      callbacks[:close].call(e)
    end
  end
end