Class: Cosmos::JsonRpcRequest
- Defined in:
- lib/cosmos/io/json_rpc.rb
Overview
Represents a JSON Remote Procedure Call Request
Constant Summary collapse
- DANGEROUS_METHODS =
['__send__', 'send', 'instance_eval', 'instance_exec']
Class Method Summary collapse
-
.from_hash(hash) ⇒ JsonRpcRequest
Creates a JsonRpcRequest object from a Hash.
-
.from_json(request_data) ⇒ JsonRpcRequest
Creates a JsonRpcRequest object from a JSON encoded String.
Instance Method Summary collapse
-
#id ⇒ Integer
The request identifier.
-
#initialize(method_name, method_params, id) ⇒ JsonRpcRequest
constructor
A new instance of JsonRpcRequest.
-
#method ⇒ String
The method to call.
-
#params ⇒ Array<String>
Array of strings which represent the parameters to send to the method.
Methods inherited from JsonRpc
Constructor Details
#initialize(method_name, method_params, id) ⇒ JsonRpcRequest
Returns a new instance of JsonRpcRequest.
184 185 186 187 188 189 190 191 192 |
# File 'lib/cosmos/io/json_rpc.rb', line 184 def initialize(method_name, method_params, id) super() @hash['jsonrpc'.freeze] = "2.0".freeze @hash['method'.freeze] = method_name.to_s if method_params and method_params.length != 0 @hash['params'.freeze] = method_params end @hash['id'.freeze] = id.to_i end |
Class Method Details
.from_hash(hash) ⇒ JsonRpcRequest
Creates a JsonRpcRequest object from a Hash
231 232 233 |
# File 'lib/cosmos/io/json_rpc.rb', line 231 def self.from_hash(hash) self.new(hash['method'.freeze], hash['params'.freeze], hash['id'.freeze]) end |
.from_json(request_data) ⇒ JsonRpcRequest
Creates a JsonRpcRequest object from a JSON encoded String. The version must be 2.0 and the JSON must include the method and id members.
215 216 217 218 219 220 221 222 223 224 |
# File 'lib/cosmos/io/json_rpc.rb', line 215 def self.from_json(request_data) begin hash = JSON.parse(request_data, :allow_nan => true, :create_additions => true) # Verify the jsonrpc version is correct and there is a method and id raise unless (hash['jsonrpc'.freeze] == "2.0".freeze && hash['method'.freeze] && hash['id'.freeze]) self.from_hash(hash) rescue raise "Invalid JSON-RPC 2.0 Request" end end |
Instance Method Details
#id ⇒ Integer
Returns The request identifier.
206 207 208 |
# File 'lib/cosmos/io/json_rpc.rb', line 206 def id @hash['id'.freeze] end |
#method ⇒ String
Returns The method to call.
195 196 197 |
# File 'lib/cosmos/io/json_rpc.rb', line 195 def method @hash['method'.freeze] end |