Class: KDL::Types::URLTemplate::Parser
- Inherits:
-
Object
- Object
- KDL::Types::URLTemplate::Parser
- Defined in:
- lib/kdl/types/url_template.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(string) ⇒ Parser
constructor
A new instance of Parser.
- #next_token ⇒ Object
- #parse ⇒ Object
- #parse_expansion(string, type) ⇒ Object
Constructor Details
#initialize(string) ⇒ Parser
Returns a new instance of Parser.
28 29 30 31 |
# File 'lib/kdl/types/url_template.rb', line 28 def initialize(string) @string = string @index = 0 end |
Class Method Details
.parse(string) ⇒ Object
24 25 26 |
# File 'lib/kdl/types/url_template.rb', line 24 def self.parse(string) new(string).parse end |
Instance Method Details
#next_token ⇒ Object
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 |
# File 'lib/kdl/types/url_template.rb', line 41 def next_token buffer = '' context = nil expansion_type = nil loop do c = @string[@index] case context when nil case c when '{' context = :expansion buffer = '' n = @string[@index + 1] expansion_type = case n when '+' then ReservedExpansion when '#' then FragmentExpansion when '.' then LabelExpansion when '/' then PathExpansion when ';' then ParameterExpansion when '?' then QueryExpansion when '&' then QueryContinuation else StringExpansion end @index += (expansion_type == StringExpansion ? 1 : 2) when nil then return nil else buffer = c @index += 1 context = :literal end when :literal case c when '{', nil then return StringLiteral.new(buffer) else buffer << c @index += 1 end when :expansion case c when '}' @index += 1 return parse_expansion(buffer, expansion_type) when nil raise ArgumentError, 'unterminated expansion' else buffer << c @index += 1 end end end end |
#parse ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/kdl/types/url_template.rb', line 33 def parse result = [] until (token = next_token).nil? result << token end result end |
#parse_expansion(string, type) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/kdl/types/url_template.rb', line 93 def parse_expansion(string, type) variables = string.split(',').map do |str| case str when /(.*)\*$/ Variable.new(Regexp.last_match(1), explode: true, allow_reserved: type.allow_reserved?, with_name: type.with_name?, keep_empties: type.keep_empties?) when /(.*):(\d+)/ Variable.new(Regexp.last_match(1), limit: Regexp.last_match(2).to_i, allow_reserved: type.allow_reserved?, with_name: type.with_name?, keep_empties: type.keep_empties?) else Variable.new(str, allow_reserved: type.allow_reserved?, with_name: type.with_name?, keep_empties: type.keep_empties?) end end type.new(variables) end |