Class: TCR::RecordableTCPSocket

Inherits:
Object
  • Object
show all
Defined in:
lib/tcr/recordable_tcp_socket.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(address, port, cassette) ⇒ RecordableTCPSocket

Returns a new instance of RecordableTCPSocket.



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/tcr/recordable_tcp_socket.rb', line 6

def initialize(address, port, cassette)
  raise TCR::NoCassetteError.new unless TCR.cassette

  if cassette.recording?
    @live = true
    @socket = TCPSocket.real_open(address, port)
    @recording = []
  else
    @live = false
    @recording = cassette.next_session
  end
  @cassette = cassette
end

Instance Attribute Details

#cassetteObject (readonly)

Returns the value of attribute cassette.



3
4
5
# File 'lib/tcr/recordable_tcp_socket.rb', line 3

def cassette
  @cassette
end

#liveObject (readonly)

Returns the value of attribute live.



3
4
5
# File 'lib/tcr/recordable_tcp_socket.rb', line 3

def live
  @live
end

#recordingObject

Returns the value of attribute recording.



4
5
6
# File 'lib/tcr/recordable_tcp_socket.rb', line 4

def recording
  @recording
end

Instance Method Details

#closeObject



59
60
61
62
63
64
# File 'lib/tcr/recordable_tcp_socket.rb', line 59

def close
  if live
    @socket.close
    cassette.append(recording)
  end
end

#closed?Boolean

Returns:

  • (Boolean)


51
52
53
54
55
56
57
# File 'lib/tcr/recordable_tcp_socket.rb', line 51

def closed?
  if live
    @socket.closed?
  else
    false
  end
end

#read_nonblock(bytes) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/tcr/recordable_tcp_socket.rb', line 20

def read_nonblock(bytes)
  if live
    data = @socket.read_nonblock(bytes)
    recording << ["read", data]
  else
    direction, data = recording.shift
    raise TCR::DirectionMismatchError.new("Expected to 'read' but next in recording was 'write'") unless direction == "read"
  end

  data
end

#to_ioObject



45
46
47
48
49
# File 'lib/tcr/recordable_tcp_socket.rb', line 45

def to_io
  if live
    @socket.to_io
  end
end

#write(str) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/tcr/recordable_tcp_socket.rb', line 32

def write(str)
  if live
    len = @socket.write(str)
    recording << ["write", str]
  else
    direction, data = recording.shift
    raise TCR::DirectionMismatchError.new("Expected to 'write' but next in recording was 'read'") unless direction == "write"
    len = data.length
  end

  len
end