Class: Gapic::Model::Method::Routing

Inherits:
Object
  • Object
show all
Defined in:
lib/gapic/model/method/routing.rb

Overview

Routing headers info determined from the proto method

Defined Under Namespace

Classes: RoutingParameter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(routing, http) ⇒ Routing

Returns a new instance of Routing.

Parameters:



34
35
36
37
# File 'lib/gapic/model/method/routing.rb', line 34

def initialize routing, http
  @routing = routing
  @http = http
end

Instance Attribute Details

#routing::Google::Api::RoutingRule (readonly)

Returns Explicit routing annotation for the Api.

Returns:



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/gapic/model/method/routing.rb', line 27

class Routing
  attr_reader :routing

  ##
  # @param routing [::Google::Api::RoutingRule] Explict routing annotation for the Api
  # @param http [Gapic::Model::Method::HttpAnnotation] Model for the Http annotation
  #
  def initialize routing, http
    @routing = routing
    @http = http
  end

  ##
  # Whether routing parameters are specified
  #
  # @return [Boolean]
  def routing_params?
    explicit_params? || implicit_params?
  end

  ##
  # Whether an explicit routing annotation (`google.api.routing`) is present
  #
  # @return [Boolean]
  def explicit_annotation?
    !@routing.nil?
  end

  ##
  # Whether an annotation that might contain implicit routing (`google.api.http`) is present
  #
  # @return [Boolean]
  def implicit_annotation?
    !@http.nil?
  end

  ##
  # Whether explicit routing parameters are present
  #
  # @return [Boolean]
  def explicit_params?
    return false unless explicit_annotation?

    explicit_params.any?
  end

  ##
  # Explicit routing parameters, if present.
  # Grouped by the routing key, in order, with the extraction and matching information.
  #
  # @return [Hash<String, Array>]
  def explicit_params
    all_explicit_params.group_by(&:key).transform_values do |params|
      params.sort_by(&:order).to_a
    end
  end

  ##
  # Whether implict routing parameters from `google.api.http` annotation are present
  #
  # @return [Boolean]
  def implicit_params?
    @http.routing_params?
  end

  ##
  # Implicit routing parameters.
  # These strings are both field and routing header key names.
  #
  # @return [Array<String>]
  def implicit_params
    return {} unless implicit_params?
    @http.routing_params
  end

  ##
  # Full routing parameter information, including parsing order
  # and matching/extraction patterns and regexes.
  #
  # @!attribute [r] order
  #   @return [Integer] Order of the parameter in the annotation
  #
  # @!attribute [r] field
  #   @return [String] Field to extract the routing header from
  #
  # @!attribute [r] raw_template
  #   @return [String, nil] Raw template as given in the annotation
  #
  # @!attribute [r] path_template
  #   @return [String] 'Processed' template, handling such cases as
  #      empty or nameless template
  #
  # @!attribute [r] key
  #   @return [String] Name of the key to add to the routing header
  #
  # @!attribute [r] value_pattern
  #   @return [String] The pattern of the value to be extracted
  #
  # @!attribute [r] field_pattern
  #   @return [String] The pattern of the full field (simplified)
  #      The field as a whole should match this pattern
  #      This pattern is simplified, stipped of the names of the
  #      resource id segments.
  #      (e.g. `collections/{resource_id=foo/*}` => `collections/foo/*`)
  #
  # @!attribute [r] field_regex_str
  #   @return [String] The regex matching the `field_pattern`
  #      (the regex form of the simplified pattern)
  #
  # @!attribute [r] field_full_regex_str
  #   @return [String] The regex matching the full unsimplified field pattern
  #      (it will contain the named capture corresponding to the
  #       resource id segment name)
  #
  class RoutingParameter
    attr_reader :order
    attr_reader :field
    attr_reader :raw_template
    attr_reader :path_template
    attr_reader :key
    attr_reader :value_pattern
    attr_reader :field_pattern
    attr_reader :field_regex_str
    attr_reader :field_full_regex_str

    ##
    # @param routing_parameter [::Google::Api::RoutingParameter]
    #   Routing parameter annotation
    #
    # @param order [Integer]
    #   Order of this annotation among its peers
    def initialize routing_parameter, order
      @order = order
      @field = routing_parameter.field
      @raw_template = routing_parameter.path_template
      @path_template = infer_template @raw_template, @field
      @path_pattern = Gapic::PathPattern.parse @path_template

      resource_segment = @path_pattern.segments.find(&:resource_id_segment?)

      # Only one segment providing an argument and only one argument in the segment
      # (no `{foo}~{bar}` segments)
      valid = @path_pattern.segments.count(&:resource_id_segment?) == 1 &&
              resource_segment.arguments.count == 1

      unless valid
        error_text = create_invalid_error_text @path_pattern, @raw_template
        raise ModelError, error_text
      end

      @field_pattern = @path_pattern.simplified_pattern
      @field_full_regex_str =  to_field_pattern @path_pattern
      @field_regex_str = to_field_pattern Gapic::PathPattern.parse(@field_pattern)

      @key = resource_segment.arguments[0]
      @value_pattern = resource_segment.resource_patterns[0]
    end

    ##
    # Whether pattern matching is not needed
    # since the patterns allow all strings
    # @return [Boolean]
    def pattern_matching_not_needed?
      field_pattern == "**" && value_pattern == "**"
    end

    ##
    # Whether the value to be added to the routing header
    # is the value of the whole field
    # @return [Boolean]
    def value_is_full_field?
      @path_pattern.segments.count == 1
    end

    private

    # Makes a regex pattern match a field
    # - adds markers for the beginning and end of the string
    # - adds handling of an optional tail `/` if needed
    # @param pattern [String]
    # @return [String]
    def to_field_pattern pattern
      tail_slash_accept = if pattern.segments.last.simplified_pattern == "**"
                            ""
                          else
                            "/?"
                          end
      "^#{pattern.to_regex_str}#{tail_slash_accept}$"
    end

    # Converts path template simplified forms into canonical
    # ResourceId representations by adding a field as a Resource Id
    # @param template [String]
    # @param field [String]
    # @return [String]
    def infer_template template, field
      if template.nil? || template.empty?
        return "{#{field}=**}"
      end

      if template.strip == "**"
        return "{#{field}=**}"
      end

      if template.strip == "*"
        return "{#{field}=*}"
      end

      template
    end

    def create_invalid_error_text path_pattern, raw_template
      reason = if path_pattern.segments.count(&:resource_id_segment?).zero?
                 "it contains no ResourceId (e.g. `{foo=*}`) segments"
               elsif path_pattern.segments.count(&:resource_id_segment?) > 1
                 "it contains more than one ResourceId (e.g. `{foo=*}`) segments "
               else
                 "it contains a multivariate ResourceId segment (e.g. `{foo}~{bar}`)"
               end

      "A routing header parameter with the path_template #{raw_template}\n " \
          "is invalid: #{reason}"
    end
  end

  private

  # All explicit routing parameters in an array.
  # @return [Array<RoutingParameter>]
  def all_explicit_params
    return [] unless explicit_annotation?
    @routing.routing_parameters.each_with_index.map { |param, index| RoutingParameter.new param, index }.to_a
  end
end

Instance Method Details

#explicit_annotation?Boolean

Whether an explicit routing annotation (google.api.routing) is present

Returns:

  • (Boolean)


51
52
53
# File 'lib/gapic/model/method/routing.rb', line 51

def explicit_annotation?
  !@routing.nil?
end

#explicit_paramsHash<String, Array>

Explicit routing parameters, if present. Grouped by the routing key, in order, with the extraction and matching information.

Returns:

  • (Hash<String, Array>)


78
79
80
81
82
# File 'lib/gapic/model/method/routing.rb', line 78

def explicit_params
  all_explicit_params.group_by(&:key).transform_values do |params|
    params.sort_by(&:order).to_a
  end
end

#explicit_params?Boolean

Whether explicit routing parameters are present

Returns:

  • (Boolean)


67
68
69
70
71
# File 'lib/gapic/model/method/routing.rb', line 67

def explicit_params?
  return false unless explicit_annotation?

  explicit_params.any?
end

#implicit_annotation?Boolean

Whether an annotation that might contain implicit routing (google.api.http) is present

Returns:

  • (Boolean)


59
60
61
# File 'lib/gapic/model/method/routing.rb', line 59

def implicit_annotation?
  !@http.nil?
end

#implicit_paramsArray<String>

Implicit routing parameters. These strings are both field and routing header key names.

Returns:

  • (Array<String>)


97
98
99
100
# File 'lib/gapic/model/method/routing.rb', line 97

def implicit_params
  return {} unless implicit_params?
  @http.routing_params
end

#implicit_params?Boolean

Whether implict routing parameters from google.api.http annotation are present

Returns:

  • (Boolean)


88
89
90
# File 'lib/gapic/model/method/routing.rb', line 88

def implicit_params?
  @http.routing_params?
end

#routing_params?Boolean

Whether routing parameters are specified

Returns:

  • (Boolean)


43
44
45
# File 'lib/gapic/model/method/routing.rb', line 43

def routing_params?
  explicit_params? || implicit_params?
end