Class: JSONRPC::BatchRequest
- Inherits:
-
Object
- Object
- JSONRPC::BatchRequest
- Includes:
- Enumerable
- Defined in:
- lib/jsonrpc/batch_request.rb
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.
Constructor Details
#initialize(requests) ⇒ BatchRequest
Creates a new JSON-RPC 2.0 Batch Request object
50 51 52 53 |
# File 'lib/jsonrpc/batch_request.rb', line 50 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)
29 30 31 |
# File 'lib/jsonrpc/batch_request.rb', line 29 def requests @requests end |
Instance Method Details
#each {|request| ... } ⇒ Enumerator, BatchRequest
Implements the Enumerable contract by yielding each request in the batch
96 97 98 99 100 101 |
# File 'lib/jsonrpc/batch_request.rb', line 96 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
147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/jsonrpc/batch_request.rb', line 147 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
112 113 114 |
# File 'lib/jsonrpc/batch_request.rb', line 112 def size requests.size end |
#to_h ⇒ Array<Hash>
Converts the batch request to a JSON-compatible Array
64 65 66 |
# File 'lib/jsonrpc/batch_request.rb', line 64 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
77 78 79 |
# File 'lib/jsonrpc/batch_request.rb', line 77 def to_json(*) MultiJson.dump(to_h, *) end |