Class: X::Response
- Inherits:
-
Object
- Object
- X::Response
- Includes:
- ResponseHeaders
- Defined in:
- x-core/lib/x/core/response.rb
Overview
A summary of one API response, or one object of a stream, which a client passes to its on_response hook
Instance Attribute Summary collapse
-
#http_method ⇒ Symbol
readonly
The HTTP method of the request.
-
#http_response ⇒ Net::HTTPResponse
readonly
The response itself, as the client received it.
-
#uri ⇒ URI::Generic
readonly
The URI of the request.
Instance Method Summary collapse
-
#body ⇒ String?
The body summarized: one streamed object, or else the whole body.
-
#headers ⇒ Hash{String => String}
The headers of the response.
-
#initialize(http_method:, uri:, http_response: nil, status: nil, headers: nil, body: nil) ⇒ Response
constructor
Summarize a response.
-
#rate_limit ⇒ RateLimit?
The 15-minute rate limit of the endpoint, which nearly every response reports.
-
#rate_limits ⇒ Array<RateLimit>
The rate limits the response reports in its headers.
-
#resource_count ⇒ Integer
The number of resources the body holds, in data and includes together.
-
#resource_counts ⇒ Hash{String => Integer}
The number of resources the body holds, as data and as each kind of include.
-
#status ⇒ Integer
The HTTP status code.
-
#success? ⇒ Boolean
Check whether the request succeeded.
Constructor Details
#initialize(http_method:, uri:, http_response: nil, status: nil, headers: nil, body: nil) ⇒ Response
Summarize a response
Public, so that an on_response hook can be tested with a summary built from the status, headers, and body of a response, or from a Net::HTTP response, as the client builds one for each response it reads.
74 75 76 77 78 79 |
# File 'x-core/lib/x/core/response.rb', line 74 def initialize(http_method:, uri:, http_response: nil, status: nil, headers: nil, body: nil) @http_method = http_method.downcase.to_sym @uri = uri @http_response = BuiltResponse.of(http_response, status:, headers:, body: (body if http_response.nil?)) @body = body end |
Instance Attribute Details
#http_method ⇒ Symbol (readonly)
The HTTP method of the request
30 31 32 |
# File 'x-core/lib/x/core/response.rb', line 30 def http_method @http_method end |
#http_response ⇒ Net::HTTPResponse (readonly)
The response itself, as the client received it
It is an escape hatch, for what a summary does not read: the status is #status, the headers are #headers, and the body is #body. It is the Net::HTTP response the client sent the request with, or the one built of the status, headers, and body the summary was given.
49 50 51 |
# File 'x-core/lib/x/core/response.rb', line 49 def http_response @http_response end |
#uri ⇒ URI::Generic (readonly)
The URI of the request
37 38 39 |
# File 'x-core/lib/x/core/response.rb', line 37 def uri @uri end |
Instance Method Details
#body ⇒ String?
The body summarized: one streamed object, or else the whole body
It is tagged UTF-8, the encoding of the JSON the API sends. A body that is not valid UTF-8 keeps its bytes, so valid_encoding? tells it apart, and scrub replaces what is not UTF-8.
90 |
# File 'x-core/lib/x/core/response.rb', line 90 def body = @body || http_response.body |
#headers ⇒ Hash{String => String}
The headers of the response
The names are lowercase, and a field the API sent more than once is joined with a comma.
|
|
# File 'x-core/lib/x/core/response.rb', line 16
|
#rate_limit ⇒ RateLimit?
The 15-minute rate limit of the endpoint, which nearly every response reports
122 |
# File 'x-core/lib/x/core/response.rb', line 122 def rate_limit = rate_limits.find { |limit| limit.type.eql?(RateLimit::RATE_LIMIT_TYPE) } |
#rate_limits ⇒ Array<RateLimit>
The rate limits the response reports in its headers
114 |
# File 'x-core/lib/x/core/response.rb', line 114 def rate_limits = RateLimit.__send__(:all_from, http_response) |
#resource_count ⇒ Integer
The number of resources the body holds, in data and includes together
148 |
# File 'x-core/lib/x/core/response.rb', line 148 def resource_count = resource_counts.values.sum |
#resource_counts ⇒ Hash{String => Integer}
The number of resources the body holds, as data and as each kind of include
The API bills reads by the resource, so these counts are the units a request consumed. The body is parsed once, however many times a summary is asked what it holds. A body whose includes is not an object holds no includes to count.
134 135 136 137 138 139 140 |
# File 'x-core/lib/x/core/response.rb', line 134 def resource_counts body = parsed_body data = body["data"] counts = {"data" => Array.try_convert(data)&.size || [data].compact.size} Hash.try_convert(body["includes"])&.each { |key, resources| counts[key] = Array(resources).size } counts end |
#status ⇒ Integer
The HTTP status code
98 |
# File 'x-core/lib/x/core/response.rb', line 98 def status = Integer(http_response.code) |
#success? ⇒ Boolean
Check whether the request succeeded
106 |
# File 'x-core/lib/x/core/response.rb', line 106 def success? = http_response.is_a?(Net::HTTPSuccess) |