Class: OpenC3::JsonRpcRequest
- Defined in:
- lib/openc3/io/json_rpc.rb
Overview
Represents a JSON Remote Procedure Call Request
Constant Summary collapse
- DANGEROUS_METHODS =
Class Method Summary collapse
-
.from_hash(hash) ⇒ JsonRpcRequest
Creates a JsonRpcRequest object from a Hash.
-
.from_json(request_data, request_headers) ⇒ JsonRpcRequest
Creates a JsonRpcRequest object from a JSON encoded String.
Instance Method Summary collapse
-
#id ⇒ Integer
The request identifier.
-
#initialize(method_name, method_params, keyword_params, id) ⇒ JsonRpcRequest
constructor
A new instance of JsonRpcRequest.
-
#keyword_params ⇒ Hash<String, Variable>
Hash which represents the keyword parameters to send to the method.
-
#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, keyword_params, id) ⇒ JsonRpcRequest
Returns a new instance of JsonRpcRequest.
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/openc3/io/json_rpc.rb', line 237 def initialize(method_name, method_params, keyword_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 if keyword_params and keyword_params.length != 0 symbolized = {} keyword_params.each do |key, value| symbolized[key.intern] = value end @hash['keyword_params'.freeze] = symbolized end @hash['id'.freeze] = id.to_i end |
Class Method Details
.from_hash(hash) ⇒ JsonRpcRequest
Creates a JsonRpcRequest object from a Hash
299 300 301 |
# File 'lib/openc3/io/json_rpc.rb', line 299 def self.from_hash(hash) self.new(hash['method'.freeze], hash['params'.freeze], hash['keyword_params'.freeze], hash['id'.freeze]) end |
.from_json(request_data, request_headers) ⇒ 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.
282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/openc3/io/json_rpc.rb', line 282 def self.from_json(request_data, request_headers) hash = JSON.parse(request_data, allow_nan: true, create_additions: true) hash['keyword_params']['token'] = request_headers['HTTP_AUTHORIZATION'] if request_headers['HTTP_AUTHORIZATION'] hash['keyword_params']['manual'] = request_headers['HTTP_MANUAL'] if request_headers['HTTP_MANUAL'] # 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\n#{request_data.inspect}\n" end |
Instance Method Details
#id ⇒ Integer
Returns The request identifier.
272 273 274 |
# File 'lib/openc3/io/json_rpc.rb', line 272 def id @hash['id'.freeze] end |
#keyword_params ⇒ Hash<String, Variable>
Returns Hash which represents the keyword parameters to send to the method.
267 268 269 |
# File 'lib/openc3/io/json_rpc.rb', line 267 def keyword_params @hash['keyword_params'.freeze] end |
#method ⇒ String
Returns The method to call.
255 256 257 |
# File 'lib/openc3/io/json_rpc.rb', line 255 def method @hash['method'.freeze] end |