Class: NATS::RPC::Client

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

Instance Method Summary collapse

Constructor Details

#initialize(cluster_opts = nil) ⇒ Client

Returns a new instance of Client.



4
5
6
7
# File 'lib/nats/rpc/client.rb', line 4

def initialize(cluster_opts=nil)
  @nats = NATS::IO::Client.new
  @nats.connect cluster_opts || NATS::RPC.cluster_opts
end

Instance Method Details

#request(subscription, obj, opts) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/nats/rpc/client.rb', line 9

def request(subscription, obj, opts)
  obj_json = if obj.is_a? String
    obj
  else
    obj.to_json
  end

  msg = @nats.request subscription, obj_json, opts
  data = JSON.parse(msg["data"])
  payload = JSON.parse(data["payload"])

  if data["status"] == "error"
    case data["code"]
    when 2.0
      rex = RemoteError.new payload["message"]
      rex.set_backtrace payload["backtrace"]
      rex.remote_exception = payload["exception"]
      raise rex
    else
      raise "Error code: #{data["code"]}"
    end
  end
  [data, payload]
end