Class: Gruf::Controllers::Base

Inherits:
Object
  • Object
show all
Includes:
Errors::Helpers
Defined in:
lib/gruf/controllers/base.rb

Overview

Base controller object for Gruf gRPC requests

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Errors::Helpers

#fail!

Constructor Details

#initialize(method_key:, service:, rpc_desc:, active_call:, message:) ⇒ Base

Initialize the controller within the given request context

Parameters:

  • method_key (Symbol)

    The gRPC method that this controller relates to

  • service (GRPC::GenericService)

    The gRPC service stub for this controller

  • rpc_desc (GRPC::RpcDesc)

    The RPC descriptor for this service method

  • active_call (GRPC::ActiveCall)

    The gRPC ActiveCall object

  • message (Google::Protobuf::MessageExts)

    The incoming protobuf request message



48
49
50
51
52
53
54
55
56
57
58
# 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

Class Attribute Details

.bound_serviceObject (readonly)

Returns the value of attribute bound_service.



36
37
38
# File 'lib/gruf/controllers/base.rb', line 36

def bound_service
  @bound_service
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



32
33
34
# File 'lib/gruf/controllers/base.rb', line 32

def error
  @error
end

#requestObject (readonly)

Returns the value of attribute request.



30
31
32
# File 'lib/gruf/controllers/base.rb', line 30

def request
  @request
end

Class Method Details

.bind(service) ⇒ Object

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

Parameters:

  • service (GRPC::GenericService)

    The name of the service to bind this controller to



65
66
67
68
69
70
# File 'lib/gruf/controllers/base.rb', line 65

def self.bind(service)
  service_class = service.name.constantize
  Gruf.services << service_class
  @bound_service = service_class
  ServiceBinder.new(service_class).bind!(self)
end

Instance Method Details

#call(method_key, &block) ⇒ Object

Call a method on this controller

Parameters:

  • method_key (Symbol)

    The name of the gRPC service method being called as a Symbol

  • &block (block)

    The passed block for executing the method



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/gruf/controllers/base.rb', line 89

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) ⇒ Object

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

Parameters:

  • method_key (Symbol)

    The name of the gRPC service method being called as a Symbol

  • &block (block)

    The passed block for executing the method



79
80
81
# File 'lib/gruf/controllers/base.rb', line 79

def process_action(method_key, &block)
  send(method_key, &block)
end