Class: Gruf::Controllers::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/gruf/controllers/request.rb

Overview

Encapsulates a request for a controller

Defined Under Namespace

Classes: Type

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Initialize an inbound controller request object

Parameters:

  • method_key (Symbol)

    The method symbol of the RPC method being executed

  • service (Class)

    The class of the service being executed against

  • rpc_desc (GRPC::RpcDesc)

    The RPC descriptor of the call

  • active_call (GRPC::ActiveCall)

    The restricted view of the call

  • message (Object|Google::Protobuf::MessageExts)

    The protobuf message (or messages) of the request



73
74
75
76
77
78
79
80
81
# File 'lib/gruf/controllers/request.rb', line 73

def initialize(method_key:, service:, rpc_desc:, active_call:, message:)
  @method_key = method_key
  @service = service
  @active_call = active_call
  @message = message
  @rpc_desc = rpc_desc
  @type = Type.new(rpc_desc)
  @context = ::ActiveSupport::HashWithIndifferentAccess.new
end

Instance Attribute Details

#active_callObject (readonly)

Returns the value of attribute active_call.



29
30
31
# File 'lib/gruf/controllers/request.rb', line 29

def active_call
  @active_call
end

#contextObject (readonly)

Returns the value of attribute context.



43
44
45
# File 'lib/gruf/controllers/request.rb', line 43

def context
  @context
end

#messageObject (readonly)

Returns the value of attribute message.



26
27
28
# File 'lib/gruf/controllers/request.rb', line 26

def message
  @message
end

#method_keyObject (readonly)

Returns the value of attribute method_key.



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

def method_key
  @method_key
end

#serviceObject (readonly)

Returns the value of attribute service.



38
39
40
# File 'lib/gruf/controllers/request.rb', line 38

def service
  @service
end

#typeObject (readonly)

Returns the value of attribute type.



35
36
37
# File 'lib/gruf/controllers/request.rb', line 35

def type
  @type
end

Instance Method Details

#messagesEnumerable<Object>, Object

Return all messages for this request, properly handling different request types

Returns:

  • (Enumerable<Object>)

    All messages for this request

  • (Object)

    If a bidi streamed request, will return the message object



122
123
124
125
126
127
128
129
130
131
132
# File 'lib/gruf/controllers/request.rb', line 122

def messages
  if client_streamer?
    # rubocop:disable Style/ExplicitBlockArgument
    @message.call { |msg| yield msg }
    # rubocop:enable Style/ExplicitBlockArgument
  elsif bidi_streamer?
    @message
  else
    [@message]
  end
end

#method_nameString

Parse the method signature into a service.method name format

Returns:

  • (String)

    The parsed service method name



112
113
114
# File 'lib/gruf/controllers/request.rb', line 112

def method_name
  "#{service_key}.#{@method_key}"
end

#request_classClass

Returns The class of the request message.

Returns:

  • (Class)

    The class of the request message



103
104
105
# File 'lib/gruf/controllers/request.rb', line 103

def request_class
  @rpc_desc.input
end

#response_classClass

Returns The class of the response message.

Returns:

  • (Class)

    The class of the response message



96
97
98
# File 'lib/gruf/controllers/request.rb', line 96

def response_class
  @rpc_desc.output
end

#service_keyString

Returns the service name as a translated name separated by periods. Strips the superfluous “Service” suffix from the name

Returns:

  • (String)

    The mapped service key



89
90
91
# File 'lib/gruf/controllers/request.rb', line 89

def service_key
  @service.name.to_s.underscore.tr('/', '.').gsub('.service', '')
end