Class: RPC::Client

Inherits:
BasicObject
Defined in:
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.



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rpc.rb', line 70

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

1) Sync: it’ll return the value. 2) Async: you have to add #subscribe



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/rpc.rb', line 94

def method_missing(method, *args, &callback)
  binary = @encoder.encode(method, *args)

  if @client.async?
    @client.send(binary) do |encoded_result|
      result = @encoder.decode(encoded_result)
      callback.call(result["result"], get_exception(result["error"]))
    end
  else
    ::Kernel.raise("You can't specify callback for a synchronous client.") if callback

    encoded_result = @client.send(binary)
    result = @encoder.decode(encoded_result)

    if result.respond_to?(:merge) # Hash, only one result.
      result_or_raise(result)
    else # Array, multiple results.
      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



65
66
67
68
# File 'lib/rpc.rb', line 65

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



87
88
89
90
# File 'lib/rpc.rb', line 87

def batch(*args)
  data = @encoder.batch(*args)
  @client.send(data)
end

#close_connectionObject



139
140
141
# File 'lib/rpc.rb', line 139

def close_connection
  @client.disconnect
end

#get_exception(error) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
# File 'lib/rpc.rb', line 127

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"]
  instance = klass.new(message)
  instance.extend(::RPC::ExceptionsMixin)
  instance.server_backtrace = exception["backtrace"]
  instance
end

#notification(*args) ⇒ Object



82
83
84
85
# File 'lib/rpc.rb', line 82

def notification(*args)
  data = @encoder.notification(*args)
  @client.send(data)
end

#result_or_raise(result) ⇒ Object



118
119
120
121
122
123
124
125
# File 'lib/rpc.rb', line 118

def result_or_raise(result)
  if error = result["error"]
    exception = self.get_exception(error)
    ::Kernel.raise(exception)
  else
    result["result"]
  end
end