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:



64
65
66
# File 'lib/philiprehberger/http_client/response.rb', line 64

def metrics
  @metrics
end

#redirectsArray<String> (readonly)

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

Returns:

  • (Array<String>)


69
70
71
# File 'lib/philiprehberger/http_client/response.rb', line 69

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

#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)


53
54
55
56
57
58
59
# File 'lib/philiprehberger/http_client/response.rb', line 53

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)


74
75
76
# File 'lib/philiprehberger/http_client/response.rb', line 74

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