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.

Instance Method Summary collapse

Constructor Details

#initialize(processor) ⇒ ResponseReader

Initialize the reader.

Parameters:

  • processor (Processor)

    the processor object



12
13
14
# File 'lib/patient_http/response_reader.rb', line 12

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:



27
28
29
30
31
32
33
# File 'lib/patient_http/response_reader.rb', line 27

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