Class: Protobuf::Rpc::Clients::Base
- Inherits:
-
Object
- Object
- Protobuf::Rpc::Clients::Base
- Defined in:
- lib/protobuf/rpc/clients/base.rb
Instance Attribute Summary collapse
-
#error_logger ⇒ Object
Returns the value of attribute error_logger.
-
#host ⇒ Object
Returns the value of attribute host.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#port ⇒ Object
Returns the value of attribute port.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
Class Method Summary collapse
Instance Method Summary collapse
- #check_response_error(res, raise_error: true) ⇒ Object
-
#initialize(host: nil, port: nil, timeout: 1, stdout: STDOUT, stderr: STDERR, namespace: nil) ⇒ Base
constructor
A new instance of Base.
- #internal_client ⇒ Object
- #send_rpc_request(method, msg, serializer:) ⇒ Object
Constructor Details
#initialize(host: nil, port: nil, timeout: 1, stdout: STDOUT, stderr: STDERR, namespace: nil) ⇒ Base
10 11 12 13 14 15 16 17 18 19 20 |
# 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) names = self.class.name.split('::') @service = Object.const_get([names[0..-3], 'Services', names[-1]].flatten.join('::')) end |
Instance Attribute Details
#error_logger ⇒ Object
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 |
#host ⇒ Object
Returns the value of attribute host.
6 7 8 |
# File 'lib/protobuf/rpc/clients/base.rb', line 6 def host @host end |
#logger ⇒ Object
Returns the value of attribute logger.
5 6 7 |
# File 'lib/protobuf/rpc/clients/base.rb', line 5 def logger @logger end |
#port ⇒ Object
Returns the value of attribute port.
6 7 8 |
# File 'lib/protobuf/rpc/clients/base.rb', line 6 def port @port end |
#timeout ⇒ Object
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
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/protobuf/rpc/clients/base.rb', line 107 def self.implement_rpc(rpc_method) define_method(rpc_method) do |*args| names = self.class.name.split('::') msg_class_name = [names[0..-3], 'Messages', names[-1]].flatten.join('::') msg = if args.first.is_a?(Protobuf::Message) args.shift elsif Object.const_defined?(msg_class_name, false) msg_class = Object.const_get(msg_class_name) msg_class.new(*args) 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 raise ArgumentError, "#{args} should be be a Hash or Protobuf Message." end response = send_rpc_request(rpc_method, msg, serializer: :JSON) if response.class.name == msg_class_name.pluralize m = self.class.name.demodulize.underscore.pluralize.to_sym response.respond_to?(m) ? response.public_send(m) : response else response end end end |
Instance Method Details
#check_response_error(res, raise_error: true) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/protobuf/rpc/clients/base.rb', line 42 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.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.) raise_error(error, raise_error) else res end end |
#internal_client ⇒ Object
22 23 24 |
# File 'lib/protobuf/rpc/clients/base.rb', line 22 def internal_client @service.client(host: host, port: port, timeout: timeout) end |
#send_rpc_request(method, msg, serializer:) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/protobuf/rpc/clients/base.rb', line 26 def send_rpc_request(method, msg, serializer:) res = nil internal_client.send(method, Serializer.dump(msg, serializer: serializer)) do |c| c.on_success do || res = Protobuf::Rpc::Serializer.load() end c.on_failure do |error| res = error end end check_response_error(res) end |