Class: SSE::StreamingHTTPConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/sse_client/streaming_http.rb

Overview

Wrapper around a socket providing a simplified HTTP request-response cycle including streaming. The socket is created and managed by Socketry, which we use so that we can have a read timeout.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, proxy, headers, connect_timeout, read_timeout) ⇒ StreamingHTTPConnection



13
14
15
16
17
18
19
20
# File 'lib/sse_client/streaming_http.rb', line 13

def initialize(uri, proxy, headers, connect_timeout, read_timeout)
  @socket = HTTPConnectionFactory.connect(uri, proxy, connect_timeout, read_timeout)
  @socket.write(build_request(uri, headers))
  @reader = HTTPResponseReader.new(@socket, read_timeout)
  @status = @reader.status
  @headers = @reader.headers
  @closed = Concurrent::AtomicBoolean.new(false)
end

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



11
12
13
# File 'lib/sse_client/streaming_http.rb', line 11

def headers
  @headers
end

#statusObject (readonly)

Returns the value of attribute status.



11
12
13
# File 'lib/sse_client/streaming_http.rb', line 11

def status
  @status
end

Instance Method Details

#closeObject



22
23
24
25
26
27
# File 'lib/sse_client/streaming_http.rb', line 22

def close
  if @closed.make_true
    @socket.close if @socket
    @socket = nil
  end
end

#read_allObject

Consumes the entire response body and returns it.



36
37
38
# File 'lib/sse_client/streaming_http.rb', line 36

def read_all
  @reader.read_all
end

#read_linesObject

Generator that returns one line of the response body at a time (delimited by r, n, or rn) until the response is fully consumed or the socket is closed.



31
32
33
# File 'lib/sse_client/streaming_http.rb', line 31

def read_lines
  @reader.read_lines
end