Class: Protobuf::Rpc::Clients::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/protobuf/rpc/clients/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host: nil, port: nil, timeout: 1, stdout: STDOUT, stderr: STDERR, namespace: nil) ⇒ Base



10
11
12
13
14
15
16
17
# File 'lib/protobuf/rpc/clients/base.rb', line 10

def initialize(host: nil, port: nil, timeout: 1, stdout: STDOUT, stderr: STDERR, namespace: nil)
  @host = host
  @port = port
  @timeout = timeout
  @namespace = namespace
  @logger ||= Logger.new(stdout)
  @error_logger ||= Logger.new(stderr)
end

Instance Attribute Details

#error_loggerObject

Returns the value of attribute error_logger.



5
6
7
# File 'lib/protobuf/rpc/clients/base.rb', line 5

def error_logger
  @error_logger
end

#hostObject

Returns the value of attribute host.



6
7
8
# File 'lib/protobuf/rpc/clients/base.rb', line 6

def host
  @host
end

#loggerObject

Returns the value of attribute logger.



5
6
7
# File 'lib/protobuf/rpc/clients/base.rb', line 5

def logger
  @logger
end

#portObject

Returns the value of attribute port.



6
7
8
# File 'lib/protobuf/rpc/clients/base.rb', line 6

def port
  @port
end

#timeoutObject

Returns the value of attribute timeout.



6
7
8
# File 'lib/protobuf/rpc/clients/base.rb', line 6

def timeout
  @timeout
end

Class Method Details

.implement_rpc(rpc_method) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/protobuf/rpc/clients/base.rb', line 108

def self.implement_rpc(rpc_method)
  define_method(rpc_method) do |*args|
    msg = if args.first.is_a?(Protobuf::Message)
            args.shift
          elsif args.first.respond_to?(:to_hash)
            Messages::RpcCompressedMessage.new(response_body: ActiveSupport::Gzip.compress(args.first.to_hash.to_json),
                                               serializer: :JSON,
                                               compressed: true)
          else
            names = self.class.name.split('::')
            msg_class = Object.const_get([names[0..-3], 'Messages', names[-1]].flatten.join('::'))
            msg_class.new(*args)
          end

    send_rpc_request(rpc_method, msg, serializer: :JSON)
  end
end

Instance Method Details

#check_response_error(res, raise_error: true) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/protobuf/rpc/clients/base.rb', line 35

def check_response_error(res, raise_error: true)
  case res
    when Protobuf::Rpc::Messages::Error
      error_class = self.class.mutex.synchronize { define_error_class(res) }
      error = error_class.new(res.error_message)
      error.set_backtrace(res.error_backtrace)
      raise_error(error, raise_error)
    when Protobuf::Error
      error_reason = Protobuf::Socketrpc::ErrorReason.name_for_tag(res.code).to_s.downcase
      error_class = Protobuf::Rpc.const_get(error_reason.camelize)
      error = error_class.new(res.message)
      raise_error(error, raise_error)
    else
      res
  end
end

#send_rpc_request(method, msg, serializer:) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/protobuf/rpc/clients/base.rb', line 19

def send_rpc_request(method, msg, serializer:)
  res = nil

  internal_client.send(method, Serializer.dump(msg, serializer: serializer)) do |c|
    c.on_success do |rpc_compressed_message|
      res = Protobuf::Rpc::Serializer.load(rpc_compressed_message)
    end

    c.on_failure do |error|
      res = error
    end
  end

  check_response_error(res)
end