Class: Gritz::Router

Inherits:
Object
  • Object
show all
Defined in:
/home/runner/work/gritz/gritz/sources/gritz-core/lib/gritz/router.rb

Overview

Resolves generated service descriptors to controller actions at boot.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controllers:, strict: false, logger: nil) ⇒ Router

Returns a new instance of Router.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File '/home/runner/work/gritz/gritz/sources/gritz-core/lib/gritz/router.rb', line 9

def initialize(controllers:, strict: false, logger: nil)
  @routes = {}
  services = {}
  missing = []
  controllers.each do |controller|
    service = controller.service_class
    raise ArgumentError, "#{controller} has no bound service" unless service
    raise ArgumentError, "service #{service.service_name} is already bound" if services.key?(service.service_name)

    services[service.service_name] = controller
    service.rpc_descs.each do |name, rpc|
      input_stream = rpc.client_streamer? || rpc.bidi_streamer?
      output_stream = rpc.server_streamer? || rpc.bidi_streamer?
      route = MethodDescriptor.new(service: service.service_name, name:, input_type: input_stream ? rpc.input.type : rpc.input,
                                   output_type: output_stream ? rpc.output.type : rpc.output, client_streaming: input_stream,
                                   server_streaming: output_stream, controller:, service_class: service)
      @routes[route.full_name] = route
      missing << "#{controller}##{route.action}" unless controller.action_defined?(route.action)
    end
  end
  unless missing.empty?
    message = "unimplemented RPC actions: #{missing.join(', ')}"
    raise ArgumentError, message if strict

    logger&.warn(message)
  end
  @routes.freeze
end

Instance Attribute Details

#routes ⇒ Object (readonly)



7
8
9
# File '/home/runner/work/gritz/gritz/sources/gritz-core/lib/gritz/router.rb', line 7

def routes
  @routes
end

Instance Method Details

#resolve(full_name) ⇒ Object



38
39
40
# File '/home/runner/work/gritz/gritz/sources/gritz-core/lib/gritz/router.rb', line 38

def resolve(full_name)
  routes.fetch(full_name) { raise Errors::Unimplemented, "unknown RPC: #{full_name}" }
end