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.
30 31 32 33 |
# File 'lib/kdl/types/url_template.rb', line 30 def initialize(string) @string = string @index = 0 end |
Class Method Details
.parse(string) ⇒ Object
26 27 28 |
# File 'lib/kdl/types/url_template.rb', line 26 def self.parse(string) new(string).parse end |
Instance Method Details
#next_token ⇒ Object
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 |
# File 'lib/kdl/types/url_template.rb', line 43 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
35 36 37 38 39 40 41 |
# File 'lib/kdl/types/url_template.rb', line 35 def parse result = [] until (token = next_token).nil? result << token end result end |
#parse_expansion(string, type) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/kdl/types/url_template.rb', line 95 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 |