Class: Gapic::PathTemplate::Parser
- Inherits:
-
Object
- Object
- Gapic::PathTemplate::Parser
- Defined in:
- lib/gapic/path_template/parser.rb
Overview
A URI path template parser.
Instance Attribute Summary collapse
-
#path_template ⇒ String
readonly
The URI path template to be parsed.
-
#segments ⇒ Array<Segment|String>
readonly
The segments of the parsed URI path template.
Instance Method Summary collapse
-
#initialize(path_template) ⇒ Parser
constructor
Create a new URI path template parser.
Constructor Details
#initialize(path_template) ⇒ Parser
Create a new URI path template parser.
46 47 48 49 |
# File 'lib/gapic/path_template/parser.rb', line 46 def initialize path_template @path_template = path_template @segments = parse! path_template end |
Instance Attribute Details
#path_template ⇒ String (readonly)
Returns The URI path template to be parsed.
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 |
# File 'lib/gapic/path_template/parser.rb', line 30 class Parser # @private # /((?<positional>\*\*?)|{(?<name>[^\/]+?)(?:=(?<template>.+?))?})/ PATH_TEMPLATE = %r{ ( (?<positional>\*\*?) | {(?<name>[^\/]+?)(?:=(?<template>.+?))?} ) }x.freeze attr_reader :path_template, :segments # Create a new URI path template parser. # # @param path_template [String] The URI path template to be parsed. def initialize path_template @path_template = path_template @segments = parse! path_template end protected def parse! path_template # segments contain either Strings or segment objects segments = [] segment_pos = 0 while (match = PATH_TEMPLATE.match path_template) # The String before the match needs to be added to the segments segments << match.pre_match unless match.pre_match.empty? segment, segment_pos = segment_and_pos_from_match match, segment_pos segments << segment path_template = match.post_match end # Whatever String is unmatched needs to be added to the segments segments << path_template unless path_template.empty? segments end def segment_and_pos_from_match match, pos if match[:positional] [Segment.new(pos, match[:positional]), pos + 1] else [Segment.new(match[:name], match[:template]), pos] end end end |
#segments ⇒ Array<Segment|String> (readonly)
Returns The segments of the parsed URI path template.
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 |
# File 'lib/gapic/path_template/parser.rb', line 30 class Parser # @private # /((?<positional>\*\*?)|{(?<name>[^\/]+?)(?:=(?<template>.+?))?})/ PATH_TEMPLATE = %r{ ( (?<positional>\*\*?) | {(?<name>[^\/]+?)(?:=(?<template>.+?))?} ) }x.freeze attr_reader :path_template, :segments # Create a new URI path template parser. # # @param path_template [String] The URI path template to be parsed. def initialize path_template @path_template = path_template @segments = parse! path_template end protected def parse! path_template # segments contain either Strings or segment objects segments = [] segment_pos = 0 while (match = PATH_TEMPLATE.match path_template) # The String before the match needs to be added to the segments segments << match.pre_match unless match.pre_match.empty? segment, segment_pos = segment_and_pos_from_match match, segment_pos segments << segment path_template = match.post_match end # Whatever String is unmatched needs to be added to the segments segments << path_template unless path_template.empty? segments end def segment_and_pos_from_match match, pos if match[:positional] [Segment.new(pos, match[:positional]), pos + 1] else [Segment.new(match[:name], match[:template]), pos] end end end |