Class: SSE::Impl::StreamingHTTPConnection
- Inherits:
-
Object
- Object
- SSE::Impl::StreamingHTTPConnection
- 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
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Instance Method Summary collapse
-
#close ⇒ Object
Closes the connection.
-
#initialize(uri, proxy: nil, headers: {}, connect_timeout: nil, read_timeout: nil) ⇒ StreamingHTTPConnection
constructor
Opens a new connection.
-
#read_all ⇒ String
Consumes the entire response body and returns it.
-
#read_lines ⇒ Object
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.
Constructor Details
#initialize(uri, proxy: nil, headers: {}, connect_timeout: nil, read_timeout: nil) ⇒ StreamingHTTPConnection
Opens a new connection.
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
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
14 15 16 |
# File 'lib/ld-eventsource/impl/streaming_http.rb', line 14 def headers @headers end |
#status ⇒ Object (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
#close ⇒ Object
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_all ⇒ String
Consumes the entire response body and returns it.
57 58 59 |
# File 'lib/ld-eventsource/impl/streaming_http.rb', line 57 def read_all @reader.read_all end |
#read_lines ⇒ Object
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 |