Class: JSONRPC::BatchResponse
- Inherits:
-
Object
- Object
- JSONRPC::BatchResponse
- Includes:
- Enumerable, Enumerable[Response]
- Defined in:
- lib/jsonrpc/batch_response.rb,
sig/jsonrpc/batch_response.rbs
Overview
A JSON-RPC 2.0 Batch Response object
A Batch Response is an Array containing Response objects, corresponding to a Batch Request. The Server should respond with one Response for each Request (except for Notifications which don't receive responses).
Instance Attribute Summary collapse
-
#responses ⇒ Array<JSONRPC::Response>
readonly
The collection of response objects in this batch.
Instance Method Summary collapse
-
#each {|response| ... } ⇒ Enumerator, BatchResponse
Implements the Enumerable contract by yielding each response in the batch.
-
#initialize(responses) ⇒ BatchResponse
constructor
Creates a new JSON-RPC 2.0 Batch Response object.
-
#to_h ⇒ Array<Hash>
Converts the batch response to a JSON-compatible Array.
-
#to_json ⇒ String
Converts the batch response to a JSON string.
-
#to_response ⇒ Array
Converts the batch response to a response format.
-
#validate_responses(responses) ⇒ void
private
Validates that the responses is a valid array of Response objects.
Constructor Details
#initialize(responses) ⇒ BatchResponse
Creates a new JSON-RPC 2.0 Batch Response object
50 51 52 53 |
# File 'lib/jsonrpc/batch_response.rb', line 50 def initialize(responses) validate_responses(responses) @responses = responses end |
Instance Attribute Details
#responses ⇒ Array<JSONRPC::Response> (readonly)
The collection of response objects in this batch
31 32 33 |
# File 'lib/jsonrpc/batch_response.rb', line 31 def responses @responses end |
Instance Method Details
#each ⇒ self #each ⇒ Enumerator[Response, self]
Implements the Enumerable contract by yielding each response in the batch
96 97 98 99 100 101 |
# File 'lib/jsonrpc/batch_response.rb', line 96 def each(&) return to_enum(:each) unless block_given? responses.each(&) self end |
#to_h ⇒ Array<Hash>
Converts the batch response to a JSON-compatible Array
64 65 66 |
# File 'lib/jsonrpc/batch_response.rb', line 64 def to_h responses.map(&:to_h) end |
#to_json ⇒ String
Converts the batch response to a JSON string
77 78 79 |
# File 'lib/jsonrpc/batch_response.rb', line 77 def to_json(*) MultiJson.dump(to_h, *) end |
#to_response ⇒ Array
Converts the batch response to a response format
112 113 114 |
# File 'lib/jsonrpc/batch_response.rb', line 112 def to_response responses.map(&:to_response) end |
#validate_responses(responses) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Validates that the responses is a valid array of Response objects
130 131 132 133 134 135 136 137 |
# File 'lib/jsonrpc/batch_response.rb', line 130 def validate_responses(responses) raise ArgumentError, 'Responses must be an Array' unless responses.is_a?(Array) raise ArgumentError, 'Batch response cannot be empty' if responses.empty? responses.each_with_index do |response, index| raise ArgumentError, "Response at index #{index} is not a valid Response" unless response.is_a?(Response) end end |