Class: Philiprehberger::HttpClient::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/philiprehberger/http_client/response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status:, body:, headers: {}, streaming: false, request_id: nil) ⇒ Response

Returns a new instance of Response.

Parameters:

  • status (Integer)

    HTTP status code

  • body (String, nil)

    Response body

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

    Response headers

  • streaming (Boolean) (defaults to: false)

    Whether the response was streamed

  • request_id (String, nil) (defaults to: nil)

    Request ID for tracking



15
16
17
18
19
20
21
22
23
# File 'lib/philiprehberger/http_client/response.rb', line 15

def initialize(status:, body:, headers: {}, streaming: false, request_id: nil)
  @status = status
  @body = body
  @headers = headers
  @streaming = streaming
  @request_id = request_id
  @metrics = nil
  @redirects = []
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



8
9
10
# File 'lib/philiprehberger/http_client/response.rb', line 8

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



8
9
10
# File 'lib/philiprehberger/http_client/response.rb', line 8

def headers
  @headers
end

#metricsMetrics? (readonly)

Returns request timing metrics (nil if not available).

Returns:



50
51
52
# File 'lib/philiprehberger/http_client/response.rb', line 50

def metrics
  @metrics
end

#redirectsArray<String> (readonly)

Returns the redirect chain (empty if no redirects occurred).

Returns:

  • (Array<String>)


55
56
57
# File 'lib/philiprehberger/http_client/response.rb', line 55

def redirects
  @redirects
end

#request_idObject (readonly)

Returns the value of attribute request_id.



8
9
10
# File 'lib/philiprehberger/http_client/response.rb', line 8

def request_id
  @request_id
end

#statusObject (readonly)

Returns the value of attribute status.



8
9
10
# File 'lib/philiprehberger/http_client/response.rb', line 8

def status
  @status
end

Instance Method Details

#jsonHash, Array

Parses the response body as JSON.

Returns:

  • (Hash, Array)

    Parsed JSON

Raises:

  • (JSON::ParserError)

    If the body is not valid JSON



43
44
45
# File 'lib/philiprehberger/http_client/response.rb', line 43

def json
  @json ||= JSON.parse(body)
end

#ok?Boolean

Returns true if the status code is in the 2xx range.

Returns:

  • (Boolean)


28
29
30
# File 'lib/philiprehberger/http_client/response.rb', line 28

def ok?
  status >= 200 && status < 300
end

#redirected?Boolean

Returns true if the response was redirected.

Returns:

  • (Boolean)


60
61
62
# File 'lib/philiprehberger/http_client/response.rb', line 60

def redirected?
  !redirects.empty?
end

#streaming?Boolean

Returns true if the response was streamed.

Returns:

  • (Boolean)


35
36
37
# File 'lib/philiprehberger/http_client/response.rb', line 35

def streaming?
  @streaming
end