Class: Async::GRPC::Stub

Inherits:
Object
  • Object
show all
Defined in:
lib/async/grpc/stub.rb

Overview

Represents a client stub that provides method-based access to RPC calls. Created by calling Client#stub.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, interface) ⇒ Stub

Initialize a new stub instance.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/async/grpc/stub.rb', line 16

def initialize(client, interface)
	@client = client
	@interface = interface
	@interface_class = interface.class
	# Cache RPCs indexed by snake_case method name (default)
	@rpcs_by_method = {}
	
	@interface_class.rpcs.each do |pascal_case_name, rpc|
		# rpc.method is always set (either explicit or auto-converted from PascalCase)
		snake_case_method = rpc.method
		
		# Index by snake_case method name, storing RPC (which includes name field)
		@rpcs_by_method[snake_case_method] = rpc
	end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, **options, &block) ⇒ Object

Dynamically handle method calls for RPC methods. Uses snake_case method names (Ruby convention).



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/async/grpc/stub.rb', line 43

def method_missing(method_name, *args, **options, &block)
	rpc, interface_method_name = lookup_rpc(method_name)
	
	if rpc
		# Extract request from args (first positional argument):
		request = args.first
		
		# Extract metadata, timeout, encoding and initial messages from options:
		 = options.delete(:metadata) || {}
		timeout = options.delete(:timeout)
		encoding = options.delete(:encoding)
		initial = options.delete(:initial)
		
		# Delegate to client.invoke with PascalCase method name (for interface lookup):
		@client.invoke(@interface, interface_method_name, request, metadata: , timeout: timeout, encoding: encoding, initial: initial, &block)
	else
		super
	end
end

Instance Attribute Details

#interfaceObject (readonly)

Returns the value of attribute interface.



33
34
35
# File 'lib/async/grpc/stub.rb', line 33

def interface
  @interface
end

#The interface instance.(interfaceinstance.) ⇒ Object (readonly)



33
# File 'lib/async/grpc/stub.rb', line 33

attr_reader :interface

Instance Method Details

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Check if the stub responds to the given method.

Returns:

  • (Boolean)


67
68
69
70
71
72
# File 'lib/async/grpc/stub.rb', line 67

def respond_to_missing?(method_name, include_private = false)
	rpc, _ = lookup_rpc(method_name)
	return true if rpc
	
	super
end