Class: Google::Gax::PathTemplate

Inherits:
Object
  • Object
show all
Defined in:
lib/google/gax/path_template.rb

Overview

PathTemplate parses and format resource names

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ PathTemplate

Returns a new instance of PathTemplate.



133
134
135
136
137
# File 'lib/google/gax/path_template.rb', line 133

def initialize(data)
  parser = PathParse.new(PathLex.new)
  @segments = parser.parse(data)
  @size = parser.segment_count
end

Instance Attribute Details

#segmentsObject (readonly)

Returns the value of attribute segments.



131
132
133
# File 'lib/google/gax/path_template.rb', line 131

def segments
  @segments
end

#sizeObject (readonly)

Returns the value of attribute size.



131
132
133
# File 'lib/google/gax/path_template.rb', line 131

def size
  @size
end

Class Method Details

.format_segments(*segments) ⇒ String

Formats segments as a string.

Parameters:

  • The (Array<Segments>)

    segments to be formatted

Returns:

  • (String)

    the formatted output



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/google/gax/path_template.rb', line 144

def self.format_segments(*segments)
  template = ''
  slash = true
  segments.each do |segment|
    if segment.kind == TERMINAL
      template += '/' if slash
      template += segment.literal.to_s
      next
    end
    slash = true
    if segment.kind == BINDING
      template += "/{#{segment.literal}="
      slash = false
    else
      template += "#{segment.literal}}"
    end
  end
  template[1, template.length] # exclude the initial slash
end

Instance Method Details

#match(path) ⇒ Hash

Matches a fully qualified path template string.

Parameters:

  • path (String)

    A fully qualified path template string.

Returns:

  • (Hash)

    Var names to matched binding values.

Raises:

  • (ArgumentError)

    If path can’t be matched to the template.



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
# File 'lib/google/gax/path_template.rb', line 199

def match(path)
  that = path.split('/')
  current_var = nil
  bindings = {}
  segment_count = @size
  i = 0
  @segments.each do |segment|
    break if i >= that.size
    if segment.kind == TERMINAL
      if segment.literal == '*'
        bindings[current_var] = that[i]
        i += 1
      elsif segment.literal == '**'
        size = that.size - segment_count + 1
        segment_count += size - 1
        bindings[current_var] = that[i, size].join('/')
        i += size
      elsif segment.literal != that[i]
        throw ArgumentError.new(
          "mismatched literal: '#{segment.literal}' != '#{that[i]}'"
        )
      else
        i += 1
      end
    elsif segment.kind == BINDING
      current_var = segment.literal
    end
  end
  if i != that.size || i != segment_count
    throw ArgumentError.new(
      "match error: could not instantiate a path template from #{path}"
    )
  end
  bindings
end

#render(**bindings) ⇒ String

Renders a path template using the provided bindings.

Parameters:

  • binding (Hash)

    A mapping of var names to binding strings.

Returns:

  • (String)

    A rendered representation of this path template.

Raises:

  • (ArgumentError)

    If a key isn’t provided or if a sub-template can’t be parsed.



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/google/gax/path_template.rb', line 170

def render(**bindings)
  out = []
  binding = false
  @segments.each do |segment|
    if segment.kind == BINDING
      literal_sym = segment.literal.to_sym
      unless bindings.key?(literal_sym)
        msg = "Value for key #{segment.literal} is not provided"
        raise ArgumentError.new(msg)
      end
      out.concat(PathTemplate.new(bindings[literal_sym]).segments)
      binding = true
    elsif segment.kind == END_BINDING
      binding = false
    else
      next if binding
      out << segment
    end
  end
  path = self.class.format_segments(*out)
  match(path)
  path
end

#to_sObject



235
236
237
# File 'lib/google/gax/path_template.rb', line 235

def to_s
  self.class.format_segments(*@segments)
end