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.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(rpc, request_bytes)
Initialize a service with the rpc endpoint name and the bytes
def initialize(rpc, request_bytes) @rpc = rpc @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 ||= rpcs[@rpc].request_type.new.parse_from_string(@request_bytes) rescue => e raise BadRequestProto, "Unable to parse request: #{e.message}" 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 ||= rpcs[@rpc].response_type.new 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