Class: WebSocketVCR::RecordableWebsocketClient

Inherits:
Object
  • Object
show all
Includes:
EventEmitter
Defined in:
lib/simple_websocket_vcr/recordable_websocket_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cassette, real_client) ⇒ RecordableWebsocketClient

Returns a new instance of RecordableWebsocketClient.



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/simple_websocket_vcr/recordable_websocket_client.rb', line 15

def initialize(cassette, real_client)
  fail NoCassetteError 'specify the cassette' unless cassette

  if cassette.recording?
    @live = true
    @client = real_client
  else
    @live = false
    @open = true
  end
  @session = cassette.next_session
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



12
13
14
# File 'lib/simple_websocket_vcr/recordable_websocket_client.rb', line 12

def client
  @client
end

#liveObject (readonly)

Returns the value of attribute live.



12
13
14
# File 'lib/simple_websocket_vcr/recordable_websocket_client.rb', line 12

def live
  @live
end

#openObject

Returns the value of attribute open.



13
14
15
# File 'lib/simple_websocket_vcr/recordable_websocket_client.rb', line 13

def open
  @open
end

#sessionObject

Returns the value of attribute session.



13
14
15
# File 'lib/simple_websocket_vcr/recordable_websocket_client.rb', line 13

def session
  @session
end

#threadObject

Returns the value of attribute thread.



13
14
15
# File 'lib/simple_websocket_vcr/recordable_websocket_client.rb', line 13

def thread
  @thread
end

Instance Method Details

#closeObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/simple_websocket_vcr/recordable_websocket_client.rb', line 50

def close
  if @live
    @session.store(operation: 'write', event: 'close')
    @client.close
  else
    sleep 0.5 if @session.head && @session.head.event != 'close'
    @session.record_entries.size.times do
      record = @session.next
      _ensure_event('close', record.event)
      emit(record.event, record.data) if record.operation == 'read'
    end
    Thread.kill @thread if @thread
    @open = false
  end
end

#emit(event, *data) ⇒ Object



37
38
39
40
# File 'lib/simple_websocket_vcr/recordable_websocket_client.rb', line 37

def emit(event, *data)
  @session.store(operation: 'read', event: event, data: data) if @live
  super(event, *data)
end

#on(event, params = {}, &block) ⇒ Object



32
33
34
35
# File 'lib/simple_websocket_vcr/recordable_websocket_client.rb', line 32

def on(event, params = {}, &block)
  super(event, params, &block) unless @live
  _read(event, params, &block)
end

#open?Boolean

Returns:

  • (Boolean)


42
43
44
45
46
47
48
# File 'lib/simple_websocket_vcr/recordable_websocket_client.rb', line 42

def open?
  if @live
    @client.open?
  else
    @open
  end
end

#send(data, opt = { type: :text }) ⇒ Object



28
29
30
# File 'lib/simple_websocket_vcr/recordable_websocket_client.rb', line 28

def send(data, opt = { type: :text })
  _write(:send, data, opt)
end