Class: SSE::Impl::StreamingHTTPConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/ld-eventsource/impl/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: nil, headers: {}, connect_timeout: nil, read_timeout: nil) ⇒ StreamingHTTPConnection

Opens a new connection.

Parameters:

  • uri (String)

    the URI to connect o

  • proxy (String) (defaults to: nil)

    the proxy server URI, if any

  • headers (Hash) (defaults to: {})

    request headers

  • connect_timeout (Float) (defaults to: nil)

    connection timeout

  • read_timeout (Float) (defaults to: nil)

    read timeout



25
26
27
28
29
30
31
32
# File 'lib/ld-eventsource/impl/streaming_http.rb', line 25

def initialize(uri, proxy: nil, headers: {}, connect_timeout: nil, read_timeout: nil)
  @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.



14
15
16
# File 'lib/ld-eventsource/impl/streaming_http.rb', line 14

def headers
  @headers
end

#statusObject (readonly)

Returns the value of attribute status.



14
15
16
# File 'lib/ld-eventsource/impl/streaming_http.rb', line 14

def status
  @status
end

Instance Method Details

#closeObject

Closes the connection.



37
38
39
40
41
42
# File 'lib/ld-eventsource/impl/streaming_http.rb', line 37

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

#read_allString

Consumes the entire response body and returns it.

Returns:

  • (String)

    the response body



57
58
59
# File 'lib/ld-eventsource/impl/streaming_http.rb', line 57

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.



48
49
50
# File 'lib/ld-eventsource/impl/streaming_http.rb', line 48

def read_lines
  @reader.read_lines
end