Class: OpenC3::JsonRpcRequest

Inherits:
JsonRpc show all
Defined in:
lib/openc3/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

Instance Method Summary collapse

Methods inherited from JsonRpc

#<=>, #as_json, #to_json

Constructor Details

#initialize(method_name, method_params, keyword_params, id) ⇒ JsonRpcRequest

Returns a new instance of JsonRpcRequest.

Parameters:

  • method_name (String)

    The name of the method to call

  • method_params (Array<String>)

    Array of strings which represent the parameters to send to the method

  • keyword_params (Hash<String, Variable>)

    Hash of key value keyword params

  • id (Integer)

    The identifier which will be matched to the response



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/openc3/io/json_rpc.rb', line 224

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

Parameters:

  • hash (Hash)

    Hash containing the following keys: method, params, and id

Returns:



285
286
287
# File 'lib/openc3/io/json_rpc.rb', line 285

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.

Parameters:

  • request_data (String)

    JSON encoded string representing the request

  • request_headers (Hash)

    Request Header to include the auth token

Returns:



269
270
271
272
273
274
275
276
277
278
# File 'lib/openc3/io/json_rpc.rb', line 269

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']
  # 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

#idInteger

Returns The request identifier.

Returns:

  • (Integer)

    The request identifier



259
260
261
# File 'lib/openc3/io/json_rpc.rb', line 259

def id
  @hash['id'.freeze]
end

#keyword_paramsHash<String, Variable>

Returns Hash which represents the keyword parameters to send to the method.

Returns:

  • (Hash<String, Variable>)

    Hash which represents the keyword parameters to send to the method



254
255
256
# File 'lib/openc3/io/json_rpc.rb', line 254

def keyword_params
  @hash['keyword_params'.freeze]
end

#methodString

Returns The method to call.

Returns:

  • (String)

    The method to call



242
243
244
# File 'lib/openc3/io/json_rpc.rb', line 242

def method
  @hash['method'.freeze]
end

#paramsArray<String>

Returns Array of strings which represent the parameters to send to the method.

Returns:

  • (Array<String>)

    Array of strings which represent the parameters to send to the method



248
249
250
# File 'lib/openc3/io/json_rpc.rb', line 248

def params
  @hash['params'.freeze] || []
end