class Gruf::Controllers::Base

Base controller object for Gruf gRPC requests

Attributes

bound_service[R]

@var [GRPC::GenericService] bound_service

error[R]

@var [Gruf::Error] error

request[R]

@var [Gruf::Controller::Request] request

Public Class Methods

bind(service) click to toggle source

Bind the controller to the given service and add it to the service registry

@param [GRPC::GenericService] service The name of the service to bind this controller to

# File lib/gruf/controllers/base.rb, line 65
def self.bind(service)
  service_class = service.name.constantize
  Gruf.services << service_class
  # rubocop:disable ThreadSafety/InstanceVariableInClassMethod
  @bound_service = service_class
  # rubocop:enable ThreadSafety/InstanceVariableInClassMethod
  ServiceBinder.new(service_class).bind!(self)
end
new(method_key:, service:, rpc_desc:, active_call:, message:) click to toggle source

Initialize the controller within the given request context

@param [Symbol] method_key The gRPC method that this controller relates to @param [GRPC::GenericService] service The gRPC service stub for this controller @param [GRPC::RpcDesc] rpc_desc The RPC descriptor for this service method @param [GRPC::ActiveCall] active_call The gRPC ActiveCall object @param [Google::Protobuf::MessageExts] message The incoming protobuf request message

# File lib/gruf/controllers/base.rb, line 48
def initialize(method_key:, service:, rpc_desc:, active_call:, message:)
  @request = Request.new(
    method_key: method_key,
    service: service,
    rpc_desc: rpc_desc,
    active_call: active_call,
    message: message
  )
  @error = Gruf::Error.new
  @interceptors = Gruf.interceptors.prepare(@request, @error)
end

Public Instance Methods

call(method_key, &block) click to toggle source

Call a method on this controller

@param [Symbol] method_key The name of the gRPC service method being called as a Symbol @param [block] &block The passed block for executing the method

# File lib/gruf/controllers/base.rb, line 91
def call(method_key, &block)
  Interceptors::Context.new(@interceptors).intercept! do
    process_action(method_key, &block)
  end
rescue GRPC::BadStatus
  raise # passthrough, to be caught by Gruf::Interceptors::Timer
rescue GRPC::Core::CallError, StandardError => e # CallError is not a StandardError
  set_debug_info(e.message, e.backtrace) if Gruf.backtrace_on_error
  error_message = Gruf.use_exception_message ? e.message : Gruf.internal_error_message
  fail!(:internal, :unknown, error_message)
end
process_action(method_key, &block) click to toggle source

Call a method on this controller. Override this in a subclass to modify the behavior around processing a method

@param [Symbol] method_key The name of the gRPC service method being called as a Symbol @param [block] &block The passed block for executing the method

# File lib/gruf/controllers/base.rb, line 81
def process_action(method_key, &block)
  send(method_key, &block)
end