Class: JSONRPC::BatchRequest

Inherits:
Object
  • Object
show all
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.

Examples:

Create a batch request with multiple requests

batch = JSONRPC::BatchRequest.new([
  JSONRPC::Request.new(method: "sum", params: [1, 2, 4], id: "1"),
  JSONRPC::Notification.new(method: "notify_hello", params: [7]),
  JSONRPC::Request.new(method: "subtract", params: [42, 23], id: "2")
])

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(requests) ⇒ BatchRequest

Creates a new JSON-RPC 2.0 Batch Request object

Examples:

Create a batch request

requests = [
  JSONRPC::Request.new(method: 'add', params: [1, 2], id: 1),
  JSONRPC::Notification.new(method: 'notify', params: ['hello'])
]
batch = JSONRPC::BatchRequest.new(requests)

Parameters:

Raises:

  • (ArgumentError)

    if requests is not an Array

  • (ArgumentError)

    if requests is empty

  • (ArgumentError)

    if any request is not a valid Request, Notification, or Error



50
51
52
53
# File 'lib/jsonrpc/batch_request.rb', line 50

def initialize(requests)
  validate_requests(requests)
  @requests = requests
end

Instance Attribute Details

#requestsArray<JSONRPC::Request, JSONRPC::Notification, JSONRPC::Error> (readonly)

The collection of request objects in this batch (may include errors)

Examples:

Accessing requests in a batch

batch = JSONRPC::BatchRequest.new([request1, request2])
batch.requests # => [#<JSONRPC::Request...>, #<JSONRPC::Request...>]

Returns:



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

Examples:

Iterate over requests

batch.each { |request| puts request.method }

Yields:

  • (request)

    Yields each request in the batch to the block

Yield Parameters:

Returns:

  • (Enumerator)

    if no block is given

  • (BatchRequest)

    self if a block is given



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

Examples:

Handle batch with a block

batch.process_each do |request_or_notification|
  # Process the request/notification
  result = some_processing(request_or_notification.params)
  result
end

Yields:

  • (request_or_notification)

    Yields each request/notification in the batch

Yield Parameters:

Yield Returns:

  • (Object)

    the result of processing the request. Notifications yield no results.

Returns:

  • (Array<JSONRPC::Response>)

    responses for requests only (notifications return no response)

Raises:

  • (ArgumentError)


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

#sizeInteger Also known as: length

Returns the number of requests in the batch

Examples:

Get batch size

batch.size # => 3

Returns:

  • (Integer)

    the number of requests in the batch



112
113
114
# File 'lib/jsonrpc/batch_request.rb', line 112

def size
  requests.size
end

#to_hArray<Hash>

Converts the batch request to a JSON-compatible Array

Examples:

Convert batch to hash

batch.to_h # => [{"jsonrpc":"2.0","method":"add","params":[1,2],"id":1}]

Returns:

  • (Array<Hash>)

    the batch request as 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_jsonString

Converts the batch to JSON format

Examples:

Convert batch to JSON

batch.to_json # => '[{"id":"1","method":"sum","params":[1,2,4]}]'

Returns:

  • (String)

    the JSON-formatted batch



77
78
79
# File 'lib/jsonrpc/batch_request.rb', line 77

def to_json(*)
  MultiJson.dump(to_h, *)
end