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:



78
79
80
# File 'lib/philiprehberger/http_client/response.rb', line 78

def metrics
  @metrics
end

#redirectsArray<String> (readonly)

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

Returns:

  • (Array<String>)


83
84
85
# File 'lib/philiprehberger/http_client/response.rb', line 83

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

#client_error?Boolean

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

Returns:

  • (Boolean)


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

def client_error?
  status >= 400 && status < 500
end

#jsonHash, Array

Parses the response body as JSON.

Returns:

  • (Hash, Array)

    Parsed JSON

Raises:

  • (JSON::ParserError)

    If the body is not valid JSON



57
58
59
# File 'lib/philiprehberger/http_client/response.rb', line 57

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

#json?Boolean

Returns true if the ‘Content-Type` response header advertises JSON. Matches `application/json`, `application/problem+json`, and any other `+json` structured-syntax suffix defined by RFC 6838. Header lookup is case-insensitive.

Returns:

  • (Boolean)


67
68
69
70
71
72
73
# File 'lib/philiprehberger/http_client/response.rb', line 67

def json?
  header = headers.find { |k, _| k.to_s.downcase == 'content-type' }
  return false unless header

  value = header[1].to_s.downcase.split(';').first.to_s.strip
  value == 'application/json' || value.end_with?('+json')
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)


88
89
90
# File 'lib/philiprehberger/http_client/response.rb', line 88

def redirected?
  !redirects.empty?
end

#server_error?Boolean

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

Returns:

  • (Boolean)


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

def server_error?
  status >= 500 && status < 600
end

#streaming?Boolean

Returns true if the response was streamed.

Returns:

  • (Boolean)


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

def streaming?
  @streaming
end