class Service
Represents a concrete service implementation that uses an Interface. Subclass this and implement the RPC methods defined in the interface. Services are registered with DispatcherMiddleware for routing.
class GreeterService < Async::GRPC::Service
def say_hello(input, output, call)
request = input.read
reply = Hello::HelloReply.new(message: "Hello, #request.name!")
output.write(reply)
end
def xml_parser(input, output, call)
# Implementation using explicit method name
end
end
Register with dispatcher:
dispatcher = DispatcherMiddleware.new dispatcher.register("hello.Greeter", GreeterService.new(GreeterInterface, "hello.Greeter")) server = Async::HTTP::Server.for(endpoint, dispatcher)
Example: Example service implementation:
class GreeterInterface < Protocol::GRPC::Interface
rpc :SayHello, request_class: Hello::HelloRequest, response_class: Hello::HelloReply
# Optional: explicit method name for edge cases
rpc :XMLParser, request_class: Hello::ParseRequest, response_class: Hello::ParseReply,
method: :xml_parser # Explicit method name (otherwise would be :xmlparser)
end
Signature
Definitions
def initialize(interface_class, service_name)
Initialize a new service instance.
Signature
-
parameter
interface_classClass The interface class (subclass of Protocol::GRPC::Interface)
-
parameter
service_nameString The service name (e.g., "hello.Greeter")
Implementation
def initialize(interface_class, service_name)
@interface_class = interface_class
@service_name = service_name
end
attr_reader :interface_class
Signature
-
attribute
Class The interface class.
attr_reader :service_name
Signature
-
attribute
String The service name.
def rpc_descriptions
Get RPC descriptions from the interface class. Converts Interface RPC definitions (PascalCase) to rpc_descriptions format. Maps gRPC method names (PascalCase) to Ruby method names (snake_case).
Signature
-
returns
Hash RPC descriptions hash keyed by PascalCase method name
Implementation
def rpc_descriptions
descriptions = {}
@interface_class.rpcs.each do |pascal_case_name, rpc|
# rpc.method is always set (either explicitly or auto-converted in Interface.rpc)
descriptions[pascal_case_name.to_s] = rpc
end
descriptions
end