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

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)
  • service (GRPC::GenericService)
  • rpc_desc (GRPC::RpcDesc)
  • active_call (GRPC::ActiveCall)
  • message (Google::Protobuf::MessageExts)


41
42
43
44
45
46
47
48
49
50
51
# File 'lib/gruf/controllers/base.rb', line 41

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

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



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

def error
  @error
end

#requestObject (readonly)

Returns the value of attribute request.



28
29
30
# File 'lib/gruf/controllers/base.rb', line 28

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)


58
59
60
61
# File 'lib/gruf/controllers/base.rb', line 58

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

Instance Method Details

#call(method_key, &block) ⇒ Object

Call a method on this controller

Parameters:

  • method_key (Symbol)


68
69
70
71
72
73
74
75
76
77
78
# File 'lib/gruf/controllers/base.rb', line 68

def call(method_key, &block)
  Interceptors::Context.new(@interceptors).intercept! do
    send(method_key, &block)
  end
rescue GRPC::BadStatus
  raise # passthrough
rescue StandardError => e
  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