class Protobuf::Rpc::Service

Object to encapsulate the request/response types for a given service method

Constants

DEFAULT_HOST
DEFAULT_PORT

Attributes

host[W]
port[W]
env[R]
request[R]

Public Class Methods

client(options = {}) click to toggle source

Class Methods

Create a new client for the given service. See Client#initialize and ClientConnection::DEFAULT_OPTIONS for all available options.

# File lib/protobuf/rpc/service.rb, line 39
def self.client(options = {})
  ::Protobuf::Rpc::Client.new({ :service => self,
                                :host => host,
                                :port => port }.merge(options))
end
configure(config = {}) click to toggle source

Allows service-level configuration of location. Useful for system-startup configuration of a service so that any Clients using the Service.client sugar will not have to configure the location each time.

# File lib/protobuf/rpc/service.rb, line 50
def self.configure(config = {})
  self.host = config[:host] if config.key?(:host)
  self.port = config[:port] if config.key?(:port)
end
host() click to toggle source

The host location of the service.

# File lib/protobuf/rpc/service.rb, line 57
def self.host
  @host ||= DEFAULT_HOST
end
implemented_services() click to toggle source

An array of defined service classes that contain implementation code

# File lib/protobuf/rpc/service.rb, line 69
def self.implemented_services
  classes = (subclasses || []).select do |subclass|
    subclass.rpcs.any? do |(name, _)|
      subclass.method_defined? name
    end
  end

  classes.map(&:name)
end
located_at(location) click to toggle source

Shorthand call to configure, passing a string formatted as hostname:port e.g. 127.0.0.1:9933 e.g. localhost:0

# File lib/protobuf/rpc/service.rb, line 83
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
new(env) click to toggle source

Constructor!

Initialize a service with the rpc endpoint name and the bytes for the request.

# File lib/protobuf/rpc/service.rb, line 27
def initialize(env)
  @env = env.dup # Dup the env so it doesn't change out from under us
  @request = env.request
end
port() click to toggle source

The port of the service on the destination server.

# File lib/protobuf/rpc/service.rb, line 91
def self.port
  @port ||= DEFAULT_PORT
end
rpc(method, request_type, response_type, &options_block) click to toggle source

Define an rpc method with the given request and response types. This methods is only used by the generated service definitions and not useful for user code.

# File lib/protobuf/rpc/service.rb, line 105
def self.rpc(method, request_type, response_type, &options_block)
  rpcs[method] = RpcMethod.new(method, request_type, response_type, &options_block)
end
rpc_method?(name) click to toggle source

Check if the given method name is a known rpc endpoint.

# File lib/protobuf/rpc/service.rb, line 117
def self.rpc_method?(name)
  rpcs.key?(name)
end
rpcs() click to toggle source

Hash containing the set of methods defined via `rpc`.

# File lib/protobuf/rpc/service.rb, line 111
def self.rpcs
  @rpcs ||= {}
end

Public Instance Methods

call(method_name) click to toggle source
# File lib/protobuf/rpc/service.rb, line 121
def call(method_name)
  run_filters(method_name)
end
response() click to toggle source

Response object for this rpc cycle. Not assignable.

# File lib/protobuf/rpc/service.rb, line 127
def response
  @response ||= response_type.new
end
rpc_method?(name) click to toggle source

Convenience method to get back to class method.

# File lib/protobuf/rpc/service.rb, line 133
def rpc_method?(name)
  self.class.rpc_method?(name)
end
rpcs() click to toggle source

Convenience method to get back to class rpcs hash.

# File lib/protobuf/rpc/service.rb, line 139
def rpcs
  self.class.rpcs
end

Private Instance Methods

request_type() click to toggle source
# File lib/protobuf/rpc/service.rb, line 145
def request_type
  @request_type ||= env.request_type
end
respond_with(candidate) click to toggle source

Sugar to make an rpc method feel like a controller method. If this method is not called, the response will be the memoized object returned by the response reader.

# File lib/protobuf/rpc/service.rb, line 153
def respond_with(candidate)
  @response = candidate
end
Also aliased as: return_from_whence_you_came
response_type() click to toggle source
# File lib/protobuf/rpc/service.rb, line 158
def response_type
  @response_type ||= env.response_type
end
return_from_whence_you_came(candidate)
Alias for: respond_with
rpc_failed(message) click to toggle source

Automatically fail a service method.

# File lib/protobuf/rpc/service.rb, line 164
def rpc_failed(message)
  message = message.message if message.respond_to?(:message)
  fail RpcFailed, message
end