Class: Connect::StreamResponse

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/connect/stream_response.rb

Constant Summary collapse

END_OF_STREAM_FLAG =
0b00000010

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(header:, enumerator:) ⇒ StreamResponse

Returns a new instance of StreamResponse.



11
12
13
14
15
16
17
# File 'lib/connect/stream_response.rb', line 11

def initialize(header:, enumerator:)
  @header = header
  @enumerator = enumerator
  @trailer = nil
  @error = nil
  @stream_exhausted = false
end

Instance Attribute Details

#errorObject

Raises:



37
38
39
40
41
# File 'lib/connect/stream_response.rb', line 37

def error
  raise StreamReadError, "Cannot read error before stream is exhausted" unless stream_exhausted?

  @error
end

#headerObject (readonly)

Returns the value of attribute header.



9
10
11
# File 'lib/connect/stream_response.rb', line 9

def header
  @header
end

#trailerObject

Raises:



31
32
33
34
35
# File 'lib/connect/stream_response.rb', line 31

def trailer
  raise StreamReadError, "Cannot read trailer before stream is exhausted" unless stream_exhausted?

  @trailer
end

Instance Method Details

#each(&block) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/connect/stream_response.rb', line 19

def each(&block)
  enumerator.each do |flags, message|
    if flags & END_OF_STREAM_FLAG == 0
      yield message
    else
      read_end_stream_message(message)
    end
  end

  raise InvalidStreamResponseError, "Stream did not end with end stream message" unless stream_exhausted?
end

#stream_exhausted?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/connect/stream_response.rb', line 43

def stream_exhausted?
  @stream_exhausted
end