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
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 {|request| ... } ⇒ Enumerator, BatchRequest
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 |