Class: JSON_RPC::Request
- Inherits:
-
Data
- Object
- Data
- JSON_RPC::Request
- Defined in:
- lib/json_rpc/request.rb
Overview
Represents a JSON-RPC request object.
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
Class Method Summary collapse
Instance Method Summary collapse
- #as_json ⇒ Object
-
#initialize(id:, method:, params: nil) ⇒ Request
constructor
Initializes a new Request.
-
#to_h ⇒ Hash
Returns a hash representation of the request, ready for JSON serialization.
Constructor Details
#initialize(id:, method:, params: nil) ⇒ Request
Initializes a new Request.
12 13 14 15 16 17 |
# File 'lib/json_rpc/request.rb', line 12 def initialize(id:, method:, params: nil) # Basic validation for ID type (String, Numeric, or null allowed by spec) validate_id_type(id) # Basic validation for method (e.g., non-empty string) could be added. super end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id
5 6 7 |
# File 'lib/json_rpc/request.rb', line 5 def id @id end |
#method ⇒ Object (readonly)
Returns the value of attribute method
5 6 7 |
# File 'lib/json_rpc/request.rb', line 5 def method @method end |
#params ⇒ Object (readonly)
Returns the value of attribute params
5 6 7 |
# File 'lib/json_rpc/request.rb', line 5 def params @params end |
Class Method Details
.from_h(h) ⇒ Object
19 20 21 |
# File 'lib/json_rpc/request.rb', line 19 def self.from_h(h) new(id: h["id"], method: h["method"], params: h["params"]) end |
Instance Method Details
#as_json ⇒ Object
37 |
# File 'lib/json_rpc/request.rb', line 37 def as_json(*) = to_h |
#to_h ⇒ Hash
Returns a hash representation of the request, ready for JSON serialization.
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/json_rpc/request.rb', line 26 def to_h hash = { "jsonrpc" => "2.0", id: id, # Include id even if null, spec allows null id method: method } # Include params only if it's not nil hash[:params] = params unless params.nil? hash end |