class Stub
Represents a client stub that provides method-based access to RPC calls.
Created by calling Async::GRPC::Client#stub.
Definitions
def initialize(client, interface)
Initialize a new stub instance.
Signature
-
parameter
clientAsync::GRPC::Client The gRPC client
-
parameter
interfaceProtocol::GRPC::Interface The interface instance
Implementation
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
attr_reader :interface
Signature
-
attribute
Protocol::GRPC::Interface The interface instance.
def method_missing(method_name, *args, **options, &block)
Dynamically handle method calls for RPC methods. Uses snake_case method names (Ruby convention).
Signature
-
parameter
method_nameSymbol The method name to call (snake_case)
-
parameter
argsArray Positional arguments (first is the request message)
-
parameter
optionsHash Keyword arguments (metadata, timeout, encoding)
-
yields
{|input, output| ...} Block for streaming calls
-
returns
Object | Protocol::GRPC::Body::ReadableBody Response message or readable body
-
raises
NoMethodError If the method is not found
Implementation
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 from options:
metadata = options.delete(:metadata) || {}
timeout = options.delete(:timeout)
encoding = options.delete(:encoding)
# Delegate to client.invoke with PascalCase method name (for interface lookup):
@client.invoke(@interface, interface_method_name, request, metadata: metadata, timeout: timeout, encoding: encoding, &block)
else
super
end
end
def respond_to_missing?(method_name, include_private = false)
Check if the stub responds to the given method.
Signature
-
parameter
method_nameSymbol The method name to check
-
parameter
include_privateBoolean Whether to include private methods
-
returns
Boolean trueif the method exists,falseotherwise
Implementation
def respond_to_missing?(method_name, include_private = false)
rpc, _ = lookup_rpc(method_name)
return true if rpc
super
end
def lookup_rpc(method_name)
Look up RPC definition for a method name.
Signature
-
parameter
method_nameSymbol The method name to look up (snake_case)
-
returns
Array(Protocol::GRPC::RPC, Symbol) | Array(Nil, Nil) RPC definition and PascalCase method name, or nil if not found
Implementation
def lookup_rpc(method_name)
if @rpcs_by_method.key?(method_name)
rpc = @rpcs_by_method[method_name]
return [rpc, rpc.name]
end
[nil, nil]
end