class Protobuf::Rpc::Service
def self.client(options = {})
for all available options.
See Client#initialize and ClientConnection::DEFAULT_OPTIONS
Create a new client for the given service.
def self.client(options = {}) ::Protobuf::Rpc::Client.new({ :service => self, :host => host, :port => port }.merge(options)) end
def self.configure(config = {})
will not have to configure the location each time.
so that any Clients using the Service.client sugar
Useful for system-startup configuration of a service
Allows service-level configuration of location.
def self.configure(config = {}) self.host = config[:host] if config.key?(:host) self.port = config[:port] if config.key?(:port) end
def self.host
The host location of the service.
def self.host @_host ||= DEFAULT_HOST end
def self.host=(new_host)
The host location setter.
def self.host=(new_host) @_host = new_host end
def self.implemented_services
An array of defined service classes that contain implementation
def self.implemented_services classes = (self.subclasses || []).select do |subclass| subclass.rpcs.any? do |(name, _)| subclass.method_defined? name end end classes.map(&:name) end
def self.located_at(location)
e.g. localhost:0
e.g. 127.0.0.1:9933
Shorthand call to configure, passing a string formatted as hostname:port
def self.located_at(location) return if location.nil? || location.downcase.strip !~ /.+:\d+/ host, port = location.downcase.strip.split ':' configure(:host => host, :port => port.to_i) end
def self.port
The port of the service on the destination server.
def self.port @_port ||= DEFAULT_PORT end
def self.port=(new_port)
The port location setter.
def self.port=(new_port) @_port = new_port end
def self.rpc(method, request_type, response_type)
and not useful for user code.
This methods is only used by the generated service definitions
Define an rpc method with the given request and response types.
def self.rpc(method, request_type, response_type) rpcs[method] = RpcMethod.new(method, request_type, response_type) end
def self.rpc_method?(name)
Check if the given method name is a known rpc endpoint.
def self.rpc_method?(name) rpcs.key?(name) end
def self.rpcs
Hash containing the set of methods defined via `rpc`.
def self.rpcs @_rpcs ||= {} end
def callable_rpc_method(method_name)
is why we wrap the method call).
The returned lambda is expected to be called at a later time (which
to invoke the specified rpc method. Facilitates callback dispatch.
Get a callable object that will be used by the dispatcher
def callable_rpc_method(method_name) lambda { run_filters(method_name) } end
def initialize(method_name, request_bytes, client_host = nil)
Initialize a service with the rpc endpoint name and the bytes
def initialize(method_name, request_bytes, client_host = nil) @method_name = method_name @client_host = client_host @_request_bytes = request_bytes end
def on_rpc_failed(callable)
Register a failure callback for use when rpc_failed is invoked.
def on_rpc_failed(callable) @_rpc_failed_callback ||= callable end
def request
Request object for this rpc cycle. Not assignable.
def request @_request ||= if @_request_bytes.present? request_type.decode(@_request_bytes) else request_type.new end rescue => e raise BadRequestProto, "Unable to parse request: #{e.message}" end
def request_type
def request_type @_request_type ||= rpcs[@method_name].request_type end
def respond_with(candidate)
object returned by the response reader.
If this method is not called, the response will be the memoized
Sugar to make an rpc method feel like a controller method.
def respond_with(candidate) @_response = candidate end
def response
Response object for this rpc cycle. Not assignable.
def response @_response ||= response_type.new end
def response_type
def response_type @_response_type ||= rpcs[@method_name].response_type end
def rpc
Renamed attribute from prior implementaiton due to lack of clarity
def rpc if ::Protobuf.print_deprecation_warnings? $stderr.puts <<-ERROR [WARNING] Service#rpc method has been deprecated and will be removed in a future version of protobuf. ERROR end @method_name end
def rpc_failed(message)
Automatically fail a service method.
def rpc_failed(message) @_rpc_failed_callback.call(message) end
def rpc_method?(name)
Convenience method to get back to class method.
def rpc_method?(name) self.class.rpc_method?(name) end
def rpcs
Convenience method to get back to class rpcs hash.
def rpcs self.class.rpcs end