Class: JSONRPC::BatchResponse

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/jsonrpc/batch_response.rb

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:



29
30
31
# File 'lib/jsonrpc/batch_response.rb', line 29

def responses
  @responses
end

Instance Method Details

#each {|response| ... } ⇒ Enumerator, BatchResponse

Implements the Enumerable contract by yielding each response in the batch

Examples:

Iterate over responses

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

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}]'

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