Module: MixinBot::API::Blaze

Included in:
MixinBot::API
Defined in:
lib/mixin_bot/api/blaze.rb

Instance Method Summary collapse

Instance Method Details

#blazeObject



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/mixin_bot/api/blaze.rb', line 6

def blaze
  access_token = access_token('GET', '/', '')

  authorization = format('Bearer %<access_token>s', access_token:)
  Faye::WebSocket::Client.new(
    format('wss://%<host>s/', host: config.blaze_host),
    ['Mixin-Blaze-1'],
    headers: { 'Authorization' => authorization },
    ping: 60
  )
end

#start_blaze_connect(reconnect: true, &_block) ⇒ Object



18
19
20
21
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/mixin_bot/api/blaze.rb', line 18

def start_blaze_connect(reconnect: true, &_block)
  ws ||= blaze
  yield if block_given?

  ws.on :open do |event|
    if defined? on_open
      on_open ws, event
    else
      p [Time.now.to_s, :open]
      ws.send list_pending_message
    end
  end

  ws.on :message do |event|
    if defined? on_message
      on_message ws, event
    else
      raw = JSON.parse ws_message(event.data)
      p [Time.now.to_s, :message, raw&.[]('action')]

      ws.send acknowledge_message_receipt(raw['data']['message_id']) unless raw&.[]('data')&.[]('message_id').nil?
    end
  end

  ws.on :error do |event|
    if defined? on_error
      on_error ws, event
    else
      p [Time.now.to_s, :error]
    end
  end

  ws.on :close do |event|
    if defined? on_close
      on_close ws, event
    else
      p [Time.now.to_s, :close, event.code, event.reason]
    end

    ws = nil
    start_blaze_connect(&_block) if reconnect
  end
end