Class: GrpcRest::GeneratedMethod

Inherits:
Object
  • Object
show all
Defined in:
lib/generator/method.rb

Defined Under Namespace

Classes: PathInfo

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(proto_method) ⇒ GeneratedMethod

Returns a new instance of GeneratedMethod.

Parameters:

  • proto_method (Google::Protobuf::MethodDescriptor)


10
11
12
13
14
15
16
# File 'lib/generator/method.rb', line 10

def initialize(proto_method)
  @method = proto_method
  @rest_options = extract_rest_options
  @options = extract_options
  @http_method, @path = method_and_path
  @path_info = extract_path_info
end

Instance Attribute Details

#http_methodObject

Returns the value of attribute http_method.



7
8
9
# File 'lib/generator/method.rb', line 7

def http_method
  @http_method
end

#pathObject

Returns the value of attribute path.



7
8
9
# File 'lib/generator/method.rb', line 7

def path
  @path
end

#path_infoObject

Returns the value of attribute path_info.



7
8
9
# File 'lib/generator/method.rb', line 7

def path_info
  @path_info
end

#rest_optionsObject

Returns the value of attribute rest_options.



7
8
9
# File 'lib/generator/method.rb', line 7

def rest_options
  @rest_options
end

Instance Method Details

#extract_optionsGoogle::Api::HttpRule

Returns:

  • (Google::Api::HttpRule)


45
46
47
48
49
50
# File 'lib/generator/method.rb', line 45

def extract_options
  return nil if @method.options.nil?

  extension = Google::Protobuf::DescriptorPool.generated_pool.lookup('google.api.http')
  extension.get(@method.options)
end

#extract_path_infoPathInfo

Returns:



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/generator/method.rb', line 79

def extract_path_info
  @path.scan(/\{(.*?)}/).map do |match|
    name = match[0]
    val = ''
    equal = name.index('=')
    if equal
      val = name[equal+1..]
      name = name[0..equal - 1]
    end
    PathInfo.new(
      name: name,
      val: val,
      split_name: name.split('.')
    )
  end
end

#extract_rest_optionsHash

Returns:

  • (Hash)


53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/generator/method.rb', line 53

def extract_rest_options
  return nil if @method.options.nil?

  extension = Google::Protobuf::DescriptorPool.generated_pool
                                              .lookup('grpc.gateway.protoc_gen_openapiv2.options.openapiv2_operation')
  result = extension.get(@method.options)
  return {} if result.nil?

  emit_defaults = result.extensions['x-grpc-rest-emit-defaults']&.bool_value || false
  {
    emit_defaults: emit_defaults
  }
end

#method_and_pathString

Returns:

  • (String, String)


68
69
70
71
72
73
74
75
76
# File 'lib/generator/method.rb', line 68

def method_and_path
  return if @options.nil?

  if @options.pattern == :custom
    [@options.custom.kind, @options.custom.path]
  else
    [@options.pattern, @options.send(@options.pattern)]
  end
end

#nameString

Returns:

  • (String)


21
# File 'lib/generator/method.rb', line 21

def name = @method.name.underscore

#option_bodyString

Returns:

  • (String)


19
20
# File 'lib/generator/method.rb', line 19

def option_body = @options&.body
# @return [String]

#request_typeString

Returns:

  • (String)


24
25
26
# File 'lib/generator/method.rb', line 24

def request_type
  @method.input_type.msgclass
end

#sanitized_pathString

Returns:

  • (String)


29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/generator/method.rb', line 29

def sanitized_path
  path = @path
  re = /\{(.*?)}/
  matches = path.scan(re)
  matches.each do |match|
    repl = match[0]
    equal = repl.index('=')
    repl = repl[0...equal] if equal
    dot = repl.index('.')
    repl = repl[(dot + 1)..] if dot
    path = path.sub("{#{match[0]}}", "*#{repl}")
  end
  path
end