Class: RPC::Client
- Inherits:
-
BasicObject
- Defined in:
- lib/rpc/lib/rpc.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(client, encoder = Encoders::Json::Client.new, &block) ⇒ Client
Returns a new instance of Client.
71
72
73
74
75
76
77
78
79
80
81
|
# File 'lib/rpc/lib/rpc.rb', line 71
def initialize(client, encoder = Encoders::Json::Client.new, &block)
@client, @encoder = client, encoder
if block
@client.run do
block.call(self)
end
else
@client.connect
end
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &callback) ⇒ Object
TODO: this should be refactored and moved to the encoder, because result and similar are JSON-RPC-specific.
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
# File 'lib/rpc/lib/rpc.rb', line 98
def method_missing(method, *args, &callback)
binary = @encoder.encode(method, *args)
if @client.async? && ! callback @client.send(binary)
elsif @client.async? && callback
@client.send(binary) do |encoded_result|
result = @encoder.decode(encoded_result)
if result.respond_to?(:merge) callback.call(result["result"], get_exception(result["error"]))
else result.map do |result|
callback.call(result["result"], get_exception(result["error"]))
end
end
end
else
::Kernel.raise("You can't specify callback for a synchronous client.") if callback
encoded_result = @client.send(binary)
if encoded_result.nil?
::Kernel.raise("Bug in #{@client.class}#send(data), it can never return nil in the sync mode!")
end
result = @encoder.decode(encoded_result)
if result.respond_to?(:merge) result_or_raise(result)
else result.map do |result|
result_or_raise(result)
end
end
end
end
|
Class Method Details
.setup(uri, client_class = Clients::NetHttp, encoder = Encoders::Json::Client.new) ⇒ Object
66
67
68
69
|
# File 'lib/rpc/lib/rpc.rb', line 66
def self.setup(uri, client_class = Clients::NetHttp, encoder = Encoders::Json::Client.new)
client = client_class.new(uri)
self.new(client, encoder)
end
|
Instance Method Details
#batch(*args) ⇒ Object
88
89
90
91
|
# File 'lib/rpc/lib/rpc.rb', line 88
def batch(*args)
data = @encoder.batch(*args)
@client.send(data)
end
|
#close_connection ⇒ Object
162
163
164
|
# File 'lib/rpc/lib/rpc.rb', line 162
def close_connection
@client.disconnect
end
|
#get_exception(error) ⇒ Object
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
# File 'lib/rpc/lib/rpc.rb', line 145
def get_exception(error)
return unless error
exception = error["error"]
resolved_class = ::RPC.full_const_get(exception["class"])
klass = resolved_class || ::RuntimeError
message = resolved_class ? exception["message"] : error["message"]
case klass.instance_method(:initialize).arity
when 2
instance = klass.new(message, nil)
else
instance = klass.new(message)
end
instance.extend(::RPC::ExceptionsMixin)
instance.server_backtrace = exception["backtrace"]
instance
end
|
#notification(*args) ⇒ Object
83
84
85
86
|
# File 'lib/rpc/lib/rpc.rb', line 83
def notification(*args)
data = @encoder.notification(*args)
@client.send(data)
end
|
#result_or_raise(result) ⇒ Object
136
137
138
139
140
141
142
143
|
# File 'lib/rpc/lib/rpc.rb', line 136
def result_or_raise(result)
if error = result["error"]
exception = self.get_exception(error)
::Kernel.raise(exception)
else
result["result"]
end
end
|