Class: JSONRPC::BatchResponse

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

Examples:

Create a batch response

batch = JSONRPC::BatchResponse.new([
  JSONRPC::Response.new(result: 7, id: "1"),
  JSONRPC::Response.new(result: 19, id: "2"),
  JSONRPC::Response.new(error: JSONRPC::Error.new(code: -32600, message: "Invalid Request"), id: nil)
])

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(responses) ⇒ BatchResponse

Creates a new JSON-RPC 2.0 Batch Response object

Examples:

Create a batch response

responses = [
  JSONRPC::Response.new(result: 42, id: 1),
  JSONRPC::Response.new(result: "hello", id: 2)
]
batch = JSONRPC::BatchResponse.new(responses)

Parameters:

Raises:

  • (ArgumentError)

    if responses is not an Array

  • (ArgumentError)

    if responses is empty

  • (ArgumentError)

    if any response is not a valid Response



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

def initialize(responses)
  validate_responses(responses)
  @responses = responses
end

Instance Attribute Details

#responsesArray<JSONRPC::Response> (readonly)

The collection of response objects in this batch

Examples:

Accessing responses in a batch

batch.responses # => [#<JSONRPC::Response...>, #<JSONRPC::Response...>]

Returns:



31
32
33
# File 'lib/jsonrpc/batch_response.rb', line 31

def responses
  @responses
end

Instance Method Details

#eachself #eachEnumerator[Response, self]

Implements the Enumerable contract by yielding each response in the batch

Examples:

Iterate over responses

batch.each { |response| puts response.result }

Overloads:

  • #eachself

    Returns:

    • (self)
  • #eachEnumerator[Response, self]

    Returns:

Yields:

  • (response)

    Yields each response in the batch to the block

Yield Parameters:

Returns:

  • (Enumerator)

    if no block is given

  • (BatchResponse)

    self if a block is given



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_hArray<Hash>

Converts the batch response to a JSON-compatible Array

Examples:

Convert batch to hash

batch.to_h # => [{"jsonrpc":"2.0","result":42,"id":1}]

Returns:

  • (Array<Hash>)

    the batch response as a JSON-compatible Array



64
65
66
# File 'lib/jsonrpc/batch_response.rb', line 64

def to_h
  responses.map(&:to_h)
end

#to_jsonString

Converts the batch response to a JSON string

Examples:

Convert batch to JSON

batch.to_json # => '[{"jsonrpc":"2.0","result":42,"id":1}]'

Parameters:

  • (Object)

Returns:

  • (String)

    the JSON-formatted batch response



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

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

#to_responseArray

Converts the batch response to a response format

Examples:

Convert to response format

batch.to_response # => Array of response hashes

Returns:

  • (Array)

    array of response objects



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

Parameters:

  • responses (Array)

    the array of responses

  • (Array[untyped])

Raises:

  • (ArgumentError)

    if responses is not an Array

  • (ArgumentError)

    if responses is empty

  • (ArgumentError)

    if any response is not a valid Response



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