class Google::Gax::PathTemplate

PathTemplate parses and format resource names

Attributes

segments[R]
size[R]

Public Class Methods

format_segments(*segments) click to toggle source

Formats segments as a string.

@param [Array<Segments>]

The segments to be formatted

@return [String] the formatted output

# 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
new(data) click to toggle source
# 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

Public Instance Methods

match(path) click to toggle source

Matches a fully qualified path template string. @param path [String]

A fully qualified path template string.

@return [Hash] Var names to matched binding values. @raise [ArgumentError] If path can't be matched to the template.

# File lib/google/gax/path_template.rb, line 197
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) click to toggle source

Renders a path template using the provided bindings. @param binding [Hash]

A mapping of var names to binding strings.

@return [String] A rendered representation of this path template. @raise [ArgumentError] If a key isn't provided or if a sub-template

can't be parsed.
# 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 << bindings[literal_sym]
      binding = true
    elsif segment.kind == END_BINDING
      binding = false
    else
      next if binding
      out << segment.literal.to_s
    end
  end
  out.join('/')
end
to_s() click to toggle source
# File lib/google/gax/path_template.rb, line 233
def to_s
  self.class.format_segments(*@segments)
end