class Jsonrpctcp::Client
The JSON-RPC client
Attributes
host[R]
port[R]
Public Class Methods
gen_id()
click to toggle source
Generate a message id - currently the current time @return [Fixnum] A time-based id
# File lib/jsonrpctcp/client.rb, line 66 def self.gen_id Time.now.to_i end
is_error?(response)
click to toggle source
@return [TrueClass,FalseClass] returns whether a
response does have an error key
# File lib/jsonrpctcp/client.rb, line 43 def self.is_error?(response) return !response || !response.is_a?(Hash) || response.has_key?('error') end
new(host, port)
click to toggle source
Initialize @param host [String] a hostname or IP @param port [String,Fixnum] a port number
# File lib/jsonrpctcp/client.rb, line 30 def initialize(host, port) @host = host @port = port end
success?(response)
click to toggle source
@return [TrueClass,FalseClass] returns whether a
response does not have an error key
# File lib/jsonrpctcp/client.rb, line 37 def self.success?(response) return !Client.is_error?(response) end
Public Instance Methods
[](method, *args)
click to toggle source
Calls an RPC methods in the client[:mymethod, “arg1”,…] fashion @param method [Symbol] A RPC method name @param args [Array] The arguments for the method are passed as
parameters to the function
# File lib/jsonrpctcp/client.rb, line 60 def [](method, *args) return process_call(method, args) end
method_missing(method, *args)
click to toggle source
Allows to call RPC methods as if they were defined functions: client.mymethod(…) @param method [Symbol] A RPC method name @param args [Array] The arguments for the method are passed as
parameters to the function
# File lib/jsonrpctcp/client.rb, line 52 def method_missing(method, *args) return process_call(method, args) end
Private Instance Methods
process_call(method, args)
click to toggle source
# File lib/jsonrpctcp/client.rb, line 72 def process_call(method, args) call_obj = { 'jsonrpc' => '2.0', 'method' => method, 'params' => args, 'id' => Client.gen_id } call_obj_json = call_obj.to_json begin socket = TCPSocket.open(@host, @port) socket.write(call_obj_json) socket.close_write() response = socket.read() parsed_response = JSON.load(response) rescue JSON::ParserError raise RPCException.new("RPC response could not be parsed") ensure socket.close() if socket end if Client.is_error?(parsed_response) raise RPCError.from_rpc_response(parsed_response) else return parsed_response end end