Module: Protocol::Jsonrpc::Message
- Defined in:
- lib/protocol/jsonrpc/message.rb
Overview
Protocol::Jsonrpc::Message provides operations for creating and validating JSON-RPC messages. This class handles the pure functional aspects of JSON-RPC like:
-
Creating properly formatted request/notification messages
-
Validating incoming messages
-
Parsing responses and errors
Class Method Summary collapse
-
.from_hash(parsed) ⇒ Message
The parsed message.
-
.load(data) ⇒ Message+
Validate, and return the JSON-RPC message or batch.
Instance Method Summary collapse
- #as_json ⇒ Object
-
#error? ⇒ Boolean
Is this an error response? (ErrorResponse).
-
#invalid? ⇒ Boolean
Is this an invalid message? (InvalidMessage).
-
#notification? ⇒ Boolean
Is this a notification? (Notification).
-
#request? ⇒ Boolean
Is this a request? (Request).
-
#response? ⇒ Boolean
Is this a response to a request? (Error or Response).
- #to_json ⇒ Object
- #to_s ⇒ Object
Class Method Details
.from_hash(parsed) ⇒ Message
Returns The parsed message.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/protocol/jsonrpc/message.rb', line 37 def from_hash(parsed) return InvalidMessage.new(data: parsed.inspect) unless parsed.is_a?(Hash) jsonrpc = parsed[:jsonrpc] case parsed in {id:, error:} ErrorResponse.new(id:, error: Error.(**error), jsonrpc:) in {id:, result:} Response.new(id:, result:, jsonrpc:) in {id:, method:} Request.new(id:, method:, params: parsed[:params], jsonrpc:) in {method:} Notification.new(method:, params: parsed[:params], jsonrpc:) else InvalidMessage.new(data: parsed.inspect) end rescue => error InvalidMessage.new(error:, data: parsed.inspect) end |
.load(data) ⇒ Message+
Validate, and return the JSON-RPC message or batch
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/protocol/jsonrpc/message.rb', line 24 def load(data) case data when Hash from_hash(data) when Array Batch.load(data) else InvalidMessage.new(data: data.inspect) end end |
Instance Method Details
#as_json ⇒ Object
59 |
# File 'lib/protocol/jsonrpc/message.rb', line 59 def as_json = to_h |
#error? ⇒ Boolean
Is this an error response? (ErrorResponse)
75 |
# File 'lib/protocol/jsonrpc/message.rb', line 75 def error? = false |
#invalid? ⇒ Boolean
Is this an invalid message? (InvalidMessage)
78 |
# File 'lib/protocol/jsonrpc/message.rb', line 78 def invalid? = false |
#notification? ⇒ Boolean
Is this a notification? (Notification)
69 |
# File 'lib/protocol/jsonrpc/message.rb', line 69 def notification? = false |
#request? ⇒ Boolean
Is this a request? (Request)
66 |
# File 'lib/protocol/jsonrpc/message.rb', line 66 def request? = false |
#response? ⇒ Boolean
Is this a response to a request? (Error or Response)
72 |
# File 'lib/protocol/jsonrpc/message.rb', line 72 def response? = false |
#to_json ⇒ Object
61 |
# File 'lib/protocol/jsonrpc/message.rb', line 61 def to_json(...) = JSON.generate(as_json, ...) |
#to_s ⇒ Object
63 |
# File 'lib/protocol/jsonrpc/message.rb', line 63 def to_s = to_json |