Class: JSONRPC::BatchRequest
- Inherits:
-
Object
- Object
- JSONRPC::BatchRequest
- Includes:
- Enumerable, Enumerable[Request
- Defined in:
- lib/jsonrpc/batch_request.rb,
sig/jsonrpc/batch_request.rbs
Overview
A JSON-RPC 2.0 batch request object
A batch request is an Array filled with Request objects to send several requests at once. The Server should respond with an Array containing the corresponding Response objects.
Instance Attribute Summary collapse
-
#requests ⇒ Array<JSONRPC::Request, JSONRPC::Notification, JSONRPC::Error>
readonly
The collection of request objects in this batch (may include errors).
Instance Method Summary collapse
-
#each {|request| ... } ⇒ Enumerator, BatchRequest
Implements the Enumerable contract by yielding each request in the batch.
-
#initialize(requests) ⇒ BatchRequest
constructor
Creates a new JSON-RPC 2.0 Batch Request object.
-
#process_each {|request_or_notification| ... } ⇒ Array<JSONRPC::Response>
Handles each request/notification in the batch and returns responses.
-
#size ⇒ Integer
(also: #length)
Returns the number of requests in the batch.
-
#to_h ⇒ Array<Hash>
Converts the batch request to a JSON-compatible Array.
-
#to_json ⇒ String
Converts the batch to JSON format.
-
#validate_requests(requests) ⇒ void
private
Validates the requests array.
Constructor Details
#initialize(requests) ⇒ BatchRequest
Creates a new JSON-RPC 2.0 Batch Request object
51 52 53 54 |
# File 'lib/jsonrpc/batch_request.rb', line 51 def initialize(requests) validate_requests(requests) @requests = requests end |
Instance Attribute Details
#requests ⇒ Array<JSONRPC::Request, JSONRPC::Notification, JSONRPC::Error> (readonly)
The collection of request objects in this batch (may include errors)
31 32 33 |
# File 'lib/jsonrpc/batch_request.rb', line 31 def requests @requests end |
Instance Method Details
#each ⇒ self #each ⇒ Enumerator[Request | Notification | Error, self]
Implements the Enumerable contract by yielding each request in the batch
97 98 99 100 101 102 |
# File 'lib/jsonrpc/batch_request.rb', line 97 def each(&) return to_enum(:each) unless block_given? requests.each(&) self end |
#process_each {|request_or_notification| ... } ⇒ Array<JSONRPC::Response>
Handles each request/notification in the batch and returns responses
146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/jsonrpc/batch_request.rb', line 146 def process_each raise ArgumentError, 'Block required' unless block_given? flat_map do |request_or_notification| result = yield(request_or_notification) if request_or_notification.is_a?(JSONRPC::Request) JSONRPC::Response.new(id: request_or_notification.id, result:) end end.compact end |
#size ⇒ Integer Also known as: length
Returns the number of requests in the batch
113 |
# File 'lib/jsonrpc/batch_request.rb', line 113 def size = requests.size |
#to_h ⇒ Array<Hash>
Converts the batch request to a JSON-compatible Array
65 66 67 |
# File 'lib/jsonrpc/batch_request.rb', line 65 def to_h requests.map { |item| item.respond_to?(:to_h) ? item.to_h : item } end |
#to_json ⇒ String
Converts the batch to JSON format
78 79 80 |
# File 'lib/jsonrpc/batch_request.rb', line 78 def to_json(*) MultiJson.dump(to_h, *) end |
#validate_requests(requests) ⇒ 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 the requests array
172 173 174 175 176 177 178 179 180 181 |
# File 'lib/jsonrpc/batch_request.rb', line 172 def validate_requests(requests) raise ArgumentError, 'Requests must be an Array' unless requests.is_a?(Array) raise ArgumentError, 'Batch request cannot be empty' if requests.empty? requests.each_with_index do |request, index| unless request.is_a?(Request) || request.is_a?(Notification) || request.is_a?(Error) raise ArgumentError, "Request at index #{index} is not a valid Request, Notification, or Error" end end end |