Class: PatientHttp::ResponseReader

Inherits:
Object
  • Object
show all
Defined in:
lib/patient_http/response_reader.rb

Overview

Reads and validates HTTP response bodies.

Encapsulates the logic for reading async HTTP responses with size validation and building Response objects from the raw response data.

Defined Under Namespace

Classes: ReadAbortedError

Instance Method Summary collapse

Constructor Details

#initialize(processor) ⇒ ResponseReader

Initialize the reader.

Parameters:

  • processor (Processor)

    the processor object



19
20
21
# File 'lib/patient_http/response_reader.rb', line 19

def initialize(processor)
  @processor = processor
end

Instance Method Details

#read_body(async_response, headers_hash) ⇒ String?

Read the response body with size validation.

Reads the async HTTP response body asynchronously to completion, which allows the connection to be reused. The async-http client handles connection pooling and keep-alive internally. Using iteration instead of read() ensures non-blocking I/O that yields to the reactor.

Parameters:

  • async_response (Async::HTTP::Protocol::Response)

    the async HTTP response

  • headers_hash (Hash)

    the response headers

Returns:

  • (String, nil)

    the response body or nil if no body present

Raises:



35
36
37
38
39
40
41
# File 'lib/patient_http/response_reader.rb', line 35

def read_body(async_response, headers_hash)
  return nil unless async_response.body

  validate_content_length(headers_hash)
  body = read_body_chunks(async_response)
  apply_charset_encoding(body, headers_hash)
end