Class: RawJsonRpc::RawClientJsonRpc

Inherits:
Object
  • Object
show all
Defined in:
lib/rawjsonrpc/client.rb

Overview

Implements the base class for all Clients. The class get all methode calls generate the json data from it

Every client must implement get_response and send_request.

=Examples ==send request

client.request("run", [1,2,3]) # calls: run params: 1 2 3 ret = client.request("attack", "alpha", 1, 3.0) # calls: attack # params "alpha", 1, 3.0 # returns data into ret ==send request shortcut

client.run([1,2,3]) ret = client.attack("alpha", 1, 3.0)

==send notification (no return value) client.notification("beat", [:alpha, 123], "fast")

Direct Known Subclasses

ClientSock

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

redirect calls



37
38
39
40
41
42
43
# File 'lib/rawjsonrpc/client.rb', line 37

def method_missing(name, *args)
  if args.empty?
    request(name)
  else
    request(name, *args)
  end
end

Instance Method Details

#get_responseObject

returns the server response must be implmented

Raises:

  • (NotImplementedError)


53
54
55
# File 'lib/rawjsonrpc/client.rb', line 53

def get_response
  raise NotImplementedError.new("method not overriden")
end

#notification(function_name, *para) ⇒ Object

Sends a notification to server. A Notifications don't return any value



66
67
68
69
70
71
# File 'lib/rawjsonrpc/client.rb', line 66

def notification(function_name, *para)
  para = {'method' => function_name, 'params' => para, 'id' => nil}
    .to_json
  send_request para
  nil
end

#request(function_name, *para) ⇒ Object

Sends a request to server return a value or raise an RpcException if server sends an error.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/rawjsonrpc/client.rb', line 74

def request(function_name, *para)
  id_count = id
  para = {'method' => function_name, 'params' =>  para, 'id' => id_count}
    .to_json
  send_request(para)
  result = get_response
  if result.empty? or result == nil
    raise(RpcError.new("empty respons"))
  end
  response = JSON.load(result)
  if response["error"] != nil
    raise(RpcError.new(response["error"]))
  elsif response["id"] != id_count
    raise(RpcError.new("server send wrong data."))
  end
  response["result"]
end

#send_request(para) ⇒ Object

Send the request to server must be implmented. takes json string.

Raises:

  • (NotImplementedError)


59
60
61
# File 'lib/rawjsonrpc/client.rb', line 59

def send_request para
  raise NotImplementedError.new("method not overriden")
end