Class: JSON_RPC::Response
- Inherits:
-
Data
- Object
- Data
- JSON_RPC::Response
- Defined in:
- lib/json_rpc/response.rb
Overview
Represents a JSON-RPC response object.
Instance Attribute Summary collapse
-
#error ⇒ Object
readonly
Returns the value of attribute error.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#result ⇒ Object
readonly
Returns the value of attribute result.
Class Method Summary collapse
Instance Method Summary collapse
- #as_json ⇒ Object
-
#initialize(id:, **kwargs) ⇒ Response
constructor
Initializes a new Response.
-
#to_h ⇒ Hash
Returns a hash representation of the response, ready for JSON serialization.
Constructor Details
#initialize(id:, **kwargs) ⇒ Response
Initializes a new Response.
12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/json_rpc/response.rb', line 12 def initialize(id:, **kwargs) # Check which parameters were actually provided has_result = kwargs.key?(:result) has_error = kwargs.key?(:error) result = kwargs[:result] error = kwargs[:error] validate_response(id, has_result, has_error, result, error) error_obj = process_error(error) super(id: id, result: result, error: error_obj) end |
Instance Attribute Details
#error ⇒ Object (readonly)
Returns the value of attribute error
5 6 7 |
# File 'lib/json_rpc/response.rb', line 5 def error @error end |
#id ⇒ Object (readonly)
Returns the value of attribute id
5 6 7 |
# File 'lib/json_rpc/response.rb', line 5 def id @id end |
#result ⇒ Object (readonly)
Returns the value of attribute result
5 6 7 |
# File 'lib/json_rpc/response.rb', line 5 def result @result end |
Class Method Details
.from_h(h) ⇒ Object
26 27 28 29 30 31 |
# File 'lib/json_rpc/response.rb', line 26 def self.from_h(h) args = { id: h["id"] } args[:result] = h["result"] if h.key?("result") args[:error] = h["error"] if h.key?("error") new(**args) end |
Instance Method Details
#as_json ⇒ Object
47 |
# File 'lib/json_rpc/response.rb', line 47 def as_json(*) = to_h |
#to_h ⇒ Hash
Returns a hash representation of the response, ready for JSON serialization.
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/json_rpc/response.rb', line 36 def to_h hash = { "jsonrpc" => "2.0", id: id } if error hash[:error] = error # error is already a hash here else # Result must be included, even if null, for successful responses hash[:result] = result end hash end |