Class: Gruf::Controllers::Base
- Inherits:
-
Object
- Object
- Gruf::Controllers::Base
- Includes:
- Errors::Helpers
- Defined in:
- lib/gruf/controllers/base.rb
Overview
Base controller object for Gruf gRPC requests
Direct Known Subclasses
Class Attribute Summary collapse
-
.bound_service ⇒ Object
readonly
Returns the value of attribute bound_service.
Instance Attribute Summary collapse
-
#error ⇒ Object
readonly
Returns the value of attribute error.
-
#request ⇒ Object
readonly
Returns the value of attribute request.
Class Method Summary collapse
-
.bind(service) ⇒ Object
Bind the controller to the given service and add it to the service registry.
Instance Method Summary collapse
-
#call(method_key, &block) ⇒ Object
Call a method on this controller.
-
#initialize(method_key:, service:, rpc_desc:, active_call:, message:) ⇒ Base
constructor
Initialize the controller within the given request context.
-
#process_action(method_key, &block) ⇒ Object
Call a method on this controller.
Methods included from Errors::Helpers
Constructor Details
#initialize(method_key:, service:, rpc_desc:, active_call:, message:) ⇒ Base
Initialize the controller within the given request context
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: ) @error = Gruf::Error.new @interceptors = Gruf.interceptors.prepare(@request, @error) end |
Class Attribute Details
.bound_service ⇒ Object (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
#error ⇒ Object (readonly)
Returns the value of attribute error.
31 32 33 |
# File 'lib/gruf/controllers/base.rb', line 31 def error @error end |
#request ⇒ Object (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
65 66 67 68 69 70 71 72 73 |
# File 'lib/gruf/controllers/base.rb', line 65 def self.bind(service) service_class = service.name.constantize ::Gruf.logger.debug "[gruf] Binding #{service_class} to #{name}" ::Gruf.services << service_class # rubocop:disable ThreadSafety/InstanceVariableInClassMethod @bound_service = service_class # rubocop:enable ThreadSafety/InstanceVariableInClassMethod ServiceBinder.bind!(service: service_class, controller: self) end |
Instance Method Details
#call(method_key, &block) ⇒ Object
Call a method on this controller
92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/gruf/controllers/base.rb', line 92 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., e.backtrace) if Gruf.backtrace_on_error = Gruf. ? e. : Gruf. fail!(:internal, :unknown, ) 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
82 83 84 |
# File 'lib/gruf/controllers/base.rb', line 82 def process_action(method_key, &block) send(method_key, &block) end |